git.iko.soy/typeable/pandoc
on master help sign in
Muse reader: make code blocks round trip
commit00004f042c7c49197d57968cae23785ffcba5c63 authorAlexander Krotov <ilabdsf@gmail.com> dateMon 27 Nov 2017, 04:51 (9 years ago) parentbdad8c1
2 files changed+41−13
src/Text/Pandoc/Readers/Muse.hs+16−8 test/Tests/Readers/Muse.hs+25−5
src/Text/Pandoc/Readers/Muse.hs+16−8
@@ -129,6 +129,13 @@ parseHtmlContentWithAttrs tag parser = do
129129parseHtmlContent :: PandocMonad m => String -> MuseParser m a -> MuseParser m [a]
130130parseHtmlContent tag p = fmap snd (parseHtmlContentWithAttrs tag p)
131131
132+commonPrefix :: String -> String -> String
133+commonPrefix _ [] = []
134+commonPrefix [] _ = []
135+commonPrefix (x:xs) (y:ys)
136+ | x == y = x : commonPrefix xs ys
137+ | otherwise = []
138+
132139--
133140-- directive parsers
134141--
@@ -365,7 +372,7 @@ lineBlock = try $ do
365372listLine :: PandocMonad m => Int -> MuseParser m String
366373listLine markerLength = try $ do
367374 indentWith markerLength
368− anyLineNewline
375+ manyTill anyChar eol
369376
370377withListContext :: PandocMonad m => MuseParser m a -> MuseParser m a
371378withListContext p = do
@@ -379,7 +386,7 @@ withListContext p = do
379386listContinuation :: PandocMonad m => Int -> MuseParser m [String]
380387listContinuation markerLength = try $ do
381388 result <- many1 $ listLine markerLength
382− blank <- option id ((++ ["\n"]) <$ blankline)
389+ blank <- option id ((++ [""]) <$ blankline)
383390 return $ blank result
384391
385392listStart :: PandocMonad m => MuseParser m Int -> MuseParser m Int
@@ -394,17 +401,18 @@ listStart marker = try $ do
394401dropSpacePrefix :: [String] -> [String]
395402dropSpacePrefix lns =
396403 map (drop maxIndent) lns
397− where maxIndent = minimum $ map (length . takeWhile (== ' ')) lns
404+ where flns = filter (\s -> not $ all (== ' ') s) lns
405+ maxIndent = if null flns then 0 else length $ takeWhile (== ' ') $ foldl1 commonPrefix flns
398406
399407listItemContents :: PandocMonad m => Int -> MuseParser m (F Blocks)
400408listItemContents markerLength = do
401− firstLine <- anyLineNewline
409+ firstLine <- manyTill anyChar eol
402410 restLines <- many $ listLine markerLength
403− blank <- option id ((++ ["\n"]) <$ blankline)
411+ blank <- option id ((++ [""]) <$ blankline)
404412 let first = firstLine : blank restLines
405413 rest <- many $ listContinuation markerLength
406414 let allLines = concat (first : rest)
407− parseFromString (withListContext parseBlocks) $ concat (dropSpacePrefix allLines) ++ "\n"
415+ parseFromString (withListContext parseBlocks) $ unlines (dropSpacePrefix allLines)
408416
409417listItem :: PandocMonad m => MuseParser m Int -> MuseParser m (F Blocks)
410418listItem start = try $ do
@@ -444,8 +452,8 @@ definitionListItem = try $ do
444452 string "::"
445453 firstLine <- manyTill anyChar eol
446454 restLines <- manyTill anyLine endOfListItemElement
447− let lns = (dropWhile (== ' ') firstLine) : dropSpacePrefix restLines
448− lineContent <- parseFromString (withListContext parseBlocks) $ unlines lns ++ "\n"
455+ let lns = dropWhile (== ' ') firstLine : dropSpacePrefix restLines
456+ lineContent <- parseFromString (withListContext parseBlocks) $ unlines lns
449457 pure $ do lineContent' <- lineContent
450458 term' <- term
451459 pure (term', [lineContent'])
test/Tests/Readers/Muse.hs+25−5
@@ -32,16 +32,12 @@ removeTables :: Block -> Block
3232removeTables (Table{}) = Para [Str "table was here"]
3333removeTables x = x
3434
35−removeCodeBlocks :: Block -> Block
36−removeCodeBlocks (CodeBlock{}) = Para [Str "table was here"]
37−removeCodeBlocks x = x
38−
3935-- Demand that any AST produced by Muse reader and written by Muse writer can be read back exactly the same way.
4036-- Currently we remove code blocks and tables and compare third rewrite to the second.
4137-- First and second rewrites are not equal yet.
4238roundTrip :: Block -> Bool
4339roundTrip b = d'' == d'''
44− where d = walk (removeCodeBlocks . removeTables) $ Pandoc nullMeta [b]
40+ where d = walk removeTables $ Pandoc nullMeta [b]
4541 d' = rewrite d
4642 d'' = rewrite d'
4743 d''' = rewrite d''
@@ -348,6 +344,18 @@ tests =
348344 , " </example>"
349345 ] =?>
350346 bulletList [ codeBlock "foo" ]
347+ , "Example inside list with empty lines" =:
348+ T.unlines [ " - <example>"
349+ , " foo"
350+ , " </example>"
351+ , ""
352+ , " bar"
353+ , ""
354+ , " <example>"
355+ , " baz"
356+ , " </example>"
357+ ] =?>
358+ bulletList [ codeBlock "foo" <> para "bar" <> codeBlock "baz" ]
351359 , "Indented example inside list" =:
352360 T.unlines [ " - <example>"
353361 , " foo"
@@ -360,6 +368,18 @@ tests =
360368 , " </example>"
361369 ] =?>
362370 definitionList [ ("foo", [codeBlock "bar"]) ]
371+ , "Example inside list definition with empty lines" =:
372+ T.unlines [ " term :: <example>"
373+ , " foo"
374+ , " </example>"
375+ , ""
376+ , " bar"
377+ , ""
378+ , " <example>"
379+ , " baz"
380+ , " </example>"
381+ ] =?>
382+ definitionList [ ("term", [codeBlock "foo" <> para "bar" <> codeBlock "baz"]) ]
363383 ]
364384 , testGroup "Literal blocks"
365385 [ test emacsMuse "Literal block"