X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FWiki%2FEngine.hs;h=02e987cd20f7ed92c6eb7521f3cd2d2e877c6f3f;hb=42f51754dea02201aececaacbf194d714cd58aaf;hp=e3e49ee176c89676cfa12734c7bde40b7fded674;hpb=dc29dc9081156af3b536b19ffd828cdd67ddd84a;p=Rakka.git
diff --git a/Rakka/Wiki/Engine.hs b/Rakka/Wiki/Engine.hs
index e3e49ee..02e987c 100644
--- a/Rakka/Wiki/Engine.hs
+++ b/Rakka/Wiki/Engine.hs
@@ -1,3 +1,10 @@
+{-# LANGUAGE
+ Arrows
+ , OverloadedStrings
+ , ScopedTypeVariables
+ , TypeOperators
+ , UnicodeSyntax
+ #-}
module Rakka.Wiki.Engine
( InterpTable
, makeMainXHTML
@@ -7,18 +14,25 @@ module Rakka.Wiki.Engine
, makeDraft
)
where
-
-import qualified Codec.Binary.Base64 as B64
+import Control.Applicative
+import Control.Arrow
+import Control.Arrow.ArrowIO
+import Control.Arrow.ArrowList
+import Control.Arrow.Unicode
+import Control.Monad.Unicode
import qualified Codec.Binary.UTF8.String as UTF8
-import Control.Arrow
-import Control.Arrow.ArrowIO
-import Control.Arrow.ArrowList
import qualified Data.ByteString.Lazy as Lazy
+import qualified Data.ByteString.Lazy.Char8 as L8 hiding (ByteString)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe
+import Data.Monoid.Unicode
+import Data.Text (Text)
+import qualified Data.Text as T
import Network.HTTP.Lucu
import Network.URI
+import OpenSSL.EVP.Base64
+import Prelude.Unicode
import Rakka.Page
import Rakka.Storage
import Rakka.SystemConfig
@@ -29,45 +43,43 @@ import Rakka.Wiki.Formatter
import Rakka.Wiki.Interpreter
import Text.HyperEstraier hiding (getText)
import Text.ParserCombinators.Parsec
-import Text.XML.HXT.Arrow.XmlArrow hiding (err)
-import Text.XML.HXT.Arrow.XmlNodeSet
-import Text.XML.HXT.DOM.TypeDefs
-
+import Text.XML.HXT.Arrow.XmlArrow hiding (err)
+import Text.XML.HXT.DOM.TypeDefs
+import Text.XML.HXT.XPath
-type InterpTable = Map String Interpreter
+type InterpTable = Map Text Interpreter
-
-wikifyPage :: (ArrowXml a, ArrowChoice a) => InterpTable -> a XmlTree WikiPage
+wikifyPage â· (ArrowXml (â), ArrowChoice (â)) â InterpTable â XmlTree â WikiPage
wikifyPage interpTable
= proc tree
- -> do pType <- getXPathTreesInDoc "/page/@type/text()" >>> getText >>> arr read -< tree
- textData <- maybeA (getXPathTreesInDoc "/page/textData/text()" >>> getText) -< tree
- base64Data <- maybeA (getXPathTreesInDoc "/page/binaryData/text()" >>> getText) -< tree
-
- let dataURI = fmap (binToURI pType) base64Data
-
- case pType of
- MIMEType "text" "x-rakka" _
- -> case parse (wikiPage $ cmdTypeOf interpTable) "" (fromJust textData) of
- Left err -> wikifyParseError -< err
- Right xs -> returnA -< xs
-
- MIMEType "image" _ _
- --
- -> returnA -< [ Paragraph [Image (Left $ fromJust dataURI) Nothing] ]
-
- _ -> if isJust dataURI then
- --
- -- application/zip
- --
- returnA -< [ Paragraph [ Anchor
- [("href", show dataURI)]
- [Text (show pType)]
- ]
- ]
- else
- -- pre
- returnA -< [ Preformatted [Text $ fromJust textData] ]
+ â do pType â getXPathTreesInDoc "/page/@type/text()" â getText â arr read ⤠tree
+ textData â maybeA (getXPathTreesInDoc "/page/textData/text()" â getText) ⤠tree
+ base64Data â maybeA (getXPathTreesInDoc "/page/binaryData/text()" â getText) ⤠tree
+
+ let dataURI = binToURI pType <$> base64Data
+
+ case pType of
+ MIMEType "text" "x-rakka" _
+ â case parse (wikiPage $ cmdTypeOf interpTable) "" (fromJust textData) of
+ Left err â wikifyParseError ⤠err
+ Right xs â returnA ⤠xs
+
+ MIMEType "image" _ _
+ --
+ â returnA ⤠[ Paragraph [Image (Left $ fromJust dataURI) Nothing] ]
+
+ _ â if isJust dataURI then
+ --
+ -- application/zip
+ --
+ returnA ⤠[ Paragraph [ Anchor
+ [("href", T.pack $ show dataURI)]
+ [Text (T.pack $ show pType)]
+ ]
+ ]
+ else
+ -- pre
+ returnA ⤠[ Preformatted [Text â T.pack $ fromJust textData] ]
where
binToURI :: MIMEType -> String -> URI
binToURI pType base64Data
@@ -83,62 +95,61 @@ wikifyPage interpTable
| otherwise = x : stripWhiteSpace xs
-wikifyBin :: (ArrowXml a, ArrowChoice a) => InterpTable -> a (MIMEType, Lazy.ByteString) WikiPage
+wikifyBin :: (ArrowXml (â), ArrowChoice (â)) â InterpTable â (MIMEType, Lazy.ByteString) â WikiPage
wikifyBin interpTable
= proc (pType, pBin)
- -> do let text = UTF8.decode $ Lazy.unpack pBin
- dataURI = binToURI pType pBin
-
- case pType of
- MIMEType "text" "x-rakka" _
- -> case parse (wikiPage $ cmdTypeOf interpTable) "" text of
- Left err -> wikifyParseError -< err
- Right xs -> returnA -< xs
-
- MIMEType "image" _ _
- --
- -> returnA -< [ Paragraph [Image (Left dataURI) Nothing] ]
-
-
- _
- --
- -- application/zip (19372 bytes)
- --
- -> returnA -< [ Paragraph [ Anchor
- [("href", show dataURI)]
- [Text (show pType ++
- " (" ++
- show (Lazy.length pBin) ++
- " bytes)")]
- ]
- ]
+ â do let text = UTF8.decode $ Lazy.unpack pBin
+ dataURI = binToURI pType pBin
+
+ case pType of
+ MIMEType "text" "x-rakka" _
+ -> case parse (wikiPage $ cmdTypeOf interpTable) "" text of
+ Left err -> wikifyParseError -< err
+ Right xs -> returnA -< xs
+
+ MIMEType "image" _ _
+ --
+ -> returnA -< [ Paragraph [Image (Left dataURI) Nothing] ]
+
+ _ --
+ -- application/zip (19372 bytes)
+ --
+ -> returnA -< [ Paragraph [ Anchor
+ [("href", T.pack $ show dataURI)]
+ [Text (T.concat [ T.pack $ show pType
+ , "("
+ , T.pack â show $ Lazy.length pBin
+ , " bytes)"
+ ])]
+ ]
+ ]
where
binToURI :: MIMEType -> Lazy.ByteString -> URI
binToURI m b
= nullURI {
uriScheme = "data:"
- , uriPath = show m ++ ";base64," ++ B64.encode (Lazy.unpack b)
+ , uriPath = show m ++ ";base64," ++ (L8.unpack $ encodeBase64LBS b)
}
-
-cmdTypeOf :: InterpTable -> String -> Maybe CommandType
+cmdTypeOf â· Alternative f â InterpTable â Text â f CommandType
cmdTypeOf interpTable name
- = fmap commandType (M.lookup name interpTable)
-
-
-makeMainXHTML :: (ArrowXml a, ArrowChoice a, ArrowIO a) =>
- Storage
- -> SystemConfig
- -> InterpTable
- -> a XmlTree XmlTree
+ = case M.lookup name interpTable of
+ Just t â pure $ commandType t
+ Nothing â empty
+
+makeMainXHTML â· (ArrowXml (â), ArrowChoice (â), ArrowIO (â))
+ â Storage
+ â SystemConfig
+ â InterpTable
+ â XmlTree â XmlTree
makeMainXHTML sto sysConf interpTable
= proc tree
- -> do BaseURI baseURI <- getSysConfA sysConf -< ()
- wiki <- wikifyPage interpTable -< tree
- pName <- getXPathTreesInDoc "/page/@name/text()" >>> getText -< tree
- interpreted <- interpretCommands sto sysConf interpTable
- -< (Just pName, Just tree, Just wiki, wiki)
- formatWikiBlocks -< (baseURI, interpreted)
+ â do BaseURI baseURI â getSysConfA sysConf ⤠()
+ wiki â wikifyPage interpTable ⤠tree
+ pName â getXPathTreesInDoc "/page/@name/text()" â getText ⤠tree
+ interpreted â interpretCommands sto sysConf interpTable
+ ⤠(Just (T.pack pName), Just tree, Just wiki, wiki)
+ formatWikiBlocks ⤠(baseURI, interpreted)
makeSubXHTML :: (ArrowXml a, ArrowChoice a, ArrowIO a) =>
@@ -223,115 +234,112 @@ interpretCommands sto sysConf interpTable
desc' <- mapM (interpInline ctx) desc
return (Definition term' desc')
- interpBlockCommand :: InterpreterContext -> BlockCommand -> IO BlockElement
+ interpBlockCommand â· InterpreterContext â BlockCommand â IO BlockElement
interpBlockCommand ctx cmd
= case M.lookup (bCmdName cmd) interpTable of
Nothing
- -> fail ("no such interpreter: " ++ bCmdName cmd)
+ â fail ("no such interpreter: " â T.unpack (bCmdName cmd))
Just interp
- -> bciInterpret interp ctx cmd
- >>=
- interpBlock ctx
+ â bciInterpret interp ctx cmd
+ â«=
+ interpBlock ctx
- interpInlineCommand :: InterpreterContext -> InlineCommand -> IO InlineElement
+ interpInlineCommand â· InterpreterContext â InlineCommand â IO InlineElement
interpInlineCommand ctx cmd
= case M.lookup (iCmdName cmd) interpTable of
Nothing
- -> fail ("no such interpreter: " ++ iCmdName cmd)
+ â fail ("no such interpreter: " â T.unpack (iCmdName cmd))
Just interp
- -> iciInterpret interp ctx cmd
- >>=
- interpInline ctx
-
+ â iciInterpret interp ctx cmd â«= interpInline ctx
-makeDraft :: (ArrowXml a, ArrowChoice a, ArrowIO a) => InterpTable -> a XmlTree Document
+makeDraft â· â(â). (ArrowXml (â), ArrowChoice (â), ArrowIO (â)) â InterpTable â XmlTree â Document
makeDraft interpTable
- = proc tree ->
- do redir <- maybeA (getXPathTreesInDoc "/page/@redirect") -< tree
+ = proc tree â
+ do redir â maybeA (getXPathTreesInDoc "/page/@redirect") ⤠tree
case redir of
- Nothing -> makeEntityDraft -< tree
- Just _ -> makeRedirectDraft -< tree
+ Nothing â makeEntityDraft ⤠tree
+ Just _ â makeRedirectDraft ⤠tree
where
- makeEntityDraft :: (ArrowXml a, ArrowChoice a, ArrowIO a) => a XmlTree Document
+ makeEntityDraft â· XmlTree â Document
makeEntityDraft
- = proc tree ->
- do doc <- arrIO0 newDocument -< ()
+ = proc tree â
+ do doc â arrIO0 newDocument ⤠()
- pName <- getXPathTreesInDoc "/page/@name/text()" >>> getText -< tree
- pType <- getXPathTreesInDoc "/page/@type/text()" >>> getText -< tree
- pLastMod <- getXPathTreesInDoc "/page/@lastModified/text()" >>> getText -< tree
- pIsLocked <- getXPathTreesInDoc "/page/@isLocked/text()" >>> getText -< tree
- pIsBinary <- getXPathTreesInDoc "/page/@isBinary/text()" >>> getText -< tree
- pRevision <- getXPathTreesInDoc "/page/@revision/text()" >>> getText -< tree
- pLang <- maybeA (getXPathTreesInDoc "/page/@lang/text()" >>> getText) -< tree
- pIsTheme <- maybeA (getXPathTreesInDoc "/page/@isTheme/text()" >>> getText) -< tree
- pIsFeed <- maybeA (getXPathTreesInDoc "/page/@isFeed/text()" >>> getText) -< tree
- pSummary <- maybeA (getXPathTreesInDoc "/page/summary/text()" >>> getText) -< tree
-
- arrIO2 setURI -< (doc, Just $ mkRakkaURI pName)
- arrIO2 (flip setAttribute "@title" ) -< (doc, Just pName)
- arrIO2 (flip setAttribute "@type" ) -< (doc, Just pType)
- arrIO2 (flip setAttribute "@mdate" ) -< (doc, Just pLastMod)
- arrIO2 (flip setAttribute "@lang" ) -< (doc, pLang)
- arrIO2 (flip setAttribute "rakka:isLocked") -< (doc, Just pIsLocked)
- arrIO2 (flip setAttribute "rakka:isBinary") -< (doc, Just pIsBinary)
- arrIO2 (flip setAttribute "rakka:revision") -< (doc, Just pRevision)
- arrIO2 (flip setAttribute "rakka:summary" ) -< (doc, pSummary)
-
- arrIO2 addHiddenText -< (doc, pName)
+ pName â getXPathTreesInDoc "/page/@name/text()" â getText ⤠tree
+ pType â getXPathTreesInDoc "/page/@type/text()" â getText ⤠tree
+ pLastMod â getXPathTreesInDoc "/page/@lastModified/text()" â getText ⤠tree
+ pIsLocked â getXPathTreesInDoc "/page/@isLocked/text()" â getText ⤠tree
+ pIsBinary â getXPathTreesInDoc "/page/@isBinary/text()" â getText ⤠tree
+ pRevision â getXPathTreesInDoc "/page/@revision/text()" â getText ⤠tree
+ pLang â maybeA (getXPathTreesInDoc "/page/@lang/text()" â getText) ⤠tree
+ pIsTheme â maybeA (getXPathTreesInDoc "/page/@isTheme/text()" â getText) ⤠tree
+ pIsFeed â maybeA (getXPathTreesInDoc "/page/@isFeed/text()" â getText) ⤠tree
+ pSummary â maybeA (getXPathTreesInDoc "/page/summary/text()" â getText) ⤠tree
+
+ arrIO2 setURI ⤠(doc, Just â mkRakkaURI $ T.pack pName )
+ arrIO2 (flip setAttribute "@title" ) ⤠(doc, Just $ T.pack pName )
+ arrIO2 (flip setAttribute "@type" ) ⤠(doc, Just $ T.pack pType )
+ arrIO2 (flip setAttribute "@mdate" ) ⤠(doc, Just $ T.pack pLastMod )
+ arrIO2 (flip setAttribute "@lang" ) ⤠(doc, T.pack <$> pLang)
+ arrIO2 (flip setAttribute "rakka:isLocked") ⤠(doc, Just $ T.pack pIsLocked)
+ arrIO2 (flip setAttribute "rakka:isBinary") ⤠(doc, Just $ T.pack pIsBinary)
+ arrIO2 (flip setAttribute "rakka:revision") ⤠(doc, Just $ T.pack pRevision)
+ arrIO2 (flip setAttribute "rakka:summary" ) ⤠(doc, T.pack <$> pSummary)
+
+ arrIO2 addHiddenText ⤠(doc, T.pack pName)
case pSummary of
- Just s -> arrIO2 addHiddenText -< (doc, s)
- Nothing -> returnA -< ()
+ Just s â arrIO2 addHiddenText ⤠(doc, T.pack s)
+ Nothing â returnA ⤠()
-- otherLang ã¯ãªã³ã¯å
ãã¼ã¸åã hidden text ã§å
¥ããã
- otherLangs <- listA (getXPathTreesInDoc "/page/otherLang/link/@page/text()" >>> getText) -< tree
+ otherLangs â listA (getXPathTreesInDoc "/page/otherLang/link/@page/text()" â getText) ⤠tree
listA ( (arr fst &&& arrL snd)
- >>>
+ â
arrIO2 addHiddenText
- >>>
+ â
none
- ) -< (doc, otherLangs)
+ ) ⤠(doc, T.pack <$> otherLangs)
case read pType of
MIMEType "text" "css" _
- -> arrIO2 (flip setAttribute "rakka:isTheme") -< (doc, pIsTheme)
+ â arrIO2 (flip setAttribute "rakka:isTheme") ⤠(doc, T.pack <$> pIsTheme)
MIMEType "text" "x-rakka" _
- -- wikify ãã¦èå³ã®ããé¨åã addText ããã
- -> do arrIO2 (flip setAttribute "rakka:isFeed") -< (doc, pIsFeed)
- wiki <- wikifyPage interpTable -< tree
- arrIO2 (mapM_ . addBlockText) -< (doc, wiki)
+ -- wikify ãã¦èå³ã®ããé¨åã addText ããã
+ â do arrIO2 (flip setAttribute "rakka:isFeed") ⤠(doc, T.pack <$> pIsFeed)
+ wiki â wikifyPage interpTable ⤠tree
+ arrIO2 (mapM_ â addBlockText) ⤠(doc, wiki)
MIMEType _ _ _
- -> returnA -< ()
+ â returnA ⤠()
- returnA -< doc
+ returnA ⤠doc
- makeRedirectDraft :: (ArrowXml a, ArrowChoice a, ArrowIO a) => a XmlTree Document
+ makeRedirectDraft â· XmlTree â Document
makeRedirectDraft
- = proc tree ->
- do doc <- arrIO0 newDocument -< ()
+ = proc tree â
+ do doc â arrIO0 newDocument ⤠()
- pName <- getXPathTreesInDoc "/page/@name/text()" >>> getText -< tree
- pRedir <- getXPathTreesInDoc "/page/@redirect/text()" >>> getText -< tree
- pIsLocked <- getXPathTreesInDoc "/page/@isLocked/text()" >>> getText -< tree
- pRevision <- getXPathTreesInDoc "/page/@revision/text()" >>> getText -< tree
- pLastMod <- getXPathTreesInDoc "/page/@lastModified/text()" >>> getText -< tree
+ pName â getXPathTreesInDoc "/page/@name/text()" â getText ⤠tree
+ pRedir â getXPathTreesInDoc "/page/@redirect/text()" â getText ⤠tree
+ pIsLocked â getXPathTreesInDoc "/page/@isLocked/text()" â getText ⤠tree
+ pRevision â getXPathTreesInDoc "/page/@revision/text()" â getText ⤠tree
+ pLastMod â getXPathTreesInDoc "/page/@lastModified/text()" â getText ⤠tree
- arrIO2 setURI -< (doc, Just $ mkRakkaURI pName)
- arrIO2 (flip setAttribute "@title" ) -< (doc, Just pName)
+ arrIO2 setURI -< (doc, Just â mkRakkaURI $ T.pack pName )
+ arrIO2 (flip setAttribute "@title" ) -< (doc, Just $ T.pack pName )
arrIO2 (flip setAttribute "@type" ) -< (doc, Just "application/x-rakka-redirection")
- arrIO2 (flip setAttribute "@mdate" ) -< (doc, Just pLastMod)
- arrIO2 (flip setAttribute "rakka:isLocked") -< (doc, Just pIsLocked)
- arrIO2 (flip setAttribute "rakka:revision") -< (doc, Just pRevision)
+ arrIO2 (flip setAttribute "@mdate" ) -< (doc, Just $ T.pack pLastMod )
+ arrIO2 (flip setAttribute "rakka:isLocked") -< (doc, Just $ T.pack pIsLocked )
+ arrIO2 (flip setAttribute "rakka:revision") -< (doc, Just $ T.pack pRevision )
-- ãªãã¤ã¬ã¯ãå
ãã¼ã¸åã¯ããã¹ãã¨ãã¦å
¥ãã
- arrIO2 addText -< (doc, pRedir)
+ arrIO2 addText ⤠(doc, T.pack pRedir)
- returnA -< doc
+ returnA ⤠doc
addElemText :: Document -> Element -> IO ()
addElemText doc (Block b) = addBlockText doc b
@@ -348,23 +356,23 @@ makeDraft interpTable
addBlockText _ EmptyBlock = return ()
addBlockText doc (BlockCmd bcmd) = addBlockCmdText doc bcmd
- addInlineText :: Document -> InlineElement -> IO ()
+ addInlineText â· Document â InlineElement â IO ()
addInlineText doc (Text text) = addText doc text
addInlineText doc (Italic inlines) = mapM_ (addInlineText doc) inlines
addInlineText doc (Bold inlines) = mapM_ (addInlineText doc) inlines
addInlineText doc (ObjectLink page Nothing) = addText doc page
addInlineText doc (ObjectLink page (Just text)) = addHiddenText doc page
- >> addText doc text
- addInlineText doc (PageLink page fragm Nothing) = addText doc (fromMaybe "" page ++ fromMaybe "" fragm)
- addInlineText doc (PageLink page fragm (Just text)) = addHiddenText doc (fromMaybe "" page ++ fromMaybe "" fragm)
- >> addText doc text
- addInlineText doc (ExternalLink uri Nothing) = addText doc (uriToString id uri "")
- addInlineText doc (ExternalLink uri (Just text)) = addHiddenText doc (uriToString id uri "")
- >> addText doc text
+ *> addText doc text
+ addInlineText doc (PageLink page fragm Nothing) = addText doc (fromMaybe (â
) page â maybe (â
) (T.cons '#') fragm)
+ addInlineText doc (PageLink page fragm (Just text)) = addHiddenText doc (fromMaybe (â
) page â maybe (â
) (T.cons '#') fragm)
+ *> addText doc text
+ addInlineText doc (ExternalLink uri Nothing) = addText doc (T.pack $ uriToString id uri "")
+ addInlineText doc (ExternalLink uri (Just text)) = addHiddenText doc (T.pack $ uriToString id uri "")
+ *> addText doc text
addInlineText _ (LineBreak _) = return ()
addInlineText doc (Span _ inlines) = mapM_ (addInlineText doc) inlines
addInlineText doc (Image src alt) = do case src of
- Left uri -> addHiddenText doc (uriToString id uri "")
+ Left uri -> addHiddenText doc (T.pack $ uriToString id uri "")
Right page -> addHiddenText doc page
case alt of
Just text -> addHiddenText doc text
@@ -389,18 +397,18 @@ makeDraft interpTable
addInlineCmdText doc (InlineCommand _ _ inlines) = mapM_ (addInlineText doc) inlines
-makePageLinkList :: (ArrowXml a, ArrowChoice a, ArrowIO a) =>
- Storage
- -> SystemConfig
- -> InterpTable
- -> a XmlTree [PageName]
+makePageLinkList â· (ArrowXml (â), ArrowChoice (â), ArrowIO (â))
+ â Storage
+ â SystemConfig
+ â InterpTable
+ â XmlTree â [PageName]
makePageLinkList sto sysConf interpTable
= proc tree
- -> do wiki <- wikifyPage interpTable -< tree
- pName <- getXPathTreesInDoc "/page/@name/text()" >>> getText -< tree
- interpreted <- interpretCommands sto sysConf interpTable
- -< (Just pName, Just tree, Just wiki, wiki)
- returnA -< concatMap extractFromBlock interpreted
+ â do wiki â wikifyPage interpTable ⤠tree
+ pName â getXPathTreesInDoc "/page/@name/text()" â getText ⤠tree
+ interpreted â interpretCommands sto sysConf interpTable
+ ⤠(Just (T.pack pName), Just tree, Just wiki, wiki)
+ returnA ⤠concatMap extractFromBlock interpreted
where
extractFromElem :: Element -> [PageName]
extractFromElem (Block b) = extractFromBlock b
@@ -430,8 +438,7 @@ makePageLinkList sto sysConf interpTable
++
concatMap extractFromInline desc
-
-wikifyParseError :: Arrow a => a ParseError WikiPage
+wikifyParseError â· Arrow (â) â ParseError â WikiPage
wikifyParseError = proc err
- -> returnA -< [Div [("class", "error")]
- [ Block (Preformatted [Text (show err)]) ]]
+ â returnA -< [Div [("class", "error")]
+ [ Block (Preformatted [Text (T.pack $ show err)]) ]]