git.iko.soy/typeable/pandoc
on master help sign in
AsciiDoc writer: improve ordered lists.

Use `.`+ as list markers to support nested ordered lists. Closes #5087. Support list number styles. Closes #5089.

commita5910c0a31351e1145d82447552cbb37d5f37d86 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 19 Nov 2018, 13:16 (8 years ago) parentc0d8b0a
2 files changed+83−71
src/Text/Pandoc/Writers/AsciiDoc.hs+27−32 test/writer.asciidoc+56−39
src/Text/Pandoc/Writers/AsciiDoc.hs+27−32
@@ -40,7 +40,7 @@ AsciiDoc: <http://www.methods.co.nz/asciidoc/>
4040module Text.Pandoc.Writers.AsciiDoc (writeAsciiDoc) where
4141import Prelude
4242import Control.Monad.State.Strict
43−import Data.Char (isPunctuation, isSpace)
43+import Data.Char (isPunctuation, isSpace, toLower)
4444import Data.List (intercalate, intersperse, stripPrefix)
4545import Data.Maybe (fromMaybe, isJust, listToMaybe)
4646import qualified Data.Set as Set
@@ -258,17 +258,18 @@ blockToAsciiDoc opts (Table caption aligns widths headers rows) = do
258258blockToAsciiDoc opts (BulletList items) = do
259259 contents <- mapM (bulletListItemToAsciiDoc opts) items
260260 return $ cat contents <> blankline
261−blockToAsciiDoc opts (OrderedList (_start, sty, _delim) items) = do
262− let sty' = case sty of
263− UpperRoman -> UpperAlpha
264− LowerRoman -> LowerAlpha
265− x -> x
266− let markers = orderedListMarkers (1, sty', Period) -- start num not used
267− let markers' = map (\m -> if length m < 3
268− then m ++ replicate (3 - length m) ' '
269− else m) markers
270− contents <- zipWithM (orderedListItemToAsciiDoc opts) markers' items
271− return $ cat contents <> blankline
261+blockToAsciiDoc opts (OrderedList (start, sty, _delim) items) = do
262+ let listStyle = case sty of
263+ DefaultStyle -> []
264+ Decimal -> ["arabic"]
265+ Example -> []
266+ _ -> [map toLower (show sty)]
267+ let listStart = if start == 1 then [] else ["start=" ++ show start]
268+ let listoptions = case intercalate ", " (listStyle ++ listStart) of
269+ [] -> empty
270+ x -> brackets (text x)
271+ contents <- mapM (orderedListItemToAsciiDoc opts) items
272+ return $ listoptions $$ cat contents <> blankline
272273blockToAsciiDoc opts (DefinitionList items) = do
273274 contents <- mapM (definitionListItemToAsciiDoc opts) items
274275 return $ cat contents <> blankline
@@ -281,40 +282,34 @@ blockToAsciiDoc opts (Div (ident,_,_) bs) = do
281282bulletListItemToAsciiDoc :: PandocMonad m
282283 => WriterOptions -> [Block] -> ADW m Doc
283284bulletListItemToAsciiDoc opts blocks = do
284− let addBlock :: PandocMonad m => Doc -> Block -> ADW m Doc
285− addBlock d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
286− addBlock d b@(BulletList _) = do x <- blockToAsciiDoc opts b
287− return $ d <> cr <> chomp x
288− addBlock d b@(OrderedList _ _) = do x <- blockToAsciiDoc opts b
289− return $ d <> cr <> chomp x
290− addBlock d b = do x <- blockToAsciiDoc opts b
291− return $ d <> cr <> text "+" <> cr <> chomp x
292285 lev <- gets bulletListLevel
293286 modify $ \s -> s{ bulletListLevel = lev + 1 }
294− contents <- foldM addBlock empty blocks
287+ contents <- foldM (addBlock opts) empty blocks
295288 modify $ \s -> s{ bulletListLevel = lev }
296289 let marker = text (replicate lev '*')
297290 return $ marker <> text " " <> contents <> cr
298291
292+addBlock :: PandocMonad m => WriterOptions -> Doc -> Block -> ADW m Doc
293+addBlock opts d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
294+addBlock opts d b@(BulletList _) = do x <- blockToAsciiDoc opts b
295+ return $ d <> cr <> chomp x
296+addBlock opts d b@(OrderedList _ _) = do x <- blockToAsciiDoc opts b
297+ return $ d <> cr <> chomp x
298+addBlock opts d b = do x <- blockToAsciiDoc opts b
299+ return $ d <> cr <> text "+" <> cr <> chomp x
300+
299301-- | Convert ordered list item (a list of blocks) to asciidoc.
300302orderedListItemToAsciiDoc :: PandocMonad m
301303 => WriterOptions -- ^ options
302− -> String -- ^ list item marker
303304 -> [Block] -- ^ list item (list of blocks)
304305 -> ADW m Doc
305−orderedListItemToAsciiDoc opts marker blocks = do
306− let addBlock d b | isEmpty d = chomp `fmap` blockToAsciiDoc opts b
307− addBlock d b@(BulletList _) = do x <- blockToAsciiDoc opts b
308− return $ d <> cr <> chomp x
309− addBlock d b@(OrderedList _ _) = do x <- blockToAsciiDoc opts b
310− return $ d <> cr <> chomp x
311− addBlock d b = do x <- blockToAsciiDoc opts b
312− return $ d <> cr <> text "+" <> cr <> chomp x
306+orderedListItemToAsciiDoc opts blocks = do
313307 lev <- gets orderedListLevel
314308 modify $ \s -> s{ orderedListLevel = lev + 1 }
315− contents <- foldM addBlock empty blocks
309+ contents <- foldM (addBlock opts) empty blocks
316310 modify $ \s -> s{ orderedListLevel = lev }
317− return $ text marker <> text " " <> contents <> cr
311+ let marker = text (replicate lev '.')
312+ return $ marker <> text " " <> contents <> cr
318313
319314-- | Convert definition list item (label, list of blocks) to asciidoc.
320315definitionListItemToAsciiDoc :: PandocMonad m
test/writer.asciidoc+56−39
@@ -66,8 +66,9 @@ sub status {
6666
6767A list:
6868
69−1. item one
70−2. item two
69+[arabic]
70+. item one
71+. item two
7172
7273Nested block quotes:
7374
@@ -156,35 +157,40 @@ Minuses loose:
156157
157158Tight:
158159
159−1. First
160−2. Second
161−3. Third
160+[arabic]
161+. First
162+. Second
163+. Third
162164
163165and:
164166
165−1. One
166−2. Two
167−3. Three
167+[arabic]
168+. One
169+. Two
170+. Three
168171
169172Loose using tabs:
170173
171−1. First
172−2. Second
173−3. Third
174+[arabic]
175+. First
176+. Second
177+. Third
174178
175179and using spaces:
176180
177−1. One
178−2. Two
179−3. Three
181+[arabic]
182+. One
183+. Two
184+. Three
180185
181186Multiple paragraphs:
182187
183−1. Item 1, graf one.
188+[arabic]
189+. Item 1, graf one.
184190+
185191Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.
186−2. Item 2.
187−3. Item 3.
192+. Item 2.
193+. Item 3.
188194
189195=== Nested
190196
@@ -194,21 +200,23 @@ Item 1. graf two. The quick brown fox jumped over the lazy dog’s back.
194200
195201Here’s another:
196202
197−1. First
198−2. Second:
203+[arabic]
204+. First
205+. Second:
199206* Fee
200207* Fie
201208* Foe
202−3. Third
209+. Third
203210
204211Same thing but with paragraphs:
205212
206−1. First
207−2. Second:
213+[arabic]
214+. First
215+. Second:
208216* Fee
209217* Fie
210218* Foe
211−3. Third
219+. Third
212220
213221=== Tabs and spaces
214222
@@ -219,27 +227,34 @@ Same thing but with paragraphs:
219227
220228=== Fancy list markers
221229
222−1. begins with 2
223−2. and now 3
230+[arabic, start=2]
231+. begins with 2
232+. and now 3
224233+
225234with a continuation
226−a. sublist with roman numerals, starting with 4
227−b. more items
228−A. a subsublist
229−B. a subsublist
235+[lowerroman, start=4]
236+.. sublist with roman numerals, starting with 4
237+.. more items
238+[upperalpha]
239+... a subsublist
240+... a subsublist
230241
231242Nesting:
232243
233−A. Upper Alpha
234−A. Upper Roman.
235−1. Decimal start with 6
236−a. Lower alpha with paren
244+[upperalpha]
245+. Upper Alpha
246+[upperroman]
247+.. Upper Roman.
248+[arabic, start=6]
249+... Decimal start with 6
250+[loweralpha, start=3]
251+.... Lower alpha with paren
237252
238253Autonumbering:
239254
240−1. Autonumber.
241−2. More.
242−1. Nested.
255+. Autonumber.
256+. More.
257+.. Nested.
243258
244259Should not be a list item:
245260
@@ -326,8 +341,9 @@ apple::
326341orange::
327342 orange fruit
328343 +
329− 1. sublist
330− 2. sublist
344+ [arabic]
345+ . sublist
346+ . sublist
331347
332348== HTML Blocks
333349
@@ -622,6 +638,7 @@ ___________________________________________
622638Notes can go in quotes.footnote:[In quote.]
623639___________________________________________
624640
625−1. And in list items.footnote:[In list.]
641+[arabic]
642+. And in list items.footnote:[In list.]
626643
627644This paragraph should not be part of the note, as it is not indented.