git.iko.soy/typeable/pandoc
on master help sign in
Muse reader: concatenate inlines of the same type
commit351765d4ad4e7bfa674fa48cb36dee824efc98ea authorAlexander Krotov <ilabdsf@gmail.com> dateTue 21 Nov 2017, 23:46 (9 years ago) parentdf3a80c
2 files changed+20−4
src/Text/Pandoc/Readers/Muse.hs+18−4 test/Tests/Readers/Muse.hs+2
src/Text/Pandoc/Readers/Muse.hs+18−4
@@ -139,6 +139,20 @@ parseHtmlContentWithAttrs tag parser = do
139139parseHtmlContent :: PandocMonad m => String -> MuseParser m a -> MuseParser m [a]
140140parseHtmlContent tag p = fmap snd (parseHtmlContentWithAttrs tag p)
141141
142+normalizeInlineList :: [Inline] -> [Inline]
143+normalizeInlineList (Code a1 x1 : Code a2 x2 : ils) | a1 == a2
144+ = normalizeInlineList $ Code a1 (x1 ++ x2) : ils
145+normalizeInlineList (RawInline f1 x1 : RawInline f2 x2 : ils) | f1 == f2
146+ = normalizeInlineList $ RawInline f1 (x1 ++ x2) : ils
147+normalizeInlineList (x:xs) = x : normalizeInlineList xs
148+normalizeInlineList [] = []
149+
150+normalizeInlines :: Inlines -> Inlines
151+normalizeInlines = B.fromList . normalizeInlineList . B.toList . B.trimInlines
152+
153+normalizeInlinesF :: Future s Inlines -> Future s Inlines
154+normalizeInlinesF = liftM normalizeInlines
155+
142156--
143157-- directive parsers
144158--
@@ -150,7 +164,7 @@ parseDirective = do
150164 space
151165 spaces
152166 raw <- manyTill anyChar eol
153− value <- parseFromString (trimInlinesF . mconcat <$> many inline) raw
167+ value <- parseFromString (normalizeInlinesF . mconcat <$> many inline) raw
154168 return (key, value)
155169
156170directive :: PandocMonad m => MuseParser m ()
@@ -217,7 +231,7 @@ header = try $ do
217231 level <- fmap length $ many1 $ char '*'
218232 guard $ level <= 5
219233 spaceChar
220− content <- trimInlinesF . mconcat <$> manyTill inline eol
234+ content <- normalizeInlinesF . mconcat <$> manyTill inline eol
221235 attr <- registerHeader ("", [], []) (runF content defaultParserState)
222236 return $ B.headerWith attr level <$> content
223237
@@ -286,7 +300,7 @@ verseLines :: PandocMonad m => MuseParser m (F Blocks)
286300verseLines = do
287301 optionMaybe blankline -- Skip blankline after opening tag on separate line
288302 lns <- many verseLine
289− lns' <- mapM (parseFromString' (trimInlinesF . mconcat <$> many inline)) lns
303+ lns' <- mapM (parseFromString' (normalizeInlinesF . mconcat <$> many inline)) lns
290304 return $ B.lineBlock <$> sequence lns'
291305
292306verseTag :: PandocMonad m => MuseParser m (F Blocks)
@@ -302,7 +316,7 @@ para :: PandocMonad m => MuseParser m (F Blocks)
302316para = do
303317 indent <- length <$> many spaceChar
304318 let f = if indent >= 2 && indent < 6 then B.blockQuote else id
305− fmap (f . B.para) . trimInlinesF . mconcat <$> many1Till inline endOfParaElement
319+ fmap (f . B.para) . normalizeInlinesF . mconcat <$> many1Till inline endOfParaElement
306320 where
307321 endOfParaElement = lookAhead $ endOfInput <|> endOfPara <|> newBlockElement
308322 endOfInput = try $ skipMany blankline >> skipSpaces >> eof
test/Tests/Readers/Muse.hs+2
@@ -125,6 +125,8 @@ tests =
125125
126126 , "Verbatim inside code" =: "<code><verbatim>foo</verbatim></code>" =?> para (code "<verbatim>foo</verbatim>")
127127
128+ , "Code normalization" =: "<code><code><</code><code>/code></code>" =?> para (code "<code></code>")
129+
128130 , testGroup "Links"
129131 [ "Link without description" =:
130132 "[[https://amusewiki.org/]]" =?>