Skip to content

Commit 84f6b1e

Browse files
committed
Merge pull request #1680 from shelf/master
Respect indent when parsing Org bullet lists
2 parents 31713d5 + f1f56e8 commit 84f6b1e

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

src/Text/Pandoc/Readers/Org.hs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,14 @@ list :: OrgParser (F Blocks)
853853
list = choice [ definitionList, bulletList, orderedList ] <?> "list"
854854

855855
definitionList :: OrgParser (F Blocks)
856-
definitionList = fmap B.definitionList . fmap compactify'DL . sequence
857-
<$> many1 (definitionListItem bulletListStart)
856+
definitionList = try $ do n <- lookAhead (bulletListStart' Nothing)
857+
fmap B.definitionList . fmap compactify'DL . sequence
858+
<$> many1 (definitionListItem $ bulletListStart' (Just n))
858859

859860
bulletList :: OrgParser (F Blocks)
860-
bulletList = fmap B.bulletList . fmap compactify' . sequence
861-
<$> many1 (listItem bulletListStart)
861+
bulletList = try $ do n <- lookAhead (bulletListStart' Nothing)
862+
fmap B.bulletList . fmap compactify' . sequence
863+
<$> many1 (listItem (bulletListStart' $ Just n))
862864

863865
orderedList :: OrgParser (F Blocks)
864866
orderedList = fmap B.orderedList . fmap compactify' . sequence
@@ -870,10 +872,27 @@ genericListStart listMarker = try $
870872
(+) <$> (length <$> many spaceChar)
871873
<*> (length <$> listMarker <* many1 spaceChar)
872874

873-
-- parses bullet list start and returns its length (excl. following whitespace)
875+
-- parses bullet list marker. maybe we know the indent level
874876
bulletListStart :: OrgParser Int
875-
bulletListStart = genericListStart bulletListMarker
876-
where bulletListMarker = pure <$> oneOf "*-+"
877+
bulletListStart = bulletListStart' Nothing
878+
879+
bulletListStart' :: Maybe Int -> OrgParser Int
880+
-- returns length of bulletList prefix, inclusive of marker
881+
bulletListStart' Nothing = do ind <- many spaceChar
882+
oneOf bullets
883+
many1 spaceChar
884+
return $ length ind + 1
885+
-- Unindented lists are legal, but they can't use '*' bullets
886+
-- We return n to maintain compatibility with the generic listItem
887+
bulletListStart' (Just n) = do count (n-1) spaceChar
888+
oneOf validBullets
889+
many1 spaceChar
890+
return n
891+
where validBullets = if n == 1 then noAsterisks else bullets
892+
noAsterisks = filter (/= '*') bullets
893+
894+
bullets :: String
895+
bullets = "*+-"
877896

878897
orderedListStart :: OrgParser Int
879898
orderedListStart = genericListStart orderedListMarker

tests/Tests/Readers/Org.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,33 @@ tests =
642642
]
643643
]
644644

645+
, "Bullet List with Decreasing Indent" =:
646+
(" - Discovery\n\
647+
\ - Human After All\n") =?>
648+
mconcat [ bulletList [ plain "Discovery" ]
649+
, bulletList [ plain ("Human" <> space <> "After" <> space <> "All")]
650+
]
651+
652+
, "Header follows Bullet List" =:
653+
(" - Discovery\n\
654+
\ - Human After All\n\
655+
\* Homework") =?>
656+
mconcat [ bulletList [ plain "Discovery"
657+
, plain ("Human" <> space <> "After" <> space <> "All")
658+
]
659+
, header 1 "Homework"
660+
]
661+
662+
, "Bullet List Unindented with trailing Header" =:
663+
("- Discovery\n\
664+
\- Homework\n\
665+
\* NotValidListItem") =?>
666+
mconcat [ bulletList [ plain "Discovery"
667+
, plain "Homework"
668+
]
669+
, header 1 "NotValidListItem"
670+
]
671+
645672
, "Simple Ordered List" =:
646673
("1. Item1\n" ++
647674
"2. Item2\n") =?>
@@ -737,6 +764,16 @@ tests =
737764
, ("PCR", [ plain $ spcSep [ "polymerase", "chain", "reaction" ] ])
738765
]
739766

767+
, "Definition List With Trailing Header" =:
768+
"- definition :: list\n\
769+
\- cool :: defs\n\
770+
\* header" =?>
771+
mconcat [ definitionList [ ("definition", [plain "list"])
772+
, ("cool", [plain "defs"])
773+
]
774+
, header 1 "header"
775+
]
776+
740777
, "Loose bullet list" =:
741778
unlines [ "- apple"
742779
, ""

0 commit comments

Comments
 (0)