Move more things to Reader.
This commit is contained in:
parent
0bc900e36a
commit
d2c81346e7
1 changed files with 29 additions and 36 deletions
|
@ -44,22 +44,27 @@ import Control.Monad.State
|
||||||
|
|
||||||
data WriterState = WriterState {
|
data WriterState = WriterState {
|
||||||
stNotes :: Bool -- True if there are notes
|
stNotes :: Bool -- True if there are notes
|
||||||
, stListLevel :: String -- String at beginning of list items, e.g. "**"
|
|
||||||
, stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MediaWikiWriter = ReaderT WriterOptions (State WriterState)
|
data WriterReader = WriterReader {
|
||||||
|
options :: WriterOptions -- Writer options
|
||||||
|
, stListLevel :: String -- String at beginning of list items, e.g. "**"
|
||||||
|
, stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaWikiWriter = ReaderT WriterReader (State WriterState)
|
||||||
|
|
||||||
-- | Convert Pandoc to MediaWiki.
|
-- | Convert Pandoc to MediaWiki.
|
||||||
writeMediaWiki :: WriterOptions -> Pandoc -> String
|
writeMediaWiki :: WriterOptions -> Pandoc -> String
|
||||||
writeMediaWiki opts document =
|
writeMediaWiki opts document =
|
||||||
evalState (runReaderT (pandocToMediaWiki document) opts)
|
let initialState = WriterState { stNotes = False }
|
||||||
WriterState { stNotes = False, stListLevel = [], stUseTags = False }
|
env = WriterReader { options = opts, stListLevel = [], stUseTags = False }
|
||||||
|
in evalState (runReaderT (pandocToMediaWiki document) env) initialState
|
||||||
|
|
||||||
-- | Return MediaWiki representation of document.
|
-- | Return MediaWiki representation of document.
|
||||||
pandocToMediaWiki :: Pandoc -> MediaWikiWriter String
|
pandocToMediaWiki :: Pandoc -> MediaWikiWriter String
|
||||||
pandocToMediaWiki (Pandoc meta blocks) = do
|
pandocToMediaWiki (Pandoc meta blocks) = do
|
||||||
opts <- ask
|
opts <- asks options
|
||||||
metadata <- metaToJSON opts
|
metadata <- metaToJSON opts
|
||||||
(fmap trimr . blockListToMediaWiki)
|
(fmap trimr . blockListToMediaWiki)
|
||||||
inlineListToMediaWiki
|
inlineListToMediaWiki
|
||||||
|
@ -105,8 +110,8 @@ blockToMediaWiki (Para [Image txt (src,'f':'i':'g':':':tit)]) = do
|
||||||
return $ "[[Image:" ++ src ++ "|frame|none" ++ opt ++ "]]\n"
|
return $ "[[Image:" ++ src ++ "|frame|none" ++ opt ++ "]]\n"
|
||||||
|
|
||||||
blockToMediaWiki (Para inlines) = do
|
blockToMediaWiki (Para inlines) = do
|
||||||
useTags <- gets stUseTags
|
useTags <- asks stUseTags
|
||||||
listLevel <- gets stListLevel
|
listLevel <- asks stListLevel
|
||||||
contents <- inlineListToMediaWiki inlines
|
contents <- inlineListToMediaWiki inlines
|
||||||
return $ if useTags
|
return $ if useTags
|
||||||
then "<p>" ++ contents ++ "</p>"
|
then "<p>" ++ contents ++ "</p>"
|
||||||
|
@ -157,51 +162,39 @@ blockToMediaWiki (Table capt aligns widths headers rows') = do
|
||||||
return $ "{|\n" ++ caption ++ tableBody ++ "|}\n"
|
return $ "{|\n" ++ caption ++ tableBody ++ "|}\n"
|
||||||
|
|
||||||
blockToMediaWiki x@(BulletList items) = do
|
blockToMediaWiki x@(BulletList items) = do
|
||||||
oldUseTags <- gets stUseTags
|
oldUseTags <- asks stUseTags
|
||||||
listLevel <- gets stListLevel
|
listLevel <- asks stListLevel
|
||||||
let useTags = oldUseTags || not (isSimpleList x)
|
let useTags = oldUseTags || not (isSimpleList x)
|
||||||
if useTags
|
if useTags
|
||||||
then do
|
then do
|
||||||
modify $ \s -> s { stUseTags = True }
|
contents <- local (\ s -> s { stUseTags = True }) $ mapM listItemToMediaWiki items
|
||||||
contents <- mapM listItemToMediaWiki items
|
|
||||||
modify $ \s -> s { stUseTags = oldUseTags }
|
|
||||||
return $ "<ul>\n" ++ vcat contents ++ "</ul>\n"
|
return $ "<ul>\n" ++ vcat contents ++ "</ul>\n"
|
||||||
else do
|
else do
|
||||||
modify $ \s -> s { stListLevel = stListLevel s ++ "*" }
|
contents <- local (\s -> s { stListLevel = stListLevel s ++ "*" }) $ mapM listItemToMediaWiki items
|
||||||
contents <- mapM listItemToMediaWiki items
|
|
||||||
modify $ \s -> s { stListLevel = init (stListLevel s) }
|
|
||||||
return $ vcat contents ++ if null listLevel then "\n" else ""
|
return $ vcat contents ++ if null listLevel then "\n" else ""
|
||||||
|
|
||||||
blockToMediaWiki x@(OrderedList attribs items) = do
|
blockToMediaWiki x@(OrderedList attribs items) = do
|
||||||
oldUseTags <- gets stUseTags
|
oldUseTags <- asks stUseTags
|
||||||
listLevel <- gets stListLevel
|
listLevel <- asks stListLevel
|
||||||
let useTags = oldUseTags || not (isSimpleList x)
|
let useTags = oldUseTags || not (isSimpleList x)
|
||||||
if useTags
|
if useTags
|
||||||
then do
|
then do
|
||||||
modify $ \s -> s { stUseTags = True }
|
contents <- local (\s -> s { stUseTags = True }) $ mapM listItemToMediaWiki items
|
||||||
contents <- mapM listItemToMediaWiki items
|
|
||||||
modify $ \s -> s { stUseTags = oldUseTags }
|
|
||||||
return $ "<ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++ "</ol>\n"
|
return $ "<ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++ "</ol>\n"
|
||||||
else do
|
else do
|
||||||
modify $ \s -> s { stListLevel = stListLevel s ++ "#" }
|
contents <- local (\s -> s { stListLevel = stListLevel s ++ "#" }) $ mapM listItemToMediaWiki items
|
||||||
contents <- mapM listItemToMediaWiki items
|
|
||||||
modify $ \s -> s { stListLevel = init (stListLevel s) }
|
|
||||||
return $ vcat contents ++ if null listLevel then "\n" else ""
|
return $ vcat contents ++ if null listLevel then "\n" else ""
|
||||||
|
|
||||||
blockToMediaWiki x@(DefinitionList items) = do
|
blockToMediaWiki x@(DefinitionList items) = do
|
||||||
oldUseTags <- gets stUseTags
|
oldUseTags <- asks stUseTags
|
||||||
listLevel <- gets stListLevel
|
listLevel <- asks stListLevel
|
||||||
let useTags = oldUseTags || not (isSimpleList x)
|
let useTags = oldUseTags || not (isSimpleList x)
|
||||||
if useTags
|
if useTags
|
||||||
then do
|
then do
|
||||||
modify $ \s -> s { stUseTags = True }
|
contents <- local (\s -> s { stUseTags = True }) $ mapM definitionListItemToMediaWiki items
|
||||||
contents <- mapM definitionListItemToMediaWiki items
|
|
||||||
modify $ \s -> s { stUseTags = oldUseTags }
|
|
||||||
return $ "<dl>\n" ++ vcat contents ++ "</dl>\n"
|
return $ "<dl>\n" ++ vcat contents ++ "</dl>\n"
|
||||||
else do
|
else do
|
||||||
modify $ \s -> s { stListLevel = stListLevel s ++ ";" }
|
contents <- local (\s -> s { stListLevel = stListLevel s ++ ";" }) $ mapM definitionListItemToMediaWiki items
|
||||||
contents <- mapM definitionListItemToMediaWiki items
|
|
||||||
modify $ \s -> s { stListLevel = init (stListLevel s) }
|
|
||||||
return $ vcat contents ++ if null listLevel then "\n" else ""
|
return $ vcat contents ++ if null listLevel then "\n" else ""
|
||||||
|
|
||||||
-- Auxiliary functions for lists:
|
-- Auxiliary functions for lists:
|
||||||
|
@ -221,11 +214,11 @@ listAttribsToString (startnum, numstyle, _) =
|
||||||
listItemToMediaWiki :: [Block] -> MediaWikiWriter String
|
listItemToMediaWiki :: [Block] -> MediaWikiWriter String
|
||||||
listItemToMediaWiki items = do
|
listItemToMediaWiki items = do
|
||||||
contents <- blockListToMediaWiki items
|
contents <- blockListToMediaWiki items
|
||||||
useTags <- gets stUseTags
|
useTags <- asks stUseTags
|
||||||
if useTags
|
if useTags
|
||||||
then return $ "<li>" ++ contents ++ "</li>"
|
then return $ "<li>" ++ contents ++ "</li>"
|
||||||
else do
|
else do
|
||||||
marker <- gets stListLevel
|
marker <- asks stListLevel
|
||||||
return $ marker ++ " " ++ contents
|
return $ marker ++ " " ++ contents
|
||||||
|
|
||||||
-- | Convert definition list item (label, list of blocks) to MediaWiki.
|
-- | Convert definition list item (label, list of blocks) to MediaWiki.
|
||||||
|
@ -234,12 +227,12 @@ definitionListItemToMediaWiki :: ([Inline],[[Block]])
|
||||||
definitionListItemToMediaWiki (label, items) = do
|
definitionListItemToMediaWiki (label, items) = do
|
||||||
labelText <- inlineListToMediaWiki label
|
labelText <- inlineListToMediaWiki label
|
||||||
contents <- mapM blockListToMediaWiki items
|
contents <- mapM blockListToMediaWiki items
|
||||||
useTags <- gets stUseTags
|
useTags <- asks stUseTags
|
||||||
if useTags
|
if useTags
|
||||||
then return $ "<dt>" ++ labelText ++ "</dt>\n" ++
|
then return $ "<dt>" ++ labelText ++ "</dt>\n" ++
|
||||||
intercalate "\n" (map (\d -> "<dd>" ++ d ++ "</dd>") contents)
|
intercalate "\n" (map (\d -> "<dd>" ++ d ++ "</dd>") contents)
|
||||||
else do
|
else do
|
||||||
marker <- gets stListLevel
|
marker <- asks stListLevel
|
||||||
return $ marker ++ " " ++ labelText ++ "\n" ++
|
return $ marker ++ " " ++ labelText ++ "\n" ++
|
||||||
intercalate "\n" (map (\d -> init marker ++ ": " ++ d) contents)
|
intercalate "\n" (map (\d -> init marker ++ ": " ++ d) contents)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue