diff options
| author | Lucas Viana <[email protected]> | 2022-01-05 19:02:47 -0300 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2022-01-06 19:33:13 +0100 |
| commit | 4be41e3bb54449597b998788753717a6765b413b (patch) | |
| tree | bf88f9ae9ab540c9e22b81034a4409770cbb9484 /src | |
| parent | ea745822887bd91b34bd808d3037e3da8009a18e (diff) | |
Org reader: support counter cookies in lists
This adds support for counter cookies in org lists. Such cookies are
used to override the item counter in ordered lists. In org it is
possible to set the counter at any list item, but since Pandoc AST does
not support this, we restrict the usage to setting an offset for the
entire ordered list, by using the cookie in the first list item.
Note that even though unordered lists do not have counters, Org Mode
still parses such cookies in unordered lists and suppresses them in the
output, so we do the same.
Also, even though org-list-allow-alphabetical is disabled in Emacs by
default, for some reason alphabetical cookies are always parsed and used
in Org Mode regardlessly of whether this option is enabled or the list
style is decimal, so we do the same.
E.g.
2. test
3. test
Is parsed as an ordered list starting at 1, as before. This also
conforms to Org Mode behaviour.
1. [@2] test
2. test
Is now parsed as an ordered list starting at 2, so that it conforms to
Org Mode behaviour.
Note that when parsing
1. [@2] test
2. [@9] test
the second cookie is silenced and the entire list starts at 2. This is
because the current Pandoc AST does not support expressing a change in
the counter at a specific item.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Readers/Org/BlockStarts.hs | 36 | ||||
| -rw-r--r-- | src/Text/Pandoc/Readers/Org/Blocks.hs | 8 |
2 files changed, 27 insertions, 17 deletions
diff --git a/src/Text/Pandoc/Readers/Org/BlockStarts.hs b/src/Text/Pandoc/Readers/Org/BlockStarts.hs index 11af731fa..eee13834e 100644 --- a/src/Text/Pandoc/Readers/Org/BlockStarts.hs +++ b/src/Text/Pandoc/Readers/Org/BlockStarts.hs @@ -25,8 +25,10 @@ module Text.Pandoc.Readers.Org.BlockStarts import Control.Monad (void) import Data.Text (Text) -import qualified Data.Text as T import Text.Pandoc.Readers.Org.Parsing +import Text.Pandoc.Definition as Pandoc +import Text.Pandoc.Shared (safeRead) +import Data.Char (toLower) -- | Horizontal Line (five -- dashes or more) hline :: Monad m => OrgParser m () @@ -60,30 +62,38 @@ latexEnvStart = try $ latexEnvName :: Monad m => OrgParser m Text latexEnvName = try $ mappend <$> many1Char alphaNum <*> option "" (textStr "*") +listCounterCookie :: Monad m => OrgParser m Int +listCounterCookie = try $ + string "[@" + *> ((many1Char digit >>= safeRead) <|> numFromAlph <$> oneOf asciiAlph) + <* char ']' + <* (skipSpaces <|> lookAhead eol) + where + asciiAlph = ['a'..'z'] ++ ['A'..'Z'] + -- The number makes sense because char is always in asciiAlph + numFromAlph c = fromEnum (toLower c) - fromEnum 'a' + 1 + bulletListStart :: Monad m => OrgParser m Int bulletListStart = try $ do ind <- length <$> many spaceChar -- Unindented lists cannot use '*' bullets. oneOf (if ind == 0 then "+-" else "*+-") skipSpaces1 <|> lookAhead eol - return (ind + 1) - -genericListStart :: Monad m - => OrgParser m Text - -> OrgParser m Int -genericListStart listMarker = try $ do - ind <- length <$> many spaceChar - void listMarker - skipSpaces1 <|> lookAhead eol + optionMaybe listCounterCookie return (ind + 1) eol :: Monad m => OrgParser m () eol = void (char '\n') -orderedListStart :: Monad m => OrgParser m Int -orderedListStart = genericListStart orderedListMarker +orderedListStart :: Monad m => OrgParser m (Int, ListAttributes) +orderedListStart = try $ do + ind <- length <$> many spaceChar + orderedListMarker + skipSpaces1 <|> lookAhead eol + start <- option 1 listCounterCookie + return (ind + 1, (start, DefaultStyle, DefaultDelim)) -- Ordered list markers allowed in org-mode - where orderedListMarker = T.snoc <$> many1Char digit <*> oneOf ".)" + where orderedListMarker = many1Char digit *> oneOf ".)" drawerStart :: Monad m => OrgParser m Text drawerStart = try $ skipSpaces *> drawerName <* skipSpaces <* newline diff --git a/src/Text/Pandoc/Readers/Org/Blocks.hs b/src/Text/Pandoc/Readers/Org/Blocks.hs index fed03426f..957bf6f8d 100644 --- a/src/Text/Pandoc/Readers/Org/Blocks.hs +++ b/src/Text/Pandoc/Readers/Org/Blocks.hs @@ -802,7 +802,7 @@ paraOrPlain = try $ do -- is directly followed by a list item, in which case the block is read as -- plain text. try (guard nl - *> notFollowedBy (inList *> (orderedListStart <|> bulletListStart)) + *> notFollowedBy (inList *> (void orderedListStart <|> void bulletListStart)) $> (B.para <$> ils)) <|> return (B.plain <$> ils) @@ -834,9 +834,9 @@ indented indentedMarker minIndent = try $ do orderedList :: PandocMonad m => OrgParser m (F Blocks) orderedList = try $ do - indent <- lookAhead orderedListStart - fmap (B.orderedList . compactify) . sequence - <$> many1 (listItem (orderedListStart `indented` indent)) + (indent, attr) <- lookAhead orderedListStart + fmap (B.orderedListWith attr . compactify) . sequence + <$> many1 (listItem ((fst <$> orderedListStart) `indented` indent)) definitionListItem :: PandocMonad m => OrgParser m Int |
