From 714e0eb83433cc319ac5fe3eaa570ae2c3c80fdc Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 31 Oct 2018 22:02:10 -0700
Subject: [PATCH] ZimWiki writer: number ordered list items sequentially...

rather than always with 1.
---
 src/Text/Pandoc/Writers/ZimWiki.hs | 52 ++++++++++++++----------------
 test/writer.zimwiki                | 40 +++++++++++------------
 2 files changed, 44 insertions(+), 48 deletions(-)

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 "<li>" `isInfixOf` str then return $ indent ++ show itemnum ++ "."
-        else if "</li>" `isInfixOf` str then return "\n"
-                else if "<li value=" `isInfixOf` str then do
-                        -- poor man's cut
-                        let val = drop 10 $ reverse $ drop 1 $ reverse str
-                        --let val = take ((length valls) - 2) valls
-                        modify $ \s -> s { stItemNum = read val }
-                        return ""
-                        else if "<ol>" `isInfixOf` str then do
-                                let olcount=countSubStrs "<ol>" str
-                                modify $ \s -> s { stIndent = stIndent s ++ replicate olcount '\t', stItemNum = 1 }
-                                return ""
-                                else if "</ol>" `isInfixOf` str then do
-                                        let olcount=countSubStrs "/<ol>" str
-                                        modify $ \s -> s{ stIndent = drop olcount (stIndent s) }
-                                        return ""
-                                        else
-                                                return ""
+   if "<li>" `isInfixOf` str
+      then return indent
+      else if "</li>" `isInfixOf` str
+        then return "\n"
+        else if "<li value=" `isInfixOf` str
+          then return ""
+          else if "<ol>" `isInfixOf` str
+            then do
+              let olcount=countSubStrs "<ol>" str
+              modify $ \s -> s { stIndent = stIndent s ++
+                                 replicate olcount '\t' }
+              return ""
+            else if "</ol>" `isInfixOf` str
+              then do
+                let olcount=countSubStrs "/<ol>" 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 $ "<html>" ++ str ++ "</html>"
 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 ""
 
diff --git a/test/writer.zimwiki b/test/writer.zimwiki
index 6e59ae8a2..7783b836c 100644
--- a/test/writer.zimwiki
+++ b/test/writer.zimwiki
@@ -62,7 +62,7 @@ E-mail style:
 > A list:
 > 
 > 1. item one
-> 1. item two
+> 2. item two
 > 
 > Nested block quotes:
 > 
@@ -147,33 +147,33 @@ Minuses loose:
 Tight:
 
 1. First
-1. Second
-1. Third
+2. Second
+3. Third
 
 and:
 
 1. One
-1. Two
-1. Three
+2. Two
+3. Three
 
 Loose using tabs:
 
 1. First
-1. Second
-1. Third
+2. Second
+3. Third
 
 and using spaces:
 
 1. One
-1. Two
-1. Three
+2. Two
+3. Three
 
 Multiple paragraphs:
 
 1. Item 1, graf one.
 Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.
-1. Item 2.
-1. Item 3.
+2. Item 2.
+3. Item 3.
 
 ===== Nested =====
 
@@ -184,20 +184,20 @@ Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.
 Here’s another:
 
 1. First
-1. Second:
+2. Second:
 	* Fee
 	* Fie
 	* Foe
-1. Third
+3. Third
 
 Same thing but with paragraphs:
 
 1. First
-1. Second:
+2. Second:
 	* Fee
 	* Fie
 	* Foe
-1. Third
+3. Third
 
 ===== Tabs and spaces =====
 
@@ -209,12 +209,12 @@ Same thing but with paragraphs:
 ===== Fancy list markers =====
 
 1. begins with 2
-1. and now 3
+2. and now 3
 with a continuation
 	1. sublist with roman numerals, starting with 4
-	1. more items
-		1. a subsublist
+	2. more items
 		1. a subsublist
+		2. a subsublist
 
 Nesting:
 
@@ -226,7 +226,7 @@ Nesting:
 Autonumbering:
 
 1. Autonumber.
-1. More.
+2. More.
 	1. Nested.
 
 Should not be a list item:
@@ -292,7 +292,7 @@ computer
 * **orange** orange fruit
 
 1. sublist
-1. sublist
+2. sublist
 
 ====== HTML Blocks ======