git.iko.soy/typeable/pandoc
on master help sign in
Muse reader: parse definition lists with multiple descriptions
42e39fbAlexander Krotov
src/Text/Pandoc/Readers/Muse.hs+25−14
@@ -631,26 +631,37 @@ orderedListUntil end = try $ do
631 (items, e) <- orderedListItemsUntil indent style end
631 (items, e) <- orderedListItemsUntil indent style end
632 return $ (B.orderedListWith p <$> sequence items, e)
632 return $ (B.orderedListWith p <$> sequence items, e)
633
633
634+descriptionsUntil :: PandocMonad m
635+ => Int
636+ -> MuseParser m a
637+ -> MuseParser m ([F Blocks], a)
638+descriptionsUntil indent end = do
639+ void spaceChar <|> lookAhead eol
640+ st <- getState
641+ setState $ st{ museInPara = False }
642+ (x, e) <- listItemContentsUntil indent (Right <$> try (optional blankline >> indentWith indent >> manyTill spaceChar (string "::") >> descriptionsUntil indent end)) (Left <$> end)
643+ case e of
644+ Right (xs, ee) -> return (x:xs, ee)
645+ Left ee -> return ([x], ee)
646+
634definitionListItemsUntil :: PandocMonad m
647definitionListItemsUntil :: PandocMonad m
635 => Int
648 => Int
636 -> MuseParser m a
649 -> MuseParser m a
637 -> MuseParser m ([F (Inlines, [Blocks])], a)
650 -> MuseParser m ([F (Inlines, [Blocks])], a)
638definitionListItemsUntil indent end =
651definitionListItemsUntil indent end =
639 continuation
652 continuation
653+ where
640− where continuation = try $ do
654+ continuation = try $ do
641− pos <- getPosition
655+ pos <- getPosition
642− term <- trimInlinesF . mconcat <$> manyTill (choice inlineList) (string "::")
656+ term <- trimInlinesF . mconcat <$> manyTill (choice inlineList) (string "::")
643− void spaceChar <|> lookAhead eol
644− st <- getState
645− setState $ st{ museInPara = False }
646− (x, e) <- listItemContentsUntil (sourceColumn pos) ((Right <$> try (optional blankline >> count indent spaceChar >> continuation)) <|> (Left <$> end))
657+ (x, e) <- descriptionsUntil (sourceColumn pos) ((Right <$> try (optional blankline >> indentWith indent >> continuation)) <|> (Left <$> end))
647− let xx = do
658+ let xx = do
648− term' <- term
659+ term' <- term
649− x' <- x
660+ x' <- sequence x
650− (return (term', [x']))::(F (Inlines, [Blocks]))
661+ return (term', x')
651− case e of
662+ case e of
652− Left ee -> return $ ([xx], ee)
663+ Left ee -> return ([xx], ee)
653− Right (xs, ee) -> return $ (xx : xs, ee)
664+ Right (xs, ee) -> return (xx:xs, ee)
654
665
655definitionListUntil :: PandocMonad m
666definitionListUntil :: PandocMonad m
656 => MuseParser m a
667 => MuseParser m a
test/Tests/Readers/Muse.hs+20−7
@@ -28,15 +28,9 @@ spcSep :: [Inlines] -> Inlines
28spcSep = mconcat . intersperse space
28spcSep = mconcat . intersperse space
29
29
30-- Tables don't round-trip yet
30-- Tables don't round-trip yet
31−-- Definition lists with multiple descriptions are supported by writer, but not reader yet
31+--
32−
33−singleDescription :: ([Inline], [[Block]]) -> ([Inline], [[Block]])
34−singleDescription (a, x:_) = (a, [x])
35−singleDescription x = x
36−
37makeRoundTrip :: Block -> Block
32makeRoundTrip :: Block -> Block
38makeRoundTrip Table{} = Para [Str "table was here"]
33makeRoundTrip Table{} = Para [Str "table was here"]
39−makeRoundTrip (DefinitionList items) = DefinitionList $ map singleDescription items
40makeRoundTrip x = x
34makeRoundTrip x = x
41
35
42-- Demand that any AST produced by Muse reader and written by Muse writer can be read back exactly the same way.
36-- Demand that any AST produced by Muse reader and written by Muse writer can be read back exactly the same way.
@@ -1092,6 +1086,25 @@ tests =
1092 ("Fourth", [ definitionList [ ("Fifth", [ para "Sixth"] ) ] ] ) ] ] )
1086 ("Fourth", [ definitionList [ ("Fifth", [ para "Sixth"] ) ] ] ) ] ] )
1093 , ("Seventh", [ para "Eighth" ])
1087 , ("Seventh", [ para "Eighth" ])
1094 ]
1088 ]
1089+ , testGroup "Definition lists with multiple descriptions"
1090+ [ "Correctly indented second description" =:
1091+ T.unlines
1092+ [ " First term :: first description"
1093+ , " :: second description"
1094+ ] =?>
1095+ definitionList [ ("First term", [ para "first description"
1096+ , para "second description"
1097+ ])
1098+ ]
1099+ , "Incorrectly indented second description" =:
1100+ T.unlines
1101+ [ " First term :: first description"
1102+ , " :: second description"
1103+ ] =?>
1104+ definitionList [ ("First term", [ para "first description" ])
1105+ , ("", [ para "second description" ])
1106+ ]
1107+ ]
1095 , "Two blank lines separate definition lists" =:
1108 , "Two blank lines separate definition lists" =:
1096 T.unlines
1109 T.unlines
1097 [ " First :: list"
1110 [ " First :: list"