git.iko.soy/typeable/pandoc
on master help sign in
Org reader: allow empty list items

Fixes: #4090

commitcd85c73ded2b100d33d3c1d36eac182bdd593b2f authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 22 Nov 2017, 22:17 (9 years ago) parent75e2a11
3 files changed+54−45
src/Text/Pandoc/Readers/Org/BlockStarts.hs+15−11 src/Text/Pandoc/Readers/Org/Blocks.hs+27−34 test/Tests/Readers/Org.hs+12
src/Text/Pandoc/Readers/Org/BlockStarts.hs+15−11
@@ -75,21 +75,25 @@ latexEnvStart = try $
7575 latexEnvName :: Monad m => OrgParser m String
7676 latexEnvName = try $ mappend <$> many1 alphaNum <*> option "" (string "*")
7777
78−
79−-- | Parses bullet list marker.
80−bulletListStart :: Monad m => OrgParser m ()
81−bulletListStart = try $
82− choice
83− [ () <$ skipSpaces <* oneOf "+-" <* skipSpaces1
84− , () <$ skipSpaces1 <* char '*' <* skipSpaces1
85− ]
78+bulletListStart :: Monad m => OrgParser m Int
79+bulletListStart = try $ do
80+ ind <- length <$> many spaceChar
81+ -- Unindented lists cannot use '*' bullets.
82+ oneOf (if ind == 0 then "+-" else "*+-")
83+ skipSpaces1 <|> lookAhead eol
84+ return (ind + 1)
8685
8786genericListStart :: Monad m
8887 => OrgParser m String
8988 -> OrgParser m Int
90−genericListStart listMarker = try $
91− (+) <$> (length <$> many spaceChar)
92− <*> (length <$> listMarker <* many1 spaceChar)
89+genericListStart listMarker = try $ do
90+ ind <- length <$> many spaceChar
91+ void listMarker
92+ skipSpaces1 <|> lookAhead eol
93+ return (ind + 1)
94+
95+eol :: Monad m => OrgParser m ()
96+eol = void (char '\n')
9397
9498orderedListStart :: Monad m => OrgParser m Int
9599orderedListStart = genericListStart orderedListMarker
src/Text/Pandoc/Readers/Org/Blocks.hs+27−34
@@ -744,7 +744,7 @@ paraOrPlain = try $ do
744744 -- is directly followed by a list item, in which case the block is read as
745745 -- plain text.
746746 try (guard nl
747− *> notFollowedBy (inList *> (() <$ orderedListStart <|> bulletListStart))
747+ *> notFollowedBy (inList *> (orderedListStart <|> bulletListStart))
748748 *> return (B.para <$> ils))
749749 <|> return (B.plain <$> ils)
750750
@@ -757,40 +757,34 @@ list :: PandocMonad m => OrgParser m (F Blocks)
757757list = choice [ definitionList, bulletList, orderedList ] <?> "list"
758758
759759definitionList :: PandocMonad m => OrgParser m (F Blocks)
760−definitionList = try $ do n <- lookAhead (bulletListStart' Nothing)
761− fmap (B.definitionList . compactifyDL) . sequence
762− <$> many1 (definitionListItem $ bulletListStart' (Just n))
760+definitionList = try $ do
761+ indent <- lookAhead bulletListStart
762+ fmap (B.definitionList . compactifyDL) . sequence
763+ <$> many1 (definitionListItem (bulletListStart `indented` indent))
763764
764765bulletList :: PandocMonad m => OrgParser m (F Blocks)
765−bulletList = try $ do n <- lookAhead (bulletListStart' Nothing)
766− fmap (B.bulletList . compactify) . sequence
767− <$> many1 (listItem (bulletListStart' $ Just n))
766+bulletList = try $ do
767+ indent <- lookAhead bulletListStart
768+ fmap (B.bulletList . compactify) . sequence
769+ <$> many1 (listItem (bulletListStart `indented` indent))
770+
771+indented :: Monad m => OrgParser m Int -> Int -> OrgParser m Int
772+indented indentedMarker minIndent = try $ do
773+ n <- indentedMarker
774+ guard (minIndent <= n)
775+ return n
768776
769777orderedList :: PandocMonad m => OrgParser m (F Blocks)
770−orderedList = fmap (B.orderedList . compactify) . sequence
771− <$> many1 (listItem orderedListStart)
772−
773−bulletListStart' :: Monad m => Maybe Int -> OrgParser m Int
774−-- returns length of bulletList prefix, inclusive of marker
775−bulletListStart' Nothing = do ind <- length <$> many spaceChar
776− oneOf (bullets $ ind == 0)
777− skipSpaces1
778− return (ind + 1)
779−bulletListStart' (Just n) = do count (n-1) spaceChar
780− oneOf (bullets $ n == 1)
781− many1 spaceChar
782− return n
783−
784−-- Unindented lists are legal, but they can't use '*' bullets.
785−-- We return n to maintain compatibility with the generic listItem.
786−bullets :: Bool -> String
787−bullets unindented = if unindented then "+-" else "*+-"
778+orderedList = try $ do
779+ indent <- lookAhead orderedListStart
780+ fmap (B.orderedList . compactify) . sequence
781+ <$> many1 (listItem (orderedListStart `indented` indent))
788782
789783definitionListItem :: PandocMonad m
790784 => OrgParser m Int
791785 -> OrgParser m (F (Inlines, [Blocks]))
792−definitionListItem parseMarkerGetLength = try $ do
793− markerLength <- parseMarkerGetLength
786+definitionListItem parseIndentedMarker = try $ do
787+ markerLength <- parseIndentedMarker
794788 term <- manyTill (noneOf "\n\r") (try definitionMarker)
795789 line1 <- anyLineNewline
796790 blank <- option "" ("\n" <$ blankline)
@@ -802,13 +796,12 @@ definitionListItem parseMarkerGetLength = try $ do
802796 definitionMarker =
803797 spaceChar *> string "::" <* (spaceChar <|> lookAhead newline)
804798
805−
806−-- parse raw text for one list item, excluding start marker and continuations
799+-- | parse raw text for one list item
807800listItem :: PandocMonad m
808801 => OrgParser m Int
809802 -> OrgParser m (F Blocks)
810−listItem start = try . withContext ListItemState $ do
811− markerLength <- try start
803+listItem parseIndentedMarker = try . withContext ListItemState $ do
804+ markerLength <- try parseIndentedMarker
812805 firstLine <- anyLineNewline
813806 blank <- option "" ("\n" <$ blankline)
814807 rest <- concat <$> many (listContinuation markerLength)
@@ -818,9 +811,9 @@ listItem start = try . withContext ListItemState $ do
818811-- Note: nested lists are parsed as continuations.
819812listContinuation :: Monad m => Int
820813 -> OrgParser m String
821−listContinuation markerLength = try $
814+listContinuation markerLength = try $ do
822815 notFollowedBy' blankline
823− *> (mappend <$> (concat <$> many1 listLine)
824− <*> many blankline)
816+ mappend <$> (concat <$> many1 listLine)
817+ <*> many blankline
825818 where
826819 listLine = try $ indentWith markerLength *> anyLineNewline
test/Tests/Readers/Org.hs+12
@@ -1262,6 +1262,12 @@ tests =
12621262 , headerWith ("notvalidlistitem", [], []) 1 "NotValidListItem"
12631263 ]
12641264
1265+ , "Empty bullet points" =:
1266+ T.unlines [ "-"
1267+ , "- "
1268+ ] =?>
1269+ bulletList [ plain "", plain "" ]
1270+
12651271 , "Simple Ordered List" =:
12661272 ("1. Item1\n" <>
12671273 "2. Item2\n") =?>
@@ -1289,6 +1295,12 @@ tests =
12891295 ]
12901296 in orderedListWith listStyle listStructure
12911297
1298+ , "Empty ordered list item" =:
1299+ T.unlines [ "1."
1300+ , "3. "
1301+ ] =?>
1302+ orderedList [ plain "", plain "" ]
1303+
12921304 , "Nested Ordered Lists" =:
12931305 ("1. One\n" <>
12941306 " 1. One-One\n" <>