git.iko.soy/typeable/pandoc
on master help sign in
ZimWiki writer: put in PandocMonad, added warnings for raw.
commitea84cd0842ede0ff9835bc4aae47c949ee1d1dd2 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 30 Mar 2017, 21:43 (10 years ago) parentd8a3228
1 file changed+32−17
src/Text/Pandoc/Writers/ZimWiki.hs+32−17
src/Text/Pandoc/Writers/ZimWiki.hs+32−17
@@ -32,13 +32,14 @@ http://zim-wiki.org/manual/Help/Wiki_Syntax.html
3232
3333module Text.Pandoc.Writers.ZimWiki ( writeZimWiki ) where
3434import Control.Monad (zipWithM)
35−import Control.Monad.State (State, evalState, gets, modify)
35+import Control.Monad.State (StateT, evalStateT, gets, modify)
3636import Data.Default (Default (..))
3737import Data.List (intercalate, isInfixOf, isPrefixOf, transpose)
3838import qualified Data.Map as Map
3939import Data.Text (breakOnAll, pack)
4040import Network.URI (isURI)
41−import Text.Pandoc.Class (PandocMonad)
41+import Text.Pandoc.Class (PandocMonad, report)
42+import Text.Pandoc.Logging
4243import Text.Pandoc.Definition
4344import Text.Pandoc.ImageSize
4445import Text.Pandoc.Options (WrapOption (..), WriterOptions (writerTableOfContents, writerTemplate, writerWrapText))
@@ -57,12 +58,14 @@ data WriterState = WriterState {
5758instance Default WriterState where
5859 def = WriterState { stItemNum = 1, stIndent = "", stInTable = False, stInLink = False }
5960
61+type ZW = StateT WriterState
62+
6063-- | Convert Pandoc to ZimWiki.
6164writeZimWiki :: PandocMonad m => WriterOptions -> Pandoc -> m String
62−writeZimWiki opts document = return $ evalState (pandocToZimWiki opts document) def
65+writeZimWiki opts document = evalStateT (pandocToZimWiki opts document) def
6366
6467-- | Return ZimWiki representation of document.
65−pandocToZimWiki :: WriterOptions -> Pandoc -> State WriterState String
68+pandocToZimWiki :: PandocMonad m => WriterOptions -> Pandoc -> ZW m String
6669pandocToZimWiki opts (Pandoc meta blocks) = do
6770 metadata <- metaToJSON opts
6871 (fmap trimr . blockListToZimWiki opts)
@@ -86,7 +89,7 @@ escapeString = substitute "__" "''__''" .
8689 substitute "//" "''//''"
8790
8891-- | Convert Pandoc block element to ZimWiki.
89−blockToZimWiki :: WriterOptions -> Block -> State WriterState String
92+blockToZimWiki :: PandocMonad m => WriterOptions -> Block -> ZW m String
9093
9194blockToZimWiki _ Null = return ""
9295
@@ -118,10 +121,12 @@ blockToZimWiki opts (Para inlines) = do
118121blockToZimWiki opts (LineBlock lns) = do
119122 blockToZimWiki opts $ linesToPara lns
120123
121−blockToZimWiki opts (RawBlock f str)
124+blockToZimWiki opts b@(RawBlock f str)
122125 | f == Format "zimwiki" = return str
123126 | f == Format "html" = do cont <- indentFromHTML opts str; return cont
124− | otherwise = return ""
127+ | otherwise = do
128+ report $ BlockNotRendered b
129+ return ""
125130
126131blockToZimWiki _ HorizontalRule = return "\n----\n"
127132
@@ -198,7 +203,10 @@ blockToZimWiki opts (DefinitionList items) = do
198203 contents <- (mapM (definitionListItemToZimWiki opts) items)
199204 return $ vcat contents
200205
201−definitionListItemToZimWiki :: WriterOptions -> ([Inline],[[Block]]) -> State WriterState String
206+definitionListItemToZimWiki :: PandocMonad m
207+ => WriterOptions
208+ -> ([Inline],[[Block]])
209+ -> ZW m String
202210definitionListItemToZimWiki opts (label, items) = do
203211 labelText <- inlineListToZimWiki opts label
204212 contents <- mapM (blockListToZimWiki opts) items
@@ -206,7 +214,7 @@ definitionListItemToZimWiki opts (label, items) = do
206214 return $ indent ++ "* **" ++ labelText ++ "** " ++ concat contents
207215
208216-- Auxiliary functions for lists:
209−indentFromHTML :: WriterOptions -> String -> State WriterState String
217+indentFromHTML :: PandocMonad m => WriterOptions -> String -> ZW m String
210218indentFromHTML _ str = do
211219 indent <- gets stIndent
212220 itemnum <- gets stItemNum
@@ -239,14 +247,15 @@ vcat :: [String] -> String
239247vcat = intercalate "\n"
240248
241249-- | Convert bullet list item (list of blocks) to ZimWiki.
242−listItemToZimWiki :: WriterOptions -> [Block] -> State WriterState String
250+listItemToZimWiki :: PandocMonad m => WriterOptions -> [Block] -> ZW m String
243251listItemToZimWiki opts items = do
244252 contents <- blockListToZimWiki opts items
245253 indent <- gets stIndent
246254 return $ indent ++ "* " ++ contents
247255
248256-- | Convert ordered list item (list of blocks) to ZimWiki.
249−orderedListItemToZimWiki :: WriterOptions -> [Block] -> State WriterState String
257+orderedListItemToZimWiki :: PandocMonad m
258+ => WriterOptions -> [Block] -> ZW m String
250259orderedListItemToZimWiki opts items = do
251260 contents <- blockListToZimWiki opts items
252261 indent <- gets stIndent
@@ -255,7 +264,8 @@ orderedListItemToZimWiki opts items = do
255264 return $ indent ++ show itemnum ++ ". " ++ contents
256265
257266-- Auxiliary functions for tables:
258−tableItemToZimWiki :: WriterOptions -> Alignment -> [Block] -> State WriterState String
267+tableItemToZimWiki :: PandocMonad m
268+ => WriterOptions -> Alignment -> [Block] -> ZW m String
259269tableItemToZimWiki opts align' item = do
260270 let mkcell x = (if align' == AlignRight || align' == AlignCenter
261271 then " "
@@ -269,15 +279,18 @@ tableItemToZimWiki opts align' item = do
269279 return $ mkcell contents
270280
271281-- | Convert list of Pandoc block elements to ZimWiki.
272−blockListToZimWiki :: WriterOptions -> [Block] -> State WriterState String
282+blockListToZimWiki :: PandocMonad m
283+ => WriterOptions -> [Block] -> ZW m String
273284blockListToZimWiki opts blocks = vcat <$> mapM (blockToZimWiki opts) blocks
274285
275286-- | Convert list of Pandoc inline elements to ZimWiki.
276−inlineListToZimWiki :: WriterOptions -> [Inline] -> State WriterState String
287+inlineListToZimWiki :: PandocMonad m
288+ => WriterOptions -> [Inline] -> ZW m String
277289inlineListToZimWiki opts lst = concat <$> (mapM (inlineToZimWiki opts) lst)
278290
279291-- | Convert Pandoc inline element to ZimWiki.
280−inlineToZimWiki :: WriterOptions -> Inline -> State WriterState String
292+inlineToZimWiki :: PandocMonad m
293+ => WriterOptions -> Inline -> ZW m String
281294
282295inlineToZimWiki opts (Emph lst) = do
283296 contents <- inlineListToZimWiki opts lst
@@ -331,10 +344,12 @@ inlineToZimWiki _ (Math mathType str) = return $ delim ++ str ++ delim -- note
331344 InlineMath -> "$"
332345
333346-- | f == Format "html" = return $ "<html>" ++ str ++ "</html>"
334−inlineToZimWiki opts (RawInline f str)
347+inlineToZimWiki opts il@(RawInline f str)
335348 | f == Format "zimwiki" = return str
336349 | f == Format "html" = do cont <- indentFromHTML opts str; return cont
337− | otherwise = return ""
350+ | otherwise = do
351+ report $ InlineNotRendered il
352+ return ""
338353
339354inlineToZimWiki _ LineBreak = do
340355 inTable <- gets stInTable