Org writer: support starting number cookies

This complements #7806 by supporting writing Org ordered lists that
start at a specific number.
This commit is contained in:
Lucas Viana 2022-01-06 22:40:53 -03:00 committed by John MacFarlane
parent d562de5039
commit 45e2e0d018
3 changed files with 22 additions and 10 deletions

View file

@ -207,10 +207,9 @@ blockToOrg (OrderedList (start, _, delim) items) = do
x -> x
let markers = take (length items) $ orderedListMarkers
(start, Decimal, delim')
let maxMarkerLength = maybe 0 maximum . nonEmpty $ map T.length markers
let markers' = map (\m -> let s = maxMarkerLength - T.length m
in m <> T.replicate s " ") markers
contents <- zipWithM orderedListItemToOrg markers' items
counters = (case start of 1 -> Nothing; n -> Just n) : repeat Nothing
contents <- zipWithM (\x f -> f x) items $
zipWith orderedListItemToOrg markers counters
-- ensure that sublists have preceding blank line
return $ blankline $$
(if isTightList items then vcat else vsep) contents $$
@ -232,12 +231,17 @@ bulletListItemToOrg items = do
-- | Convert ordered list item (a list of blocks) to Org.
orderedListItemToOrg :: PandocMonad m
=> Text -- ^ marker for list item
-> Maybe Int -- ^ maybe number for a counter cookie
-> [Block] -- ^ list item (list of blocks)
-> Org m (Doc Text)
orderedListItemToOrg marker items = do
orderedListItemToOrg marker counter items = do
exts <- gets $ writerExtensions . stOptions
contents <- blockListToOrg (taskListItemToOrg exts items)
return $ hang (T.length marker + 1) (literal marker <> space) contents $$
let cookie = maybe empty
(\n -> space <> literal "[@" <> literal (tshow n) <> literal "]")
counter
return $ hang (T.length marker + 1)
(literal marker <> cookie <> space) contents $$
if endsWithPlain items
then cr
else blankline

View file

@ -50,6 +50,14 @@ tests =
[ "1. [ ] a"
, "2. [X] b"
]
, "ordered task list with starting number"
=: orderedListWith
(9, DefaultStyle, DefaultDelim)
[plain ("" <> space <> "a"), plain "☒ b"]
=?> T.unlines
[ "9. [@9] [ ] a"
, "10. [X] b"
]
, test (orgWithOpts def) "bullet without task_lists" $
bulletList [plain "☐ a", plain "☒ b"]
=?> T.unlines

View file

@ -278,13 +278,13 @@ Same thing but with paragraphs:
:CUSTOM_ID: fancy-list-markers
:END:
2) begins with 2
2) [@2] begins with 2
3) and now 3
with a continuation
4. sublist with roman numerals, starting with 4
4. [@4] sublist with roman numerals, starting with 4
5. more items
1) a subsublist
@ -296,9 +296,9 @@ Nesting:
1. Upper Roman.
6) Decimal start with 6
6) [@6] Decimal start with 6
3) Lower alpha with paren
3) [@3] Lower alpha with paren
Autonumbering: