From a20fc383e5687bdbcd7d860e469bced4212dd924 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 18 Jun 2017 03:09:38 +0300 Subject: [PATCH] Muse reader: parse ordered lists --- src/Text/Pandoc/Readers/Muse.hs | 49 ++++++++++++++++++++++----------- test/Tests/Readers/Muse.hs | 19 ++++++++++++- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/Text/Pandoc/Readers/Muse.hs b/src/Text/Pandoc/Readers/Muse.hs index bed4342865c7a..bc9da26cb0784 100644 --- a/src/Text/Pandoc/Readers/Muse.hs +++ b/src/Text/Pandoc/Readers/Muse.hs @@ -33,7 +33,6 @@ TODO: - Page breaks (five "*") - Headings with anchors (make it round trip with Muse writer) - and ">" -- Ordered lists - Definition lists - Org tables - table.el tables @@ -185,6 +184,7 @@ blockElements = choice [ comment , rightTag , quoteTag , bulletList + , orderedList , table , commentTag , noteBlock @@ -299,26 +299,18 @@ listContinuation markerLength = try $ do blanks <- many1 blankline return $ concat result ++ blanks -bulletListItems :: PandocMonad m => MuseParser m (F [Blocks]) -bulletListItems = sequence <$> many1 bulletListItem - -bulletList :: PandocMonad m => MuseParser m (F Blocks) -bulletList = do - listItems <- bulletListItems - return $ B.bulletList <$> listItems - -bulletListStart :: PandocMonad m => MuseParser m Int -bulletListStart = try $ do +listStart :: PandocMonad m => MuseParser m Int -> MuseParser m Int +listStart marker = try $ do preWhitespace <- length <$> many spaceChar st <- stateParserContext <$> getState getPosition >>= \pos -> guard (st == ListItemState || sourceColumn pos /= 1) - char '-' + markerLength <- marker postWhitespace <- length <$> many1 spaceChar - return $ preWhitespace + 1 + postWhitespace + return $ preWhitespace + markerLength + postWhitespace -bulletListItem :: PandocMonad m => MuseParser m (F Blocks) -bulletListItem = try $ do - markerLength <- bulletListStart +listItem :: PandocMonad m => MuseParser m Int -> MuseParser m (F Blocks) +listItem start = try $ do + markerLength <- start firstLine <- anyLineNewline blank <- option "" ("\n" <$ blankline) restLines <- many $ listLine markerLength @@ -326,6 +318,31 @@ bulletListItem = try $ do rest <- many $ listContinuation markerLength parseFromString (withListContext parseBlocks) $ concat (first:rest) ++ "\n" +bulletListItems :: PandocMonad m => MuseParser m (F [Blocks]) +bulletListItems = sequence <$> many1 (listItem bulletListStart) + +bulletListStart :: PandocMonad m => MuseParser m Int +bulletListStart = listStart (char '-' >> return 1) + +bulletList :: PandocMonad m => MuseParser m (F Blocks) +bulletList = do + listItems <- bulletListItems + return $ B.bulletList <$> listItems + +orderedListStart :: PandocMonad m + => ListNumberStyle + -> ListNumberDelim + -> MuseParser m Int +orderedListStart style delim = listStart (snd <$> withHorizDisplacement (orderedListMarker style delim)) + +orderedList :: PandocMonad m => MuseParser m (F Blocks) +orderedList = try $ do + p@(_, style, delim) <- lookAhead (many spaceChar *> anyOrderedListMarker <* spaceChar) + guard $ style `elem` [Decimal, LowerAlpha, UpperAlpha, LowerRoman, UpperRoman] + guard $ delim == Period + items <- sequence <$> many1 (listItem $ orderedListStart style delim) + return $ B.orderedListWith p <$> items + -- -- tables -- diff --git a/test/Tests/Readers/Muse.hs b/test/Tests/Readers/Muse.hs index 549419622efc0..5a896da55ef01 100644 --- a/test/Tests/Readers/Muse.hs +++ b/test/Tests/Readers/Muse.hs @@ -229,19 +229,36 @@ tests = bulletList [ para "Item1" , para "Item2" ] + , "Ordered list" =: + T.unlines + [ " 1. Item1" + , "" + , " 2. Item2" + ] =?> + orderedListWith (1, Decimal, Period) [ para "Item1" + , para "Item2" + ] , "Nested list" =: T.unlines [ " - Item1" , " - Item2" , " - Item3" , " - Item4" + , " 1. Nested" + , " 2. Ordered" + , " 3. List" ] =?> bulletList [ mconcat [ para "Item1" , bulletList [ para "Item2" , para "Item3" ] ] - , para "Item4" + , mconcat [ para "Item4" + , orderedListWith (1, Decimal, Period) [ para "Nested" + , para "Ordered" + , para "List" + ] + ] ] ] ]