diff options
| author | John MacFarlane <[email protected]> | 2022-09-19 11:56:16 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2022-09-19 12:03:03 -0700 |
| commit | f3e9669e8478c19f1bd00ff788275f4fd99fc6be (patch) | |
| tree | b1660eab31f70cae26d049cb8c2116dff0d4c138 | |
| parent | cfbf0f096bcdd9c9a5bfc2b394be863b39b34523 (diff) | |
Commonmark writer: ensure that we don't have blank lines in raw HTML.
Closes #8307.
| -rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 15 | ||||
| -rw-r--r-- | test/command/8307.md | 49 |
2 files changed, 63 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index f6a207991..e5a13d611 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -2,6 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TupleSections #-} +{-# LANGUAGE BangPatterns #-} {- | Module : Text.Pandoc.Writers.Markdown Copyright : Copyright (C) 2006-2022 John MacFarlane @@ -29,6 +30,7 @@ import qualified Data.Map as M import Data.Maybe (fromMaybe, mapMaybe, isNothing) import qualified Data.Set as Set import Data.Text (Text) +import Data.Char (isSpace) import qualified Data.Text as T import Text.HTML.TagSoup (Tag (..), isTagText, parseTags) import Text.Pandoc.Class.PandocMonad (PandocMonad, report) @@ -448,6 +450,8 @@ blockToMarkdown' opts b@(RawBlock f str) = do Commonmark | f `elem` ["gfm", "commonmark", "commonmark_x", "markdown"] -> return $ literal str <> literal "\n" + | f `elem` ["html", "html5", "html4"] + -> return $ literal (removeBlankLinesInHTML str) <> literal "\n" Markdown | f `elem` ["markdown", "markdown_github", "markdown_phpextra", "markdown_mmd", "markdown_strict"] @@ -631,7 +635,7 @@ blockToMarkdown' opts t@(Table _ blkCapt specs thead tbody tfoot) = do (id,) <$> pipeTable opts (all null headers) aligns' widths' rawHeaders rawRows | isEnabled Ext_raw_html opts -> fmap (id,) $ - literal <$> + literal . removeBlankLinesInHTML <$> writeHtml5String opts{ writerTemplate = Nothing } (Pandoc nullMeta [t]) | otherwise -> return (id, literal "[TABLE]") return $ nst (tbl $$ caption'') $$ blankline @@ -822,3 +826,12 @@ lineBreakToSpace :: Inline -> Inline lineBreakToSpace LineBreak = Space lineBreakToSpace SoftBreak = Space lineBreakToSpace x = x + +removeBlankLinesInHTML :: Text -> Text +removeBlankLinesInHTML = T.pack . go False . T.unpack + where go _ [] = [] + go True ('\n':cs) = " " <> go False cs + go False ('\n':cs) = '\n' : go True cs + go !afternewline (!c:cs) + | isSpace c = c : go afternewline cs + | otherwise = c : go False cs diff --git a/test/command/8307.md b/test/command/8307.md new file mode 100644 index 000000000..d211d7003 --- /dev/null +++ b/test/command/8307.md @@ -0,0 +1,49 @@ +``` +% pandoc -t commonmark -f html +<table> +<tbody> +<tr> +<td title="this + + +breaks">hello</td> +</tr> +</tbody> +</table> +^D +<table> +<tbody> +<tr class="odd"> +<td title="this + +breaks">hello</td> +</tr> +</tbody> +</table> +``` + +```` +% pandoc -t commonmark -f markdown +``` {=html} +<table> +<tbody> +<tr> +<td title="this + + +breaks">hello</td> +</tr> +</tbody> +</table> +``` +^D +<table> +<tbody> +<tr> +<td title="this + +breaks">hello</td> +</tr> +</tbody> +</table> +```` |
