Skip to content

Commit

Permalink
Muse reader: parse bullet lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krotov committed May 29, 2017
1 parent e88b895 commit 070f33c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Text/Pandoc/Readers/Muse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ blockElements = choice [ comment
, centerTag
, rightTag
, quoteTag
, bulletList
, table
, commentTag
, noteBlock
Expand Down Expand Up @@ -274,6 +275,59 @@ noteBlock = try $ do
blocksTillNote =
many1Till block (eof <|> () <$ lookAhead noteMarker)

--
-- lists
--

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
preWhitespace <- length <$> many spaceChar
st <- stateParserContext <$> getState
getPosition >>= \pos -> guard (st == ListItemState || sourceColumn pos /= 1)
char '-'
postWhitespace <- length <$> many1 spaceChar
return $ preWhitespace + 1 + postWhitespace

listLine :: PandocMonad m => Int -> MuseParser m [Char]
listLine markerLength = try $ do
notFollowedBy blankline
indentWith markerLength
anyLineNewline

withListContext :: PandocMonad m => MuseParser m a -> MuseParser m a
withListContext p = do
state <- getState
let oldContext = stateParserContext state
setState $ state { stateParserContext = ListItemState }
parsed <- p
updateState (\st -> st {stateParserContext = oldContext})
return parsed

bulletListItem :: PandocMonad m => MuseParser m (F Blocks)
bulletListItem = try $ do
markerLength <- bulletListStart
firstLine <- anyLineNewline
blank <- option "" ("\n" <$ blankline)
restLines <- many $ listLine markerLength
let first = firstLine ++ blank ++ concat restLines
rest <- many $ listContinuation markerLength
parsed <- parseFromString (withListContext parseBlocks) $ concat (first:rest) ++ "\n"
return parsed

listContinuation :: PandocMonad m => Int -> MuseParser m String
listContinuation markerLength = try $ do
result <- many1 $ listLine markerLength
blanks <- many1 blankline
return $ concat result ++ blanks

--
-- tables
--
Expand Down
25 changes: 25 additions & 0 deletions test/Tests/Readers/Muse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,29 @@ tests =
[plain "", plain ""],
[plain "bar", plain ""]]
]
, testGroup "Lists" $
[ "Bullet list" =:
unlines
[ " - Item1"
, ""
, " - Item2"
] =?>
bulletList [ para "Item1"
, para "Item2"
]
, "Nested list" =:
unlines
[ " - Item1"
, " - Item2"
, " - Item3"
, " - Item4"
] =?>
bulletList [ mconcat [ para "Item1"
, bulletList [ para "Item2"
, para "Item3"
]
]
, para "Item4"
]
]
]

0 comments on commit 070f33c

Please sign in to comment.