@@ -0,0 +1,435 @@
2+Copyright (C) 2008-2014 John MacFarlane <jgm@berkeley.edu>
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+ Module : Text.Pandoc.Writers.DokuWiki
21+ Copyright : Copyright (C) 2008-2014 John MacFarlane
22+ License : GNU GPL, version 2 or above
24+ Maintainer : John MacFarlane <jgm@berkeley.edu>
26+ Portability : portable
28+Conversion of 'Pandoc' documents to DokuWiki markup.
30+DokuWiki: <https://www.dokuwiki.org/dokuwiki>
34+ [ ] Implement nested blockquotes (currently only ever does one level)
35+ [ ] Implement alignment of text in tables
36+ [ ] Implement comments
37+ [ ] Work through the Dokuwiki spec, and check I've not missed anything out
38+ [ ] Remove dud/duplicate code
41+module Text.Pandoc.Writers.DokuWiki ( writeDokuWiki ) where
42+import 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')
47+import Data.List ( intersect, intercalate )
48+import Network.URI ( isURI )
49+import Control.Monad.State
51+data WriterState = WriterState {
52+ stNotes :: Bool -- True if there are notes
53+ , stIndent :: String -- Indent after the marker at the beginning of list items
54+ , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
57+-- | Convert Pandoc to DokuWiki.
58+writeDokuWiki :: WriterOptions -> Pandoc -> String
59+writeDokuWiki opts document =
60+ evalState (pandocToDokuWiki opts document)
61+ (WriterState { stNotes = False, stIndent = "", stUseTags = False })
63+-- | Return DokuWiki representation of document.
64+pandocToDokuWiki :: WriterOptions -> Pandoc -> State WriterState String
65+pandocToDokuWiki opts (Pandoc meta blocks) = do
66+ metadata <- metaToJSON opts
67+ (fmap trimr . blockListToDokuWiki opts)
68+ (inlineListToDokuWiki opts)
70+ body <- blockListToDokuWiki opts blocks
71+ notesExist <- get >>= return . stNotes
72+ let notes = if notesExist
73+ then "" -- TODO Was "\n<references />" Check whether I can really remove this:
74+ -- if it is definitely to do with footnotes, can remove this whole bit
76+ let main = body ++ notes
77+ let context = defField "body" main
78+ $ defField "toc" (writerTableOfContents opts)
80+ if writerStandalone opts
81+ then return $ renderTemplate' (writerTemplate opts) context
84+-- | Escape special characters for MediaWiki.
85+escapeString :: String -> String
86+escapeString str = substitute "__" "%%__%%" ( substitute "**" "%%**%%" ( substitute "//" "%%//%%" str ) )
88+-- | Convert Pandoc block element to DokuWiki.
89+blockToDokuWiki :: WriterOptions -- ^ Options
90+ -> Block -- ^ Block element
91+ -> State WriterState String
93+blockToDokuWiki _ Null = return ""
95+blockToDokuWiki opts (Div _attrs bs) = do
96+ contents <- blockListToDokuWiki opts bs
97+ return $ contents ++ "\n"
99+blockToDokuWiki opts (Plain inlines) =
100+ inlineListToDokuWiki opts inlines
102+-- title beginning with fig: indicates that the image is a figure
103+-- dokuwiki doesn't support captions - so combine together alt and caption into alt
104+blockToDokuWiki opts (Para [Image txt (src,'f':'i':'g':':':tit)]) = do
107+ else (" " ++) `fmap` inlineListToDokuWiki opts txt
108+ let opt = if null txt
110+ else "|" ++ if null tit then capt else tit ++ capt
111+ return $ "{{:" ++ src ++ opt ++ "}}\n"
113+blockToDokuWiki opts (Para inlines) = do
114+ useTags <- get >>= return . stUseTags
115+ indent <- get >>= return . stIndent
116+ contents <- inlineListToDokuWiki opts inlines
118+ then "<p>" ++ contents ++ "</p>"
119+ else contents ++ if null indent then "\n" else ""
121+blockToDokuWiki _ (RawBlock f str)
122+ | f == Format "mediawiki" = return str
123+ | f == Format "html" = return $ "<html>\n" ++ str ++ "</html>"
124+ | otherwise = return str
126+blockToDokuWiki _ HorizontalRule = return "\n----\n"
128+blockToDokuWiki opts (Header level _ inlines) = do
129+ contents <- inlineListToDokuWiki opts inlines
130+ let eqs = replicate ( 7 - level ) '='
131+ return $ eqs ++ " " ++ contents ++ " " ++ eqs ++ "\n"
133+blockToDokuWiki _ (CodeBlock (_,classes,_) str) = do
134+ let at = classes `intersect` ["actionscript", "ada", "apache", "applescript", "asm", "asp",
135+ "autoit", "bash", "blitzbasic", "bnf", "c", "c_mac", "caddcl", "cadlisp", "cfdg", "cfm",
136+ "cpp", "cpp-qt", "csharp", "css", "d", "delphi", "diff", "div", "dos", "eiffel", "fortran",
137+ "freebasic", "gml", "groovy", "html4strict", "idl", "ini", "inno", "io", "java", "java5",
138+ "javascript", "latex", "lisp", "lua", "matlab", "mirc", "mpasm", "mysql", "nsis", "objc",
139+ "ocaml", "ocaml-brief", "oobas", "oracle8", "pascal", "perl", "php", "php-brief", "plsql",
140+ "python", "qbasic", "rails", "reg", "robots", "ruby", "sas", "scheme", "sdlbasic",
141+ "smalltalk", "smarty", "sql", "tcl", "", "thinbasic", "tsql", "vb", "vbnet", "vhdl",
142+ "visualfoxpro", "winbatch", "xml", "xpp", "z80"]
143+ let (beg, end) = if null at
144+ then ("<code" ++ if null classes then ">" else " class=\"" ++ unwords classes ++ "\">", "</code>")
145+ else ("<source lang=\"" ++ head at ++ "\">", "</source>")
146+ return $ beg ++ str ++ end
148+blockToDokuWiki opts (BlockQuote blocks) = do
149+ contents <- blockListToDokuWiki opts blocks
150+ return $ "> " ++ contents
152+blockToDokuWiki opts (Table capt aligns _ headers rows') = do
153+ let alignStrings = map alignmentToString aligns
154+ captionDoc <- if null capt
157+ c <- inlineListToDokuWiki opts capt
158+ return $ "" ++ c ++ "\n"
159+ head' <- if all null headers
162+ hs <- tableHeaderToDokuWiki opts alignStrings 0 headers
164+ body' <- zipWithM (tableRowToDokuWiki opts alignStrings) [1..] rows'
165+ return $ captionDoc ++ head' ++
168+blockToDokuWiki opts x@(BulletList items) = do
169+ oldUseTags <- get >>= return . stUseTags
170+ indent <- get >>= return . stIndent
171+ let useTags = oldUseTags || not (isSimpleList x)
174+ modify $ \s -> s { stUseTags = True }
175+ contents <- mapM (listItemToDokuWiki opts) items
176+ modify $ \s -> s { stUseTags = oldUseTags }
177+ return $ "<ul>\n" ++ vcat contents ++ "</ul>\n"
179+ modify $ \s -> s { stIndent = stIndent s ++ " " }
180+ contents <- mapM (listItemToDokuWiki opts) items
181+ modify $ \s -> s { stIndent = indent }
182+ return $ vcat contents ++ if null indent then "\n" else ""
184+blockToDokuWiki opts x@(OrderedList attribs items) = do
185+ oldUseTags <- get >>= return . stUseTags
186+ indent <- get >>= return . stIndent
187+ let useTags = oldUseTags || not (isSimpleList x)
190+ modify $ \s -> s { stUseTags = True }
191+ contents <- mapM (orderedListItemToDokuWiki opts) items
192+ modify $ \s -> s { stUseTags = oldUseTags }
193+ return $ "<ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++ "</ol>\n"
195+ modify $ \s -> s { stIndent = stIndent s ++ " " }
196+ contents <- mapM (orderedListItemToDokuWiki opts) items
197+ modify $ \s -> s { stIndent = indent }
198+ return $ vcat contents ++ if null indent then "\n" else ""
200+-- TODO Need to decide how to make definition lists work on dokuwiki - I don't think there
201+-- is a specific representation of them.
202+-- TODO This creates double '; ; ' if there is a bullet or ordered list inside a definition list
203+blockToDokuWiki opts x@(DefinitionList items) = do
204+ oldUseTags <- get >>= return . stUseTags
205+ indent <- get >>= return . stIndent
206+ let useTags = oldUseTags || not (isSimpleList x)
209+ modify $ \s -> s { stUseTags = True }
210+ contents <- mapM (definitionListItemToDokuWiki opts) items
211+ modify $ \s -> s { stUseTags = oldUseTags }
212+ return $ "<dl>\n" ++ vcat contents ++ "</dl>\n"
214+ modify $ \s -> s { stIndent = stIndent s ++ " " }
215+ contents <- mapM (definitionListItemToDokuWiki opts) items
216+ modify $ \s -> s { stIndent = indent }
217+ return $ vcat contents ++ if null indent then "\n" else ""
219+-- Auxiliary functions for lists:
221+-- | Convert ordered list attributes to HTML attribute string
222+listAttribsToString :: ListAttributes -> String
223+listAttribsToString (startnum, numstyle, _) =
224+ let numstyle' = camelCaseToHyphenated $ show numstyle
225+ in (if startnum /= 1
226+ then " start=\"" ++ show startnum ++ "\""
228+ (if numstyle /= DefaultStyle
229+ then " style=\"list-style-type: " ++ numstyle' ++ ";\""
232+-- | Convert bullet list item (list of blocks) to DokuWiki.
233+listItemToDokuWiki :: WriterOptions -> [Block] -> State WriterState String
234+listItemToDokuWiki opts items = do
235+ contents <- blockListToDokuWiki opts items
236+ useTags <- get >>= return . stUseTags
238+ then return $ "<li>" ++ contents ++ "</li>"
240+ indent <- get >>= return . stIndent
241+ return $ indent ++ "* " ++ contents
243+-- | Convert ordered list item (list of blocks) to DokuWiki.
244+-- | TODO Emiminate dreadful duplication of text from listItemToDokuWiki
245+orderedListItemToDokuWiki :: WriterOptions -> [Block] -> State WriterState String
246+orderedListItemToDokuWiki opts items = do
247+ contents <- blockListToDokuWiki opts items
248+ useTags <- get >>= return . stUseTags
250+ then return $ "<li>" ++ contents ++ "</li>"
252+ indent <- get >>= return . stIndent
253+ return $ indent ++ "- " ++ contents
255+-- | Convert definition list item (label, list of blocks) to DokuWiki.
256+definitionListItemToDokuWiki :: WriterOptions
257+ -> ([Inline],[[Block]])
258+ -> State WriterState String
259+definitionListItemToDokuWiki opts (label, items) = do
260+ labelText <- inlineListToDokuWiki opts label
261+ contents <- mapM (blockListToDokuWiki opts) items
262+ useTags <- get >>= return . stUseTags
264+ then return $ "<dt>" ++ labelText ++ "</dt>\n" ++
265+ (intercalate "\n" $ map (\d -> "<dd>" ++ d ++ "</dd>") contents)
267+ indent <- get >>= return . stIndent
268+ return $ indent ++ "* **" ++ labelText ++ "** " ++ concat contents
270+-- | True if the list can be handled by simple wiki markup, False if HTML tags will be needed.
271+isSimpleList :: Block -> Bool
274+ BulletList _ -> True
275+ OrderedList _ _ -> True
276+ DefinitionList _ -> True
279+-- | Concatenates strings with line breaks between them.
280+vcat :: [String] -> String
281+vcat = intercalate "\n"
283+-- Auxiliary functions for tables:
285+-- TODO Eliminate copy-and-pasted code in tableHeaderToDokuWiki and tableRowToDokuWiki
286+tableHeaderToDokuWiki :: WriterOptions
290+ -> State WriterState String
291+tableHeaderToDokuWiki opts alignStrings rownum cols' = do
292+ let celltype = if rownum == 0 then "" else ""
293+ cols'' <- sequence $ zipWith
294+ (\alignment item -> tableItemToDokuWiki opts celltype alignment item)
296+ return $ "^ " ++ "" ++ joinHeaders cols'' ++ " ^"
298+tableRowToDokuWiki :: WriterOptions
302+ -> State WriterState String
303+tableRowToDokuWiki opts alignStrings rownum cols' = do
304+ let celltype = if rownum == 0 then "" else ""
305+ cols'' <- sequence $ zipWith
306+ (\alignment item -> tableItemToDokuWiki opts celltype alignment item)
308+ return $ "| " ++ "" ++ joinColumns cols'' ++ " |"
310+alignmentToString :: Alignment -> [Char]
311+alignmentToString alignment = case alignment of
317+tableItemToDokuWiki :: WriterOptions
321+ -> State WriterState String
322+-- TODO Fix celltype and align' defined but not used
323+tableItemToDokuWiki opts _celltype _align' item = do
324+ let mkcell x = "" ++ x ++ ""
325+ contents <- blockListToDokuWiki opts item
326+ return $ mkcell contents
328+-- | Concatenates columns together.
329+joinColumns :: [String] -> String
330+joinColumns = intercalate " | "
332+-- | Concatenates headers together.
333+joinHeaders :: [String] -> String
334+joinHeaders = intercalate " ^ "
336+-- | Convert list of Pandoc block elements to DokuWiki.
337+blockListToDokuWiki :: WriterOptions -- ^ Options
338+ -> [Block] -- ^ List of block elements
339+ -> State WriterState String
340+blockListToDokuWiki opts blocks =
341+ mapM (blockToDokuWiki opts) blocks >>= return . vcat
343+-- | Convert list of Pandoc inline elements to DokuWiki.
344+inlineListToDokuWiki :: WriterOptions -> [Inline] -> State WriterState String
345+inlineListToDokuWiki opts lst = mapM (inlineToDokuWiki opts) lst >>= return . concat
347+-- | Convert Pandoc inline element to DokuWiki.
348+inlineToDokuWiki :: WriterOptions -> Inline -> State WriterState String
350+inlineToDokuWiki opts (Span _attrs ils) = do
351+ contents <- inlineListToDokuWiki opts ils
354+inlineToDokuWiki opts (Emph lst) = do
355+ contents <- inlineListToDokuWiki opts lst
356+ return $ "//" ++ contents ++ "//"
358+inlineToDokuWiki opts (Strong lst) = do
359+ contents <- inlineListToDokuWiki opts lst
360+ return $ "**" ++ contents ++ "**"
362+inlineToDokuWiki opts (Strikeout lst) = do
363+ contents <- inlineListToDokuWiki opts lst
364+ return $ "<del>" ++ contents ++ "</del>"
366+inlineToDokuWiki opts (Superscript lst) = do
367+ contents <- inlineListToDokuWiki opts lst
368+ return $ "<sup>" ++ contents ++ "</sup>"
370+inlineToDokuWiki opts (Subscript lst) = do
371+ contents <- inlineListToDokuWiki opts lst
372+ return $ "<sub>" ++ contents ++ "</sub>"
374+inlineToDokuWiki opts (SmallCaps lst) = inlineListToDokuWiki opts lst
376+inlineToDokuWiki opts (Quoted SingleQuote lst) = do
377+ contents <- inlineListToDokuWiki opts lst
378+ return $ "\8216" ++ contents ++ "\8217"
380+inlineToDokuWiki opts (Quoted DoubleQuote lst) = do
381+ contents <- inlineListToDokuWiki opts lst
382+ return $ "\8220" ++ contents ++ "\8221"
384+inlineToDokuWiki opts (Cite _ lst) = inlineListToDokuWiki opts lst
386+inlineToDokuWiki _ (Code _ str) =
387+ -- In dokuwiki, text surrounded by '' is really just a font statement, i.e. <tt>,
388+ -- and so other formatting can be present inside.
389+ -- However, in pandoc, and markdown, inlined code doesn't contain formatting.
390+ -- So I have opted for using %% to disable all formatting inside inline code blocks.
391+ -- This gives the best results when converting from other formats to dokuwiki, even if
392+ -- the resultand code is a little ugly, for short strings that don't contain formatting
394+ -- It does mean that if pandoc could ever read dokuwiki, and so round-trip the format,
395+ -- any formatting inside inlined code blocks would be lost, or presented incorrectly.
396+ return $ "''%%" ++ str ++ "%%''"
398+inlineToDokuWiki _ (Str str) = return $ escapeString str
400+inlineToDokuWiki _ (Math _ str) = return $ "<math>" ++ str ++ "</math>"
401+ -- note: str should NOT be escaped
403+inlineToDokuWiki _ (RawInline f str)
404+ | f == Format "mediawiki" = return str
405+ | f == Format "html" = return str
406+ | otherwise = return str
408+inlineToDokuWiki _ (LineBreak) = return "\\\\ "
410+inlineToDokuWiki _ Space = return " "
412+inlineToDokuWiki opts (Link txt (src, _)) = do
413+ label <- inlineListToDokuWiki opts txt
415+ [Str s] | escapeURI s == src -> return src
417+ then return $ "[[" ++ src ++ "|" ++ label ++ "]]"
418+ else return $ "[[" ++ src' ++ "|" ++ label ++ "]]"
419+ where src' = case src of
420+ '/':xs -> xs -- with leading / it's a
421+ _ -> src -- link to a help page
422+inlineToDokuWiki opts (Image alt (source, tit)) = do
423+ alt' <- inlineListToDokuWiki opts alt
424+ let txt = if (null tit)
429+ return $ "{{:" ++ source ++ txt ++ "}}"
431+inlineToDokuWiki opts (Note contents) = do
432+ contents' <- blockListToDokuWiki opts contents
433+ modify (\s -> s { stNotes = True })
434+ return $ "((" ++ contents' ++ "))"
435+ -- note - may not work for notes with multiple blocks