diff options
author | claremacrae <github@cfmacrae.fastmail.co.uk> | 2013-08-28 08:09:42 +0100 |
---|---|---|
committer | claremacrae <github@cfmacrae.fastmail.co.uk> | 2013-08-28 08:09:42 +0100 |
commit | 78ae3c24929e02710ef55614694435ecb1029390 (patch) | |
tree | 6ac1f0e576b618c80f0993f334c3b22af74aa4a0 | |
parent | fe1040d283649a3e25d08fe8a12bbc024420ea96 (diff) | |
download | pandoc-78ae3c24929e02710ef55614694435ecb1029390.tar.gz |
Implement definition lists in dokuwiki writer (#386) - credit: James Smaldon
-rw-r--r-- | src/Text/Pandoc/Writers/DokuWiki.hs | 55 | ||||
-rw-r--r-- | tests/writer.dokuwiki | 21 |
2 files changed, 36 insertions, 40 deletions
diff --git a/src/Text/Pandoc/Writers/DokuWiki.hs b/src/Text/Pandoc/Writers/DokuWiki.hs index 321e8f428..41e4d7a8f 100644 --- a/src/Text/Pandoc/Writers/DokuWiki.hs +++ b/src/Text/Pandoc/Writers/DokuWiki.hs @@ -32,7 +32,6 @@ DokuWiki: <https://www.dokuwiki.org/dokuwiki> {- [ ] Don't generate <blockquote>... - [ ] Implement definition lists [ ] Don't generate lists using <ol> and <ul> [ ] Don't generate <div> [ ] Implement alignment of text in tables @@ -54,7 +53,7 @@ import Control.Monad.State data WriterState = WriterState { stNotes :: Bool -- True if there are notes - , stListLevel :: [Char] -- String at beginning of list items, e.g. "**" + , stIndent :: String -- Indent after the marker at the beginning of list items , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list } @@ -62,7 +61,7 @@ data WriterState = WriterState { writeDokuWiki :: WriterOptions -> Pandoc -> String writeDokuWiki opts document = evalState (pandocToDokuWiki opts document) - (WriterState { stNotes = False, stListLevel = [], stUseTags = False }) + (WriterState { stNotes = False, stIndent = "", stUseTags = False }) -- | Return DokuWiki representation of document. pandocToDokuWiki :: WriterOptions -> Pandoc -> State WriterState String @@ -112,11 +111,11 @@ blockToDokuWiki opts (Para [Image txt (src,'f':'i':'g':':':tit)]) = do blockToDokuWiki opts (Para inlines) = do useTags <- get >>= return . stUseTags - listLevel <- get >>= return . stListLevel + indent <- get >>= return . stIndent contents <- inlineListToDokuWiki opts inlines return $ if useTags then "<p>" ++ contents ++ "</p>" - else contents ++ if null listLevel then "\n" else "" + else contents ++ if null indent then "\n" else "" blockToDokuWiki _ (RawBlock "mediawiki" str) = return str blockToDokuWiki _ (RawBlock "html" str) = return str @@ -166,7 +165,7 @@ blockToDokuWiki opts (Table capt aligns _ headers rows') = do blockToDokuWiki opts x@(BulletList items) = do oldUseTags <- get >>= return . stUseTags - listLevel <- get >>= return . stListLevel + indent <- get >>= return . stIndent let useTags = oldUseTags || not (isSimpleList x) if useTags then do @@ -175,14 +174,14 @@ blockToDokuWiki opts x@(BulletList items) = do modify $ \s -> s { stUseTags = oldUseTags } return $ "<ul>\n" ++ vcat contents ++ "</ul>\n" else do - modify $ \s -> s { stListLevel = stListLevel s ++ " " } + modify $ \s -> s { stIndent = stIndent s ++ " " } contents <- mapM (listItemToDokuWiki opts) items - modify $ \s -> s { stListLevel = init (stListLevel s) } - return $ vcat contents ++ if null listLevel then "\n" else "" + modify $ \s -> s { stIndent = indent } + return $ vcat contents ++ if null indent then "\n" else "" blockToDokuWiki opts x@(OrderedList attribs items) = do oldUseTags <- get >>= return . stUseTags - listLevel <- get >>= return . stListLevel + indent <- get >>= return . stIndent let useTags = oldUseTags || not (isSimpleList x) if useTags then do @@ -191,17 +190,17 @@ blockToDokuWiki opts x@(OrderedList attribs items) = do modify $ \s -> s { stUseTags = oldUseTags } return $ "<ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++ "</ol>\n" else do - modify $ \s -> s { stListLevel = stListLevel s ++ " " } + modify $ \s -> s { stIndent = stIndent s ++ " " } contents <- mapM (orderedListItemToDokuWiki opts) items - modify $ \s -> s { stListLevel = init (stListLevel s) } - return $ vcat contents ++ if null listLevel then "\n" else "" + modify $ \s -> s { stIndent = indent } + return $ vcat contents ++ if null indent then "\n" else "" -- TODO Need to decide how to make definition lists work on dokuwiki - I don't think there -- is a specific representation of them. -- TODO This creates double '; ; ' if there is a bullet or ordered list inside a definition list blockToDokuWiki opts x@(DefinitionList items) = do oldUseTags <- get >>= return . stUseTags - listLevel <- get >>= return . stListLevel + indent <- get >>= return . stIndent let useTags = oldUseTags || not (isSimpleList x) if useTags then do @@ -210,10 +209,10 @@ blockToDokuWiki opts x@(DefinitionList items) = do modify $ \s -> s { stUseTags = oldUseTags } return $ "<dl>\n" ++ vcat contents ++ "</dl>\n" else do - modify $ \s -> s { stListLevel = stListLevel s ++ ";" } + modify $ \s -> s { stIndent = stIndent s ++ " " } contents <- mapM (definitionListItemToDokuWiki opts) items - modify $ \s -> s { stListLevel = init (stListLevel s) } - return $ vcat contents ++ if null listLevel then "\n" else "" + modify $ \s -> s { stIndent = indent } + return $ vcat contents ++ if null indent then "\n" else "" -- Auxiliary functions for lists: @@ -236,10 +235,8 @@ listItemToDokuWiki opts items = do if useTags then return $ "<li>" ++ contents ++ "</li>" else do - marker <- get >>= return . stListLevel - -- This marker ++ marker is an awful hack to write 2 spaces per indentation level. - -- I couldn't find a cleaner way of doing it. - return $ marker ++ marker ++ "* " ++ contents + indent <- get >>= return . stIndent + return $ indent ++ "* " ++ contents -- | Convert ordered list item (list of blocks) to DokuWiki. -- | TODO Emiminate dreadful duplication of text from listItemToDokuWiki @@ -250,10 +247,8 @@ orderedListItemToDokuWiki opts items = do if useTags then return $ "<li>" ++ contents ++ "</li>" else do - marker <- get >>= return . stListLevel - -- This marker ++ marker is an awful hack to write 2 spaces per indentation level. - -- I couldn't find a cleaner way of doing it. - return $ marker ++ marker ++ "- " ++ contents + indent <- get >>= return . stIndent + return $ indent ++ "- " ++ contents -- | Convert definition list item (label, list of blocks) to DokuWiki. definitionListItemToDokuWiki :: WriterOptions @@ -267,9 +262,8 @@ definitionListItemToDokuWiki opts (label, items) = do then return $ "<dt>" ++ labelText ++ "</dt>\n" ++ (intercalate "\n" $ map (\d -> "<dd>" ++ d ++ "</dd>") contents) else do - marker <- get >>= return . stListLevel - return $ marker ++ " " ++ labelText ++ "\n" ++ - (intercalate "\n" $ map (\d -> init marker ++ ": " ++ d) contents) + indent <- get >>= return . stIndent + return $ indent ++ "* **" ++ labelText ++ "** " ++ concat contents -- | True if the list can be handled by simple wiki markup, False if HTML tags will be needed. isSimpleList :: Block -> Bool @@ -277,7 +271,7 @@ isSimpleList x = case x of BulletList _ -> True OrderedList _ _ -> True - DefinitionList items -> all isSimpleListItem $ concatMap snd items + DefinitionList _ -> True _ -> False -- | True if list item can be handled with the simple wiki syntax. False if @@ -371,8 +365,7 @@ blockListToDokuWiki opts blocks = -- | Convert list of Pandoc inline elements to DokuWiki. inlineListToDokuWiki :: WriterOptions -> [Inline] -> State WriterState String -inlineListToDokuWiki opts lst = - mapM (inlineToDokuWiki opts) lst >>= return . concat +inlineListToDokuWiki opts lst = mapM (inlineToDokuWiki opts) lst >>= return . concat -- | Convert Pandoc inline element to DokuWiki. inlineToDokuWiki :: WriterOptions -> Inline -> State WriterState String diff --git a/tests/writer.dokuwiki b/tests/writer.dokuwiki index bacece683..132c2d614 100644 --- a/tests/writer.dokuwiki +++ b/tests/writer.dokuwiki @@ -247,25 +247,28 @@ Loose: Multiple blocks with italics: - * **apple** red fruitcontains seeds, crisp, pleasant to taste
- * **orange** orange fruit<code>
-{ orange code block }
-</code>orange block quote
+ * **//apple//** red fruit +contains seeds, crisp, pleasant to taste + * **//orange//** orange fruit +<code>{ orange code block }</code> +<blockquote>orange block quote</blockquote> Multiple definitions, tight: - * **apple** red fruit computer + * **apple** red fruitcomputer * **orange** orange fruitbank Multiple definitions, loose: - * **apple** red fruit computer - * **orange** orange fruit bank + * **apple** red fruitcomputer + * **orange** orange fruitbank Blank line after term, indented marker, alternate markers: - * **apple** red fruit computer - * **orange** orange fruit sublist sublist + * **apple** red fruitcomputer + * **orange** orange fruit + - sublist + - sublist ====== HTML Blocks ====== |