git.iko.soy/typeable/pandoc
on master help sign in
ZimWiki writer: number ordered list items sequentially...

rather than always with 1.

commit714e0eb83433cc319ac5fe3eaa570ae2c3c80fdc authorJohn MacFarlane <jgm@berkeley.edu> dateWed 31 Oct 2018, 22:02 (8 years ago) parente0290fd
2 files changed+44−48
src/Text/Pandoc/Writers/ZimWiki.hs+24−28 test/writer.zimwiki+20−20
src/Text/Pandoc/Writers/ZimWiki.hs+24−28
@@ -52,14 +52,13 @@ import Text.Pandoc.Templates (renderTemplate')
5252import Text.Pandoc.Writers.Shared (defField, metaToJSON)
5353
5454data WriterState = WriterState {
55− stItemNum :: Int,
5655 stIndent :: String, -- Indent after the marker at the beginning of list items
5756 stInTable :: Bool, -- Inside a table
5857 stInLink :: Bool -- Inside a link description
5958 }
6059
6160instance Default WriterState where
62− def = WriterState { stItemNum = 1, stIndent = "", stInTable = False, stInLink = False }
61+ def = WriterState { stIndent = "", stInTable = False, stInLink = False }
6362
6463type ZW = StateT WriterState
6564
@@ -188,7 +187,7 @@ blockToZimWiki opts (BulletList items) = do
188187 return $ vcat contents ++ if null indent then "\n" else ""
189188
190189blockToZimWiki opts (OrderedList _ items) = do
191− contents <- mapM (orderedListItemToZimWiki opts) items
190+ contents <- zipWithM (orderedListItemToZimWiki opts) [1..] items
192191 indent <- gets stIndent
193192 return $ vcat contents ++ if null indent then "\n" else ""
194193
@@ -210,25 +209,24 @@ definitionListItemToZimWiki opts (label, items) = do
210209indentFromHTML :: PandocMonad m => WriterOptions -> String -> ZW m String
211210indentFromHTML _ str = do
212211 indent <- gets stIndent
213− itemnum <- gets stItemNum
214− if "<li>" `isInfixOf` str then return $ indent ++ show itemnum ++ "."
215− else if "</li>" `isInfixOf` str then return "\n"
216− else if "<li value=" `isInfixOf` str then do
217− -- poor man's cut
218− let val = drop 10 $ reverse $ drop 1 $ reverse str
219− --let val = take ((length valls) - 2) valls
220− modify $ \s -> s { stItemNum = read val }
221− return ""
222− else if "<ol>" `isInfixOf` str then do
223− let olcount=countSubStrs "<ol>" str
224− modify $ \s -> s { stIndent = stIndent s ++ replicate olcount '\t', stItemNum = 1 }
225− return ""
226− else if "</ol>" `isInfixOf` str then do
227− let olcount=countSubStrs "/<ol>" str
228− modify $ \s -> s{ stIndent = drop olcount (stIndent s) }
229− return ""
230− else
231− return ""
212+ if "<li>" `isInfixOf` str
213+ then return indent
214+ else if "</li>" `isInfixOf` str
215+ then return "\n"
216+ else if "<li value=" `isInfixOf` str
217+ then return ""
218+ else if "<ol>" `isInfixOf` str
219+ then do
220+ let olcount=countSubStrs "<ol>" str
221+ modify $ \s -> s { stIndent = stIndent s ++
222+ replicate olcount '\t' }
223+ return ""
224+ else if "</ol>" `isInfixOf` str
225+ then do
226+ let olcount=countSubStrs "/<ol>" str
227+ modify $ \s -> s{ stIndent = drop olcount (stIndent s) }
228+ return ""
229+ else return ""
232230
233231countSubStrs :: String -> String -> Int
234232countSubStrs sub str = length $ breakOnAll (pack sub) (pack str)
@@ -250,14 +248,12 @@ listItemToZimWiki opts items = do
250248
251249-- | Convert ordered list item (list of blocks) to ZimWiki.
252250orderedListItemToZimWiki :: PandocMonad m
253− => WriterOptions -> [Block] -> ZW m String
254−orderedListItemToZimWiki opts items = do
251+ => WriterOptions -> Int -> [Block] -> ZW m String
252+orderedListItemToZimWiki opts itemnum items = do
255253 indent <- gets stIndent
256254 modify $ \s -> s { stIndent = indent ++ "\t" }
257255 contents <- blockListToZimWiki opts items
258256 modify $ \s -> s{ stIndent = indent }
259− itemnum <- gets stItemNum
260− --modify $ \s -> s { stItemNum = itemnum + 1 } -- this is not strictly necessary for zim as zim does its own renumbering
261257 return $ indent ++ show itemnum ++ ". " ++ contents
262258
263259-- Auxiliary functions for tables:
@@ -343,8 +339,8 @@ inlineToZimWiki _ (Math mathType str) = return $ delim ++ str ++ delim -- note
343339-- | f == Format "html" = return $ "<html>" ++ str ++ "</html>"
344340inlineToZimWiki opts il@(RawInline f str)
345341 | f == Format "zimwiki" = return str
346− | f == Format "html" = indentFromHTML opts str
347− | otherwise = do
342+ | f == Format "html" = indentFromHTML opts str
343+ | otherwise = do
348344 report $ InlineNotRendered il
349345 return ""
350346
test/writer.zimwiki+20−20
@@ -62,7 +62,7 @@ E-mail style:
6262> A list:
6363>
6464> 1. item one
65−> 1. item two
65+> 2. item two
6666>
6767> Nested block quotes:
6868>
@@ -147,33 +147,33 @@ Minuses loose:
147147Tight:
148148
1491491. First
150−1. Second
151−1. Third
150+2. Second
151+3. Third
152152
153153and:
154154
1551551. One
156−1. Two
157−1. Three
156+2. Two
157+3. Three
158158
159159Loose using tabs:
160160
1611611. First
162−1. Second
163−1. Third
162+2. Second
163+3. Third
164164
165165and using spaces:
166166
1671671. One
168−1. Two
169−1. Three
168+2. Two
169+3. Three
170170
171171Multiple paragraphs:
172172
1731731. Item 1, graf one.
174174Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.
175−1. Item 2.
176−1. Item 3.
175+2. Item 2.
176+3. Item 3.
177177
178178===== Nested =====
179179
@@ -184,20 +184,20 @@ Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.
184184Here’s another:
185185
1861861. First
187−1. Second:
187+2. Second:
188188 * Fee
189189 * Fie
190190 * Foe
191−1. Third
191+3. Third
192192
193193Same thing but with paragraphs:
194194
1951951. First
196−1. Second:
196+2. Second:
197197 * Fee
198198 * Fie
199199 * Foe
200−1. Third
200+3. Third
201201
202202===== Tabs and spaces =====
203203
@@ -209,12 +209,12 @@ Same thing but with paragraphs:
209209===== Fancy list markers =====
210210
2112111. begins with 2
212−1. and now 3
212+2. and now 3
213213with a continuation
214214 1. sublist with roman numerals, starting with 4
215− 1. more items
216− 1. a subsublist
215+ 2. more items
217216 1. a subsublist
217+ 2. a subsublist
218218
219219Nesting:
220220
@@ -226,7 +226,7 @@ Nesting:
226226Autonumbering:
227227
2282281. Autonumber.
229−1. More.
229+2. More.
230230 1. Nested.
231231
232232Should not be a list item:
@@ -292,7 +292,7 @@ computer
292292* **orange** orange fruit
293293
2942941. sublist
295−1. sublist
295+2. sublist
296296
297297====== HTML Blocks ======
298298