From 714e0eb83433cc319ac5fe3eaa570ae2c3c80fdc Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 31 Oct 2018 22:02:10 -0700 Subject: ZimWiki writer: number ordered list items sequentially... rather than always with 1. --- src/Text/Pandoc/Writers/ZimWiki.hs | 52 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 28 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Writers/ZimWiki.hs b/src/Text/Pandoc/Writers/ZimWiki.hs index 40efaa8cf..84b60fdfe 100644 --- a/src/Text/Pandoc/Writers/ZimWiki.hs +++ b/src/Text/Pandoc/Writers/ZimWiki.hs @@ -52,14 +52,13 @@ import Text.Pandoc.Templates (renderTemplate') import Text.Pandoc.Writers.Shared (defField, metaToJSON) data WriterState = WriterState { - stItemNum :: Int, stIndent :: String, -- Indent after the marker at the beginning of list items stInTable :: Bool, -- Inside a table stInLink :: Bool -- Inside a link description } instance Default WriterState where - def = WriterState { stItemNum = 1, stIndent = "", stInTable = False, stInLink = False } + def = WriterState { stIndent = "", stInTable = False, stInLink = False } type ZW = StateT WriterState @@ -188,7 +187,7 @@ blockToZimWiki opts (BulletList items) = do return $ vcat contents ++ if null indent then "\n" else "" blockToZimWiki opts (OrderedList _ items) = do - contents <- mapM (orderedListItemToZimWiki opts) items + contents <- zipWithM (orderedListItemToZimWiki opts) [1..] items indent <- gets stIndent return $ vcat contents ++ if null indent then "\n" else "" @@ -210,25 +209,24 @@ definitionListItemToZimWiki opts (label, items) = do indentFromHTML :: PandocMonad m => WriterOptions -> String -> ZW m String indentFromHTML _ str = do indent <- gets stIndent - itemnum <- gets stItemNum - if "
  • " `isInfixOf` str then return $ indent ++ show itemnum ++ "." - else if "
  • " `isInfixOf` str then return "\n" - else if "
  • " `isInfixOf` str then do - let olcount=countSubStrs "
      " str - modify $ \s -> s { stIndent = stIndent s ++ replicate olcount '\t', stItemNum = 1 } - return "" - else if "
    " `isInfixOf` str then do - let olcount=countSubStrs "/
      " str - modify $ \s -> s{ stIndent = drop olcount (stIndent s) } - return "" - else - return "" + if "
    1. " `isInfixOf` str + then return indent + else if "
    2. " `isInfixOf` str + then return "\n" + else if "
    3. " `isInfixOf` str + then do + let olcount=countSubStrs "
        " str + modify $ \s -> s { stIndent = stIndent s ++ + replicate olcount '\t' } + return "" + else if "
      " `isInfixOf` str + then do + let olcount=countSubStrs "/
        " str + modify $ \s -> s{ stIndent = drop olcount (stIndent s) } + return "" + else return "" countSubStrs :: String -> String -> Int countSubStrs sub str = length $ breakOnAll (pack sub) (pack str) @@ -250,14 +248,12 @@ listItemToZimWiki opts items = do -- | Convert ordered list item (list of blocks) to ZimWiki. orderedListItemToZimWiki :: PandocMonad m - => WriterOptions -> [Block] -> ZW m String -orderedListItemToZimWiki opts items = do + => WriterOptions -> Int -> [Block] -> ZW m String +orderedListItemToZimWiki opts itemnum items = do indent <- gets stIndent modify $ \s -> s { stIndent = indent ++ "\t" } contents <- blockListToZimWiki opts items modify $ \s -> s{ stIndent = indent } - itemnum <- gets stItemNum - --modify $ \s -> s { stItemNum = itemnum + 1 } -- this is not strictly necessary for zim as zim does its own renumbering return $ indent ++ show itemnum ++ ". " ++ contents -- Auxiliary functions for tables: @@ -343,8 +339,8 @@ inlineToZimWiki _ (Math mathType str) = return $ delim ++ str ++ delim -- note -- | f == Format "html" = return $ "" ++ str ++ "" inlineToZimWiki opts il@(RawInline f str) | f == Format "zimwiki" = return str - | f == Format "html" = indentFromHTML opts str - | otherwise = do + | f == Format "html" = indentFromHTML opts str + | otherwise = do report $ InlineNotRendered il return "" -- cgit v1.2.3