@@ -44,22 +44,27 @@ import Control.Monad.State
45data WriterState = WriterState {
45data WriterState = WriterState {
46 stNotes :: Bool -- True if there are notes
46 stNotes :: Bool -- True if there are notes
47− , stListLevel :: String -- String at beginning of list items, e.g. "**"
48− , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
49+data WriterReader = WriterReader {
50+ options :: WriterOptions -- Writer options
51+ , stListLevel :: String -- String at beginning of list items, e.g. "**"
52+ , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
51−type MediaWikiWriter = ReaderT WriterOptions (State WriterState)
55+type MediaWikiWriter = ReaderT WriterReader (State WriterState)
53-- | Convert Pandoc to MediaWiki.
57-- | Convert Pandoc to MediaWiki.
54writeMediaWiki :: WriterOptions -> Pandoc -> String
58writeMediaWiki :: WriterOptions -> Pandoc -> String
55writeMediaWiki opts document =
59writeMediaWiki opts document =
56− evalState (runReaderT (pandocToMediaWiki document) opts)
57− WriterState { stNotes = False, stListLevel = [], stUseTags = False }
60+ let initialState = WriterState { stNotes = False }
61+ env = WriterReader { options = opts, stListLevel = [], stUseTags = False }
62+ in evalState (runReaderT (pandocToMediaWiki document) env) initialState
59-- | Return MediaWiki representation of document.
64-- | Return MediaWiki representation of document.
60pandocToMediaWiki :: Pandoc -> MediaWikiWriter String
65pandocToMediaWiki :: Pandoc -> MediaWikiWriter String
61pandocToMediaWiki (Pandoc meta blocks) = do
66pandocToMediaWiki (Pandoc meta blocks) = do
62− opts <- ask
67+ opts <- asks options
63 metadata <- metaToJSON opts
68 metadata <- metaToJSON opts
64 (fmap trimr . blockListToMediaWiki)
69 (fmap trimr . blockListToMediaWiki)
65 inlineListToMediaWiki
70 inlineListToMediaWiki
@@ -105,8 +110,8 @@ blockToMediaWiki (Para [Image txt (src,'f':'i':'g':':':tit)]) = do
105 return $ "[[Image:" ++ src ++ "|frame|none" ++ opt ++ "]]\n"
110 return $ "[[Image:" ++ src ++ "|frame|none" ++ opt ++ "]]\n"
107blockToMediaWiki (Para inlines) = do
112blockToMediaWiki (Para inlines) = do
108− useTags <- gets stUseTags
113+ useTags <- asks stUseTags
109− listLevel <- gets stListLevel
114+ listLevel <- asks stListLevel
110 contents <- inlineListToMediaWiki inlines
115 contents <- inlineListToMediaWiki inlines
111 return $ if useTags
116 return $ if useTags
112 then "<p>" ++ contents ++ "</p>"
117 then "<p>" ++ contents ++ "</p>"
@@ -157,51 +162,39 @@ blockToMediaWiki (Table capt aligns widths headers rows') = do
157 return $ "{|\n" ++ caption ++ tableBody ++ "|}\n"
162 return $ "{|\n" ++ caption ++ tableBody ++ "|}\n"
159blockToMediaWiki x@(BulletList items) = do
164blockToMediaWiki x@(BulletList items) = do
160− oldUseTags <- gets stUseTags
165+ oldUseTags <- asks stUseTags
161− listLevel <- gets stListLevel
166+ listLevel <- asks stListLevel
162 let useTags = oldUseTags || not (isSimpleList x)
167 let useTags = oldUseTags || not (isSimpleList x)
163 if useTags
168 if useTags
165− modify $ \s -> s { stUseTags = True }
166− contents <- mapM listItemToMediaWiki items
170+ contents <- local (\ s -> s { stUseTags = True }) $ mapM listItemToMediaWiki items
167− modify $ \s -> s { stUseTags = oldUseTags }
168 return $ "<ul>\n" ++ vcat contents ++ "</ul>\n"
171 return $ "<ul>\n" ++ vcat contents ++ "</ul>\n"
170− modify $ \s -> s { stListLevel = stListLevel s ++ "*" }
171− contents <- mapM listItemToMediaWiki items
173+ contents <- local (\s -> s { stListLevel = stListLevel s ++ "*" }) $ mapM listItemToMediaWiki items
172− modify $ \s -> s { stListLevel = init (stListLevel s) }
173 return $ vcat contents ++ if null listLevel then "\n" else ""
174 return $ vcat contents ++ if null listLevel then "\n" else ""
175blockToMediaWiki x@(OrderedList attribs items) = do
176blockToMediaWiki x@(OrderedList attribs items) = do
176− oldUseTags <- gets stUseTags
177+ oldUseTags <- asks stUseTags
177− listLevel <- gets stListLevel
178+ listLevel <- asks stListLevel
178 let useTags = oldUseTags || not (isSimpleList x)
179 let useTags = oldUseTags || not (isSimpleList x)
179 if useTags
180 if useTags
181− modify $ \s -> s { stUseTags = True }
182− contents <- mapM listItemToMediaWiki items
182+ contents <- local (\s -> s { stUseTags = True }) $ mapM listItemToMediaWiki items
183− modify $ \s -> s { stUseTags = oldUseTags }
184 return $ "<ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++ "</ol>\n"
183 return $ "<ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++ "</ol>\n"
186− modify $ \s -> s { stListLevel = stListLevel s ++ "#" }
187− contents <- mapM listItemToMediaWiki items
185+ contents <- local (\s -> s { stListLevel = stListLevel s ++ "#" }) $ mapM listItemToMediaWiki items
188− modify $ \s -> s { stListLevel = init (stListLevel s) }
189 return $ vcat contents ++ if null listLevel then "\n" else ""
186 return $ vcat contents ++ if null listLevel then "\n" else ""
191blockToMediaWiki x@(DefinitionList items) = do
188blockToMediaWiki x@(DefinitionList items) = do
192− oldUseTags <- gets stUseTags
189+ oldUseTags <- asks stUseTags
193− listLevel <- gets stListLevel
190+ listLevel <- asks stListLevel
194 let useTags = oldUseTags || not (isSimpleList x)
191 let useTags = oldUseTags || not (isSimpleList x)
195 if useTags
192 if useTags
197− modify $ \s -> s { stUseTags = True }
198− contents <- mapM definitionListItemToMediaWiki items
194+ contents <- local (\s -> s { stUseTags = True }) $ mapM definitionListItemToMediaWiki items
199− modify $ \s -> s { stUseTags = oldUseTags }
200 return $ "<dl>\n" ++ vcat contents ++ "</dl>\n"
195 return $ "<dl>\n" ++ vcat contents ++ "</dl>\n"
202− modify $ \s -> s { stListLevel = stListLevel s ++ ";" }
203− contents <- mapM definitionListItemToMediaWiki items
197+ contents <- local (\s -> s { stListLevel = stListLevel s ++ ";" }) $ mapM definitionListItemToMediaWiki items
204− modify $ \s -> s { stListLevel = init (stListLevel s) }
205 return $ vcat contents ++ if null listLevel then "\n" else ""
198 return $ vcat contents ++ if null listLevel then "\n" else ""
207-- Auxiliary functions for lists:
200-- Auxiliary functions for lists:
@@ -221,11 +214,11 @@ listAttribsToString (startnum, numstyle, _) =
221listItemToMediaWiki :: [Block] -> MediaWikiWriter String
214listItemToMediaWiki :: [Block] -> MediaWikiWriter String
222listItemToMediaWiki items = do
215listItemToMediaWiki items = do
223 contents <- blockListToMediaWiki items
216 contents <- blockListToMediaWiki items
224− useTags <- gets stUseTags
217+ useTags <- asks stUseTags
225 if useTags
218 if useTags
226 then return $ "<li>" ++ contents ++ "</li>"
219 then return $ "<li>" ++ contents ++ "</li>"
228− marker <- gets stListLevel
221+ marker <- asks stListLevel
229 return $ marker ++ " " ++ contents
222 return $ marker ++ " " ++ contents
231-- | Convert definition list item (label, list of blocks) to MediaWiki.
224-- | Convert definition list item (label, list of blocks) to MediaWiki.
@@ -234,12 +227,12 @@ definitionListItemToMediaWiki :: ([Inline],[[Block]])
234definitionListItemToMediaWiki (label, items) = do
227definitionListItemToMediaWiki (label, items) = do
235 labelText <- inlineListToMediaWiki label
228 labelText <- inlineListToMediaWiki label
236 contents <- mapM blockListToMediaWiki items
229 contents <- mapM blockListToMediaWiki items
237− useTags <- gets stUseTags
230+ useTags <- asks stUseTags
238 if useTags
231 if useTags
239 then return $ "<dt>" ++ labelText ++ "</dt>\n" ++
232 then return $ "<dt>" ++ labelText ++ "</dt>\n" ++
240 intercalate "\n" (map (\d -> "<dd>" ++ d ++ "</dd>") contents)
233 intercalate "\n" (map (\d -> "<dd>" ++ d ++ "</dd>") contents)
242− marker <- gets stListLevel
235+ marker <- asks stListLevel
243 return $ marker ++ " " ++ labelText ++ "\n" ++
236 return $ marker ++ " " ++ labelText ++ "\n" ++
244 intercalate "\n" (map (\d -> init marker ++ ": " ++ d) contents)
237 intercalate "\n" (map (\d -> init marker ++ ": " ++ d) contents)