Org writer: support starting number cookies

This complements  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
src/Text/Pandoc/Writers
test
Tests/Writers
writer.org

View file

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

View file

@ -50,6 +50,14 @@ tests =
[ "1. [ ] a" [ "1. [ ] a"
, "2. [X] b" , "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" $ , test (orgWithOpts def) "bullet without task_lists" $
bulletList [plain "☐ a", plain "☒ b"] bulletList [plain "☐ a", plain "☒ b"]
=?> T.unlines =?> T.unlines

View file

@ -278,13 +278,13 @@ Same thing but with paragraphs:
:CUSTOM_ID: fancy-list-markers :CUSTOM_ID: fancy-list-markers
:END: :END:
2) begins with 2 2) [@2] begins with 2
3) and now 3 3) and now 3
with a continuation with a continuation
4. sublist with roman numerals, starting with 4 4. [@4] sublist with roman numerals, starting with 4
5. more items 5. more items
1) a subsublist 1) a subsublist
@ -296,9 +296,9 @@ Nesting:
1. Upper Roman. 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: Autonumbering: