git.iko.soy/typeable/pandoc
on master help sign in
Muse: move inline list normalization to writer
0cfd764Alexander Krotov
src/Text/Pandoc/Readers/Muse.hs+4−18
@@ -139,20 +139,6 @@ parseHtmlContentWithAttrs tag parser = do
139parseHtmlContent :: PandocMonad m => String -> MuseParser m a -> MuseParser m [a]
139parseHtmlContent :: PandocMonad m => String -> MuseParser m a -> MuseParser m [a]
140parseHtmlContent tag p = fmap snd (parseHtmlContentWithAttrs tag p)
140parseHtmlContent tag p = fmap snd (parseHtmlContentWithAttrs tag p)
141
141
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−
156--
142--
157-- directive parsers
143-- directive parsers
158--
144--
@@ -164,7 +150,7 @@ parseDirective = do
164 space
150 space
165 spaces
151 spaces
166 raw <- manyTill anyChar eol
152 raw <- manyTill anyChar eol
167− value <- parseFromString (normalizeInlinesF . mconcat <$> many inline) raw
153+ value <- parseFromString (trimInlinesF . mconcat <$> many inline) raw
168 return (key, value)
154 return (key, value)
169
155
170directive :: PandocMonad m => MuseParser m ()
156directive :: PandocMonad m => MuseParser m ()
@@ -231,7 +217,7 @@ header = try $ do
231 level <- fmap length $ many1 $ char '*'
217 level <- fmap length $ many1 $ char '*'
232 guard $ level <= 5
218 guard $ level <= 5
233 spaceChar
219 spaceChar
234− content <- normalizeInlinesF . mconcat <$> manyTill inline eol
220+ content <- trimInlinesF . mconcat <$> manyTill inline eol
235 attr <- registerHeader ("", [], []) (runF content defaultParserState)
221 attr <- registerHeader ("", [], []) (runF content defaultParserState)
236 return $ B.headerWith attr level <$> content
222 return $ B.headerWith attr level <$> content
237
223
@@ -300,7 +286,7 @@ verseLines :: PandocMonad m => MuseParser m (F Blocks)
300verseLines = do
286verseLines = do
301 optionMaybe blankline -- Skip blankline after opening tag on separate line
287 optionMaybe blankline -- Skip blankline after opening tag on separate line
302 lns <- many verseLine
288 lns <- many verseLine
303− lns' <- mapM (parseFromString' (normalizeInlinesF . mconcat <$> many inline)) lns
289+ lns' <- mapM (parseFromString' (trimInlinesF . mconcat <$> many inline)) lns
304 return $ B.lineBlock <$> sequence lns'
290 return $ B.lineBlock <$> sequence lns'
305
291
306verseTag :: PandocMonad m => MuseParser m (F Blocks)
292verseTag :: PandocMonad m => MuseParser m (F Blocks)
@@ -317,7 +303,7 @@ para = do
317 indent <- length <$> many spaceChar
303 indent <- length <$> many spaceChar
318 st <- stateParserContext <$> getState
304 st <- stateParserContext <$> getState
319 let f = if st /= ListItemState && indent >= 2 && indent < 6 then B.blockQuote else id
305 let f = if st /= ListItemState && indent >= 2 && indent < 6 then B.blockQuote else id
320− fmap (f . B.para) . normalizeInlinesF . mconcat <$> many1Till inline endOfParaElement
306+ fmap (f . B.para) . trimInlinesF . mconcat <$> many1Till inline endOfParaElement
321 where
307 where
322 endOfParaElement = lookAhead $ endOfInput <|> endOfPara <|> newBlockElement
308 endOfParaElement = lookAhead $ endOfInput <|> endOfPara <|> newBlockElement
323 endOfInput = try $ skipMany blankline >> skipSpaces >> eof
309 endOfInput = try $ skipMany blankline >> skipSpaces >> eof
src/Text/Pandoc/Writers/Muse.hs+9−1
@@ -291,11 +291,19 @@ conditionalEscapeString s =
291 then escapeString s
291 then escapeString s
292 else s
292 else s
293
293
294+normalizeInlineList :: [Inline] -> [Inline]
295+normalizeInlineList (Code a1 x1 : Code a2 x2 : ils) | a1 == a2
296+ = normalizeInlineList $ Code a1 (x1 ++ x2) : ils
297+normalizeInlineList (RawInline f1 x1 : RawInline f2 x2 : ils) | f1 == f2
298+ = normalizeInlineList $ RawInline f1 (x1 ++ x2) : ils
299+normalizeInlineList (x:xs) = x : normalizeInlineList xs
300+normalizeInlineList [] = []
301+
294-- | Convert list of Pandoc inline elements to Muse.
302-- | Convert list of Pandoc inline elements to Muse.
295inlineListToMuse :: PandocMonad m
303inlineListToMuse :: PandocMonad m
296 => [Inline]
304 => [Inline]
297 -> StateT WriterState m Doc
305 -> StateT WriterState m Doc
298−inlineListToMuse lst = liftM hcat (mapM inlineToMuse lst)
306+inlineListToMuse lst = liftM hcat (mapM inlineToMuse (normalizeInlineList lst))
299
307
300-- | Convert Pandoc inline element to Muse.
308-- | Convert Pandoc inline element to Muse.
301inlineToMuse :: PandocMonad m
309inlineToMuse :: PandocMonad m
test/Tests/Readers/Muse.hs−2
@@ -125,8 +125,6 @@ tests =
125
125
126 , "Verbatim inside code" =: "<code><verbatim>foo</verbatim></code>" =?> para (code "<verbatim>foo</verbatim>")
126 , "Verbatim inside code" =: "<code><verbatim>foo</verbatim></code>" =?> para (code "<verbatim>foo</verbatim>")
127
127
128− , "Code normalization" =: "<code><code><</code><code>/code></code>" =?> para (code "<code></code>")
129−
130 , testGroup "Links"
128 , testGroup "Links"
131 [ "Link without description" =:
129 [ "Link without description" =:
132 "[[https://amusewiki.org/]]" =?>
130 "[[https://amusewiki.org/]]" =?>
test/Tests/Writers/Muse.hs+1
@@ -300,6 +300,7 @@ tests = [ testGroup "block elements"
300 , testGroup "code"
300 , testGroup "code"
301 [ "simple" =: code "foo" =?> "<code>foo</code>"
301 [ "simple" =: code "foo" =?> "<code>foo</code>"
302 , "escape tag" =: code "<code>foo = bar</code> baz" =?> "<code><code>foo = bar<</code><code>/code> baz</code>"
302 , "escape tag" =: code "<code>foo = bar</code> baz" =?> "<code><code>foo = bar<</code><code>/code> baz</code>"
303+ , "normalization" =: code "</co" <> code "de>" =?> "<code><</code><code>/code></code>"
303 ]
304 ]
304 , testGroup "spaces"
305 , testGroup "spaces"
305 [ "space" =: text "a" <> space <> text "b" =?> "a b"
306 [ "space" =: text "a" <> space <> text "b" =?> "a b"