git.iko.soy/typeable/pandoc
on master help sign in
DokuWiki writer: Use backslash newlines in table cells.

Write out strings in table cells with backslash linebreaks in place of newlines. We also want to remove the first two spaces of an indent in lists.

commitccda2a902c537d7c81fcf9131d4f92cad6ab968d authorJesse Rosenthal <jrosenthal@jhu.edu> dateFri 29 Aug 2014, 10:34 (12 years ago) parent608e402
1 file changed+36−10
src/Text/Pandoc/Writers/DokuWiki.hs+36−10
src/Text/Pandoc/Writers/DokuWiki.hs+36−10
@@ -63,13 +63,16 @@ data WriterState = WriterState {
6363data WriterEnvironment = WriterEnvironment {
6464 stIndent :: String -- Indent after the marker at the beginning of list items
6565 , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
66+ , stBackSlashLB :: Bool -- True if we should produce formatted strings with newlines (as in a table cell)
6667 }
6768
6869instance Default WriterState where
6970 def = WriterState { stNotes = False }
7071
7172instance Default WriterEnvironment where
72− def = WriterEnvironment { stIndent = "", stUseTags = False }
73+ def = WriterEnvironment { stIndent = ""
74+ , stUseTags = False
75+ , stBackSlashLB = False }
7376
7477type DokuWiki = ReaderT WriterEnvironment (State WriterState)
7578
@@ -197,6 +200,7 @@ blockToDokuWiki opts (Table capt aligns _ headers rows') = do
197200blockToDokuWiki opts x@(BulletList items) = do
198201 oldUseTags <- stUseTags <$> ask
199202 indent <- stIndent <$> ask
203+ backSlash <- stBackSlashLB <$> ask
200204 let useTags = oldUseTags || not (isSimpleList x)
201205 if useTags
202206 then do
@@ -204,13 +208,15 @@ blockToDokuWiki opts x@(BulletList items) = do
204208 (mapM (listItemToDokuWiki opts) items)
205209 return $ "<HTML><ul></HTML>\n" ++ vcat contents ++ "<HTML></ul></HTML>\n"
206210 else do
207− contents <- local (\s -> s { stIndent = stIndent s ++ " " })
211+ contents <- local (\s -> s { stIndent = stIndent s ++ " "
212+ , stBackSlashLB = backSlash})
208213 (mapM (listItemToDokuWiki opts) items)
209214 return $ vcat contents ++ if null indent then "\n" else ""
210215
211216blockToDokuWiki opts x@(OrderedList attribs items) = do
212217 oldUseTags <- stUseTags <$> ask
213218 indent <- stIndent <$> ask
219+ backSlash <- stBackSlashLB <$> ask
214220 let useTags = oldUseTags || not (isSimpleList x)
215221 if useTags
216222 then do
@@ -218,7 +224,8 @@ blockToDokuWiki opts x@(OrderedList attribs items) = do
218224 (mapM (orderedListItemToDokuWiki opts) items)
219225 return $ "<HTML><ol" ++ listAttribsToString attribs ++ "></HTML>\n" ++ vcat contents ++ "<HTML></ol></HTML>\n"
220226 else do
221− contents <- local (\s -> s { stIndent = stIndent s ++ " " })
227+ contents <- local (\s -> s { stIndent = stIndent s ++ " "
228+ , stBackSlashLB = backSlash})
222229 (mapM (orderedListItemToDokuWiki opts) items)
223230 return $ vcat contents ++ if null indent then "\n" else ""
224231
@@ -228,6 +235,7 @@ blockToDokuWiki opts x@(OrderedList attribs items) = do
228235blockToDokuWiki opts x@(DefinitionList items) = do
229236 oldUseTags <- stUseTags <$> ask
230237 indent <- stIndent <$> ask
238+ backSlash <- stBackSlashLB <$> ask
231239 let useTags = oldUseTags || not (isSimpleList x)
232240 if useTags
233241 then do
@@ -235,7 +243,8 @@ blockToDokuWiki opts x@(DefinitionList items) = do
235243 (mapM (definitionListItemToDokuWiki opts) items)
236244 return $ "<HTML><dl></HTML>\n" ++ vcat contents ++ "<HTML></dl></HTML>\n"
237245 else do
238− contents <- local (\s -> s { stIndent = stIndent s ++ " " })
246+ contents <- local (\s -> s { stIndent = stIndent s ++ " "
247+ , stBackSlashLB = backSlash})
239248 (mapM (definitionListItemToDokuWiki opts) items)
240249 return $ vcat contents ++ if null indent then "\n" else ""
241250
@@ -261,7 +270,9 @@ listItemToDokuWiki opts items = do
261270 then return $ "<HTML><li></HTML>" ++ contents ++ "<HTML></li></HTML>"
262271 else do
263272 indent <- stIndent <$> ask
264− return $ indent ++ "* " ++ contents
273+ backSlash <- stBackSlashLB <$> ask
274+ let indent' = if backSlash then (drop 2 indent) else indent
275+ return $ indent' ++ "* " ++ contents
265276
266277-- | Convert ordered list item (list of blocks) to DokuWiki.
267278-- | TODO Emiminate dreadful duplication of text from listItemToDokuWiki
@@ -273,7 +284,9 @@ orderedListItemToDokuWiki opts items = do
273284 then return $ "<HTML><li></HTML>" ++ contents ++ "<HTML></li></HTML>"
274285 else do
275286 indent <- stIndent <$> ask
276− return $ indent ++ "- " ++ contents
287+ backSlash <- stBackSlashLB <$> ask
288+ let indent' = if backSlash then (drop 2 indent) else indent
289+ return $ indent' ++ "- " ++ contents
277290
278291-- | Convert definition list item (label, list of blocks) to DokuWiki.
279292definitionListItemToDokuWiki :: WriterOptions
@@ -288,7 +301,9 @@ definitionListItemToDokuWiki opts (label, items) = do
288301 (intercalate "\n" $ map (\d -> "<HTML><dd></HTML>" ++ d ++ "<HTML></dd></HTML>") contents)
289302 else do
290303 indent <- stIndent <$> ask
291− return $ indent ++ "* **" ++ labelText ++ "** " ++ concat contents
304+ backSlash <- stBackSlashLB <$> ask
305+ let indent' = if backSlash then (drop 2 indent) else indent
306+ return $ indent' ++ "* **" ++ labelText ++ "** " ++ concat contents
292307
293308-- | True if the list can be handled by simple wiki markup, False if HTML tags will be needed.
294309isSimpleList :: Block -> Bool
@@ -334,6 +349,13 @@ isSimpleBlockQuote _ = False
334349vcat :: [String] -> String
335350vcat = intercalate "\n"
336351
352+backSlashLineBreaks :: String -> String
353+backSlashLineBreaks cs = reverse $ g $ reverse $ concatMap f cs
354+ where f '\n' = "\\\\ "
355+ f c = [c]
356+ g (' ' : '\\':'\\': xs) = xs
357+ g s = s
358+
337359-- Auxiliary functions for tables:
338360
339361-- TODO Eliminate copy-and-pasted code in tableHeaderToDokuWiki and tableRowToDokuWiki
@@ -376,7 +398,8 @@ tableItemToDokuWiki :: WriterOptions
376398-- TODO Fix celltype and align' defined but not used
377399tableItemToDokuWiki opts _celltype _align' item = do
378400 let mkcell x = "" ++ x ++ ""
379− contents <- blockListToDokuWiki opts item
401+ contents <- local (\s -> s { stBackSlashLB = True }) $
402+ blockListToDokuWiki opts item
380403 return $ mkcell contents
381404
382405-- | Concatenates columns together.
@@ -391,8 +414,11 @@ joinHeaders = intercalate " ^ "
391414blockListToDokuWiki :: WriterOptions -- ^ Options
392415 -> [Block] -- ^ List of block elements
393416 -> DokuWiki String
394−blockListToDokuWiki opts blocks =
395− vcat <$> mapM (blockToDokuWiki opts) blocks
417+blockListToDokuWiki opts blocks = do
418+ backSlash <- stBackSlashLB <$> ask
419+ if backSlash
420+ then (backSlashLineBreaks . vcat) <$> mapM (blockToDokuWiki opts) blocks
421+ else vcat <$> mapM (blockToDokuWiki opts) blocks
396422
397423-- | Convert list of Pandoc inline elements to DokuWiki.
398424inlineListToDokuWiki :: WriterOptions -> [Inline] -> DokuWiki String