git.iko.soy/typeable/pandoc
on master help sign in
Muse reader: refactor definition list parsing
5f57094Alexander Krotov
src/Text/Pandoc/Readers/Muse.hs+15−17
@@ -422,13 +422,17 @@ withListContext p = do
422 updateState (\st -> st {stateParserContext = oldContext})
422 updateState (\st -> st {stateParserContext = oldContext})
423 return parsed
423 return parsed
424
424
425+listItemContents' :: PandocMonad m => Int -> MuseParser m (F Blocks)
426+listItemContents' col = do
427+ first <- try $ withListContext parseBlock
428+ rest <- many $ try (skipMany blankline >> indentWith col >> withListContext parseBlock)
429+ return $ mconcat (first : rest)
430+
425listItemContents :: PandocMonad m => MuseParser m (F Blocks)
431listItemContents :: PandocMonad m => MuseParser m (F Blocks)
426listItemContents = do
432listItemContents = do
427 pos <- getPosition
433 pos <- getPosition
428 let col = sourceColumn pos - 1
434 let col = sourceColumn pos - 1
429− first <- try $ withListContext parseBlock
435+ listItemContents' col
430− rest <- many $ try (skipMany blankline >> indentWith col >> withListContext parseBlock)
431− return $ mconcat (first : rest)
432
436
433listItem :: PandocMonad m => Int -> MuseParser m () -> MuseParser m (F Blocks)
437listItem :: PandocMonad m => Int -> MuseParser m () -> MuseParser m (F Blocks)
434listItem n p = try $ do
438listItem n p = try $ do
@@ -466,25 +470,19 @@ orderedList = try $ do
466
470
467definitionListItem :: PandocMonad m => MuseParser m (F (Inlines, [Blocks]))
471definitionListItem :: PandocMonad m => MuseParser m (F (Inlines, [Blocks]))
468definitionListItem = try $ do
472definitionListItem = try $ do
469− rawTerm <- termParser
473+ guardDisabled Ext_amuse <|> void spaceChar -- Initial space is required by Amusewiki, but not Emacs Muse
474+ many spaceChar
475+ pos <- getPosition
476+ rawTerm <- many1Till (noneOf "\n") (lookAhead (void (try (spaceChar >> string "::"))))
470 term <- parseFromString (trimInlinesF . mconcat <$> many inline) rawTerm
477 term <- parseFromString (trimInlinesF . mconcat <$> many inline) rawTerm
471 many1 spaceChar
478 many1 spaceChar
472 string "::"
479 string "::"
473− firstLine <- manyTill anyChar eol
480+ void spaceChar <|> lookAhead eol
474− restLines <- manyTill anyLine endOfListItemElement
481+ contents <- listItemContents' $ sourceColumn pos
475− let lns = dropWhile (== ' ') firstLine : restLines
482+ optionMaybe blankline
476− lineContent <- parseFromString (withListContext parseBlocks) $ unlines lns
477− pure $ do lineContent' <- lineContent
483+ pure $ do lineContent' <- contents
478 term' <- term
484 term' <- term
479 pure (term', [lineContent'])
485 pure (term', [lineContent'])
480− where
481− termParser = (guardDisabled Ext_amuse <|> void spaceChar) >> -- Initial space is required by Amusewiki, but not Emacs Muse
482− many spaceChar >>
483− many1Till (noneOf "\n") (lookAhead (void (try (spaceChar >> string "::"))))
484− endOfInput = lookAhead $ try $ skipMany blankline >> skipSpaces >> eof
485− twoBlankLines = try $ blankline >> skipMany1 blankline
486− newDefinitionListItem = try $ void termParser
487− endOfListItemElement = lookAhead $ endOfInput <|> newDefinitionListItem <|> twoBlankLines
488
486
489definitionListItems :: PandocMonad m => MuseParser m (F [(Inlines, [Blocks])])
487definitionListItems :: PandocMonad m => MuseParser m (F [(Inlines, [Blocks])])
490definitionListItems = sequence <$> many1 definitionListItem
488definitionListItems = sequence <$> many1 definitionListItem
test/Tests/Readers/Muse.hs−11
@@ -955,17 +955,6 @@ tests =
955 definitionList [ ("Term1", [ para "This is a first definition\nAnd it has two lines;\nno, make that three."])
955 definitionList [ ("Term1", [ para "This is a first definition\nAnd it has two lines;\nno, make that three."])
956 , ("Term2", [ para "This is a second definition"])
956 , ("Term2", [ para "This is a second definition"])
957 ])
957 ])
958− -- Emacs Muse creates two separate lists when indentation of items is different.
959− -- We follow Amusewiki and allow different indentation within one list.
960− , "Changing indentation" =:
961− T.unlines
962− [ " First term :: Definition of first term"
963− , "and its continuation."
964− , " Second term :: Definition of second term."
965− ] =?>
966− definitionList [ ("First term", [ para "Definition of first term\nand its continuation." ])
967− , ("Second term", [ para "Definition of second term." ])
968− ]
969 , "Two blank lines separate definition lists" =:
958 , "Two blank lines separate definition lists" =:
970 T.unlines
959 T.unlines
971 [ " First :: list"
960 [ " First :: list"