@@ -40,35 +40,56 @@ DokuWiki: <https://www.dokuwiki.org/dokuwiki>
4040
4141module Text.Pandoc.Writers.DokuWiki ( writeDokuWiki ) where
4242import Text.Pandoc.Definition
43−import Text.Pandoc.Options
44−import Text.Pandoc.Shared
45−import Text.Pandoc.Writers.Shared
46−import Text.Pandoc.Templates (renderTemplate')
43+import Text.Pandoc.Options ( WriterOptions(
44+ writerTableOfContents
45+ , writerStandalone
46+ , writerTemplate) )
47+import Text.Pandoc.Shared ( escapeURI, removeFormatting, camelCaseToHyphenated
48+ , trimr, normalize, substitute )
49+import Text.Pandoc.Writers.Shared ( defField, metaToJSON )
50+import Text.Pandoc.Templates ( renderTemplate' )
4751import Data.List ( intersect, intercalate, isPrefixOf )
52+import Data.Default (Default(..))
4853import Network.URI ( isURI )
49−import Control.Monad.State
54+import Control.Monad ( zipWithM )
55+import Control.Monad.State ( modify, State, get, evalState )
56+import Control.Monad.Reader ( ReaderT, runReaderT, ask, local )
57+import Control.Applicative ( (<$>) )
5058
5159data WriterState = WriterState {
5260 stNotes :: Bool -- True if there are notes
53− , stIndent :: String -- Indent after the marker at the beginning of list items
61+ }
62+
63+data WriterEnvironment = WriterEnvironment {
64+ stIndent :: String -- Indent after the marker at the beginning of list items
5465 , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
5566 }
5667
68+instance Default WriterState where
69+ def = WriterState { stNotes = False }
70+
71+instance Default WriterEnvironment where
72+ def = WriterEnvironment { stIndent = "", stUseTags = False }
73+
74+type DokuWiki = ReaderT WriterEnvironment (State WriterState)
75+
5776-- | Convert Pandoc to DokuWiki.
5877writeDokuWiki :: WriterOptions -> Pandoc -> String
5978writeDokuWiki opts document =
60− evalState (pandocToDokuWiki opts $ normalize document)
61− (WriterState { stNotes = False, stIndent = "", stUseTags = False })
79+ runDokuWiki (pandocToDokuWiki opts $ normalize document)
80+
81+runDokuWiki :: DokuWiki a -> a
82+runDokuWiki = flip evalState def . flip runReaderT def
6283
6384-- | Return DokuWiki representation of document.
64−pandocToDokuWiki :: WriterOptions -> Pandoc -> State WriterState String
85+pandocToDokuWiki :: WriterOptions -> Pandoc -> DokuWiki String
6586pandocToDokuWiki opts (Pandoc meta blocks) = do
6687 metadata <- metaToJSON opts
6788 (fmap trimr . blockListToDokuWiki opts)
6889 (inlineListToDokuWiki opts)
6990 meta
7091 body <- blockListToDokuWiki opts blocks
71− notesExist <- get >>= return . stNotes
92+ notesExist <- stNotes <$> get
7293 let notes = if notesExist
7394 then "" -- TODO Was "\n<references />" Check whether I can really remove this:
7495 -- if it is definitely to do with footnotes, can remove this whole bit
@@ -90,7 +111,7 @@ escapeString = substitute "__" "%%__%%" .
90111-- | Convert Pandoc block element to DokuWiki.
91112blockToDokuWiki :: WriterOptions -- ^ Options
92113 -> Block -- ^ Block element
93− -> State WriterState String
114+ -> DokuWiki String
94115
95116blockToDokuWiki _ Null = return ""
96117
@@ -113,8 +134,8 @@ blockToDokuWiki opts (Para [Image txt (src,'f':'i':'g':':':tit)]) = do
113134 return $ "{{:" ++ src ++ opt ++ "}}\n"
114135
115136blockToDokuWiki opts (Para inlines) = do
116− indent <- gets stIndent
117− useTags <- gets stUseTags
137+ indent <- stIndent <$> ask
138+ useTags <- stUseTags <$> ask
118139 contents <- inlineListToDokuWiki opts inlines
119140 return $ if useTags
120141 then "<HTML><p></HTML>" ++ contents ++ "<HTML></p></HTML>"
@@ -174,54 +195,48 @@ blockToDokuWiki opts (Table capt aligns _ headers rows') = do
174195 unlines body'
175196
176197blockToDokuWiki opts x@(BulletList items) = do
177− oldUseTags <- get >>= return . stUseTags
178− indent <- get >>= return . stIndent
198+ oldUseTags <- stUseTags <$> ask
199+ indent <- stIndent <$> ask
179200 let useTags = oldUseTags || not (isSimpleList x)
180201 if useTags
181202 then do
182− modify $ \s -> s { stUseTags = True }
183− contents <- mapM (listItemToDokuWiki opts) items
184− modify $ \s -> s { stUseTags = oldUseTags }
203+ contents <- local (\s -> s { stUseTags = True })
204+ (mapM (listItemToDokuWiki opts) items)
185205 return $ "<HTML><ul></HTML>\n" ++ vcat contents ++ "<HTML></ul></HTML>\n"
186206 else do
187− modify $ \s -> s { stIndent = stIndent s ++ " " }
188− contents <- mapM (listItemToDokuWiki opts) items
189− modify $ \s -> s { stIndent = indent }
207+ contents <- local (\s -> s { stIndent = stIndent s ++ " " })
208+ (mapM (listItemToDokuWiki opts) items)
190209 return $ vcat contents ++ if null indent then "\n" else ""
191210
192211blockToDokuWiki opts x@(OrderedList attribs items) = do
193− oldUseTags <- get >>= return . stUseTags
194− indent <- get >>= return . stIndent
212+ oldUseTags <- stUseTags <$> ask
213+ indent <- stIndent <$> ask
195214 let useTags = oldUseTags || not (isSimpleList x)
196215 if useTags
197216 then do
198− modify $ \s -> s { stUseTags = True }
199− contents <- mapM (orderedListItemToDokuWiki opts) items
200− modify $ \s -> s { stUseTags = oldUseTags }
217+ contents <- local (\s -> s { stUseTags = True })
218+ (mapM (orderedListItemToDokuWiki opts) items)
201219 return $ "<HTML><ol" ++ listAttribsToString attribs ++ "></HTML>\n" ++ vcat contents ++ "<HTML></ol></HTML>\n"
202220 else do
203− modify $ \s -> s { stIndent = stIndent s ++ " " }
204− contents <- mapM (orderedListItemToDokuWiki opts) items
205− modify $ \s -> s { stIndent = indent }
221+ contents <- local (\s -> s { stIndent = stIndent s ++ " " })
222+ (mapM (orderedListItemToDokuWiki opts) items)
206223 return $ vcat contents ++ if null indent then "\n" else ""
207224
208225-- TODO Need to decide how to make definition lists work on dokuwiki - I don't think there
209226-- is a specific representation of them.
210227-- TODO This creates double '; ; ' if there is a bullet or ordered list inside a definition list
211228blockToDokuWiki opts x@(DefinitionList items) = do
212− oldUseTags <- get >>= return . stUseTags
213− indent <- get >>= return . stIndent
229+ oldUseTags <- stUseTags <$> ask
230+ indent <- stIndent <$> ask
214231 let useTags = oldUseTags || not (isSimpleList x)
215232 if useTags
216233 then do
217− modify $ \s -> s { stUseTags = True }
218− contents <- mapM (definitionListItemToDokuWiki opts) items
219− modify $ \s -> s { stUseTags = oldUseTags }
234+ contents <- local (\s -> s { stUseTags = True })
235+ (mapM (definitionListItemToDokuWiki opts) items)
220236 return $ "<HTML><dl></HTML>\n" ++ vcat contents ++ "<HTML></dl></HTML>\n"
221237 else do
222− modify $ \s -> s { stIndent = stIndent s ++ " " }
223− contents <- mapM (definitionListItemToDokuWiki opts) items
224− modify $ \s -> s { stIndent = indent }
238+ contents <- local (\s -> s { stIndent = stIndent s ++ " " })
239+ (mapM (definitionListItemToDokuWiki opts) items)
225240 return $ vcat contents ++ if null indent then "\n" else ""
226241
227242-- Auxiliary functions for lists:
@@ -238,41 +253,41 @@ listAttribsToString (startnum, numstyle, _) =
238253 else "")
239254
240255-- | Convert bullet list item (list of blocks) to DokuWiki.
241−listItemToDokuWiki :: WriterOptions -> [Block] -> State WriterState String
256+listItemToDokuWiki :: WriterOptions -> [Block] -> DokuWiki String
242257listItemToDokuWiki opts items = do
243258 contents <- blockListToDokuWiki opts items
244− useTags <- get >>= return . stUseTags
259+ useTags <- stUseTags <$> ask
245260 if useTags
246261 then return $ "<HTML><li></HTML>" ++ contents ++ "<HTML></li></HTML>"
247262 else do
248− indent <- get >>= return . stIndent
263+ indent <- stIndent <$> ask
249264 return $ indent ++ "* " ++ contents
250265
251266-- | Convert ordered list item (list of blocks) to DokuWiki.
252267-- | TODO Emiminate dreadful duplication of text from listItemToDokuWiki
253−orderedListItemToDokuWiki :: WriterOptions -> [Block] -> State WriterState String
268+orderedListItemToDokuWiki :: WriterOptions -> [Block] -> DokuWiki String
254269orderedListItemToDokuWiki opts items = do
255270 contents <- blockListToDokuWiki opts items
256− useTags <- get >>= return . stUseTags
271+ useTags <- stUseTags <$> ask
257272 if useTags
258273 then return $ "<HTML><li></HTML>" ++ contents ++ "<HTML></li></HTML>"
259274 else do
260− indent <- get >>= return . stIndent
275+ indent <- stIndent <$> ask
261276 return $ indent ++ "- " ++ contents
262277
263278-- | Convert definition list item (label, list of blocks) to DokuWiki.
264279definitionListItemToDokuWiki :: WriterOptions
265280 -> ([Inline],[[Block]])
266− -> State WriterState String
281+ -> DokuWiki String
267282definitionListItemToDokuWiki opts (label, items) = do
268283 labelText <- inlineListToDokuWiki opts label
269284 contents <- mapM (blockListToDokuWiki opts) items
270− useTags <- get >>= return . stUseTags
285+ useTags <- stUseTags <$> ask
271286 if useTags
272287 then return $ "<HTML><dt></HTML>" ++ labelText ++ "<HTML></dt></HTML>\n" ++
273288 (intercalate "\n" $ map (\d -> "<HTML><dd></HTML>" ++ d ++ "<HTML></dd></HTML>") contents)
274289 else do
275− indent <- get >>= return . stIndent
290+ indent <- stIndent <$> ask
276291 return $ indent ++ "* **" ++ labelText ++ "** " ++ concat contents
277292
278293-- | True if the list can be handled by simple wiki markup, False if HTML tags will be needed.
@@ -326,11 +341,11 @@ tableHeaderToDokuWiki :: WriterOptions
326341 -> [String]
327342 -> Int
328343 -> [[Block]]
329− -> State WriterState String
344+ -> DokuWiki String
330345tableHeaderToDokuWiki opts alignStrings rownum cols' = do
331346 let celltype = if rownum == 0 then "" else ""
332− cols'' <- sequence $ zipWith
333− (\alignment item -> tableItemToDokuWiki opts celltype alignment item)
347+ cols'' <- zipWithM
348+ (tableItemToDokuWiki opts celltype)
334349 alignStrings cols'
335350 return $ "^ " ++ "" ++ joinHeaders cols'' ++ " ^"
336351
@@ -338,11 +353,11 @@ tableRowToDokuWiki :: WriterOptions
338353 -> [String]
339354 -> Int
340355 -> [[Block]]
341− -> State WriterState String
356+ -> DokuWiki String
342357tableRowToDokuWiki opts alignStrings rownum cols' = do
343358 let celltype = if rownum == 0 then "" else ""
344− cols'' <- sequence $ zipWith
345− (\alignment item -> tableItemToDokuWiki opts celltype alignment item)
359+ cols'' <- zipWithM
360+ (tableItemToDokuWiki opts celltype)
346361 alignStrings cols'
347362 return $ "| " ++ "" ++ joinColumns cols'' ++ " |"
348363
@@ -357,7 +372,7 @@ tableItemToDokuWiki :: WriterOptions
357372 -> String
358373 -> String
359374 -> [Block]
360− -> State WriterState String
375+ -> DokuWiki String
361376-- TODO Fix celltype and align' defined but not used
362377tableItemToDokuWiki opts _celltype _align' item = do
363378 let mkcell x = "" ++ x ++ ""
@@ -375,20 +390,20 @@ joinHeaders = intercalate " ^ "
375390-- | Convert list of Pandoc block elements to DokuWiki.
376391blockListToDokuWiki :: WriterOptions -- ^ Options
377392 -> [Block] -- ^ List of block elements
378− -> State WriterState String
393+ -> DokuWiki String
379394blockListToDokuWiki opts blocks =
380− mapM (blockToDokuWiki opts) blocks >>= return . vcat
395+ vcat <$> mapM (blockToDokuWiki opts) blocks
381396
382397-- | Convert list of Pandoc inline elements to DokuWiki.
383−inlineListToDokuWiki :: WriterOptions -> [Inline] -> State WriterState String
384−inlineListToDokuWiki opts lst = mapM (inlineToDokuWiki opts) lst >>= return . concat
398+inlineListToDokuWiki :: WriterOptions -> [Inline] -> DokuWiki String
399+inlineListToDokuWiki opts lst =
400+ concat <$> (mapM (inlineToDokuWiki opts) lst)
385401
386402-- | Convert Pandoc inline element to DokuWiki.
387−inlineToDokuWiki :: WriterOptions -> Inline -> State WriterState String
403+inlineToDokuWiki :: WriterOptions -> Inline -> DokuWiki String
388404
389−inlineToDokuWiki opts (Span _attrs ils) = do
390− contents <- inlineListToDokuWiki opts ils
391− return contents
405+inlineToDokuWiki opts (Span _attrs ils) =
406+ inlineListToDokuWiki opts ils
392407
393408inlineToDokuWiki opts (Emph lst) = do
394409 contents <- inlineListToDokuWiki opts lst
@@ -461,11 +476,10 @@ inlineToDokuWiki opts (Link txt (src, _)) = do
461476 _ -> src -- link to a help page
462477inlineToDokuWiki opts (Image alt (source, tit)) = do
463478 alt' <- inlineListToDokuWiki opts alt
464− let txt = if (null tit)
465− then if null alt
466− then ""
467− else "|" ++ alt'
468− else "|" ++ tit
479+ let txt = case (tit, alt) of
480+ ("", []) -> ""
481+ ("", _ ) -> "|" ++ alt'
482+ (_ , _ ) -> "|" ++ tit
469483 return $ "{{:" ++ source ++ txt ++ "}}"
470484
471485inlineToDokuWiki opts (Note contents) = do