Markdown writer: improve handling of raw blocks/inline.

We now emit raw content using `raw_attribute` when no more
direct method is available.
This commit is contained in:
John MacFarlane 2019-03-09 16:31:39 -08:00
parent 3394d3835d
commit 00ec47b3f9

View file

@ -444,30 +444,35 @@ blockToMarkdown' opts (LineBlock lns) =
mdLines <- mapM (inlineListToMarkdown opts) lns mdLines <- mapM (inlineListToMarkdown opts) lns
return $ (vcat $ map (hang 2 (text "| ")) mdLines) <> blankline return $ (vcat $ map (hang 2 (text "| ")) mdLines) <> blankline
else blockToMarkdown opts $ linesToPara lns else blockToMarkdown opts $ linesToPara lns
blockToMarkdown' opts b@(RawBlock f str) blockToMarkdown' opts b@(RawBlock f str) = do
| f `elem` ["markdown", "markdown_github", "markdown_phpextra", plain <- asks envPlain
"markdown_mmd", "markdown_strict"] let Format fmt = f
= return $ text str <> text "\n" let rawAttribBlock = return $
| f `elem` ["html", "html5", "html4"] && isEnabled Ext_raw_html opts = do (text "```{=" <> text fmt <> "}") $$
plain <- asks envPlain text str $$
return $ if plain (text "```" <> text "\n")
then empty let renderEmpty = mempty <$ report (BlockNotRendered b)
else if isEnabled Ext_markdown_attribute opts case () of
then text (addMarkdownAttribute str) <> text "\n" _ | plain -> renderEmpty
else text str <> text "\n" | f `elem` ["markdown", "markdown_github", "markdown_phpextra",
| f `elem` ["latex", "tex"] && isEnabled Ext_raw_tex opts = do "markdown_mmd", "markdown_strict"] ->
plain <- asks envPlain return $ text str <> text "\n"
return $ if plain | f `elem` ["html", "html5", "html4"] ->
then empty case () of
else text str <> text "\n" _ | isEnabled Ext_markdown_attribute opts -> return $
| f == "plain" = do text (addMarkdownAttribute str) <> text "\n"
plain <- asks envPlain | isEnabled Ext_raw_html opts -> return $
return $ if plain text str <> text "\n"
then text str <> text "\n" | isEnabled Ext_raw_attribute opts -> rawAttribBlock
else empty | otherwise -> renderEmpty
| otherwise = do | f `elem` ["latex", "tex"] ->
report $ BlockNotRendered b case () of
return empty _ | isEnabled Ext_raw_tex opts -> return $
text str <> text "\n"
| isEnabled Ext_raw_attribute opts -> rawAttribBlock
| otherwise -> renderEmpty
| isEnabled Ext_raw_attribute opts -> rawAttribBlock
| otherwise -> renderEmpty
blockToMarkdown' opts HorizontalRule = do blockToMarkdown' opts HorizontalRule = do
return $ blankline <> text (replicate (writerColumns opts) '-') <> blankline return $ blankline <> text (replicate (writerColumns opts) '-') <> blankline
blockToMarkdown' opts (Header level attr inlines) = do blockToMarkdown' opts (Header level attr inlines) = do
@ -1115,17 +1120,33 @@ inlineToMarkdown opts (Math DisplayMath str) =
| otherwise -> (\x -> cr <> x <> cr) `fmap` | otherwise -> (\x -> cr <> x <> cr) `fmap`
(texMathToInlines DisplayMath str >>= inlineListToMarkdown opts) (texMathToInlines DisplayMath str >>= inlineListToMarkdown opts)
inlineToMarkdown opts il@(RawInline f str) = do inlineToMarkdown opts il@(RawInline f str) = do
let tickGroups = filter (\s -> '`' `elem` s) $ group str
let numticks = if null tickGroups
then 1
else 1 + maximum (map length tickGroups)
plain <- asks envPlain plain <- asks envPlain
if (plain && f == "plain") || (not plain && let Format fmt = f
( f `elem` ["markdown", "markdown_github", "markdown_phpextra", let rawAttribInline = return $
"markdown_mmd", "markdown_strict"] || text (replicate numticks '`') <> text str <>
(isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex")) || text (replicate numticks '`') <> text "{=" <> text fmt <> text "}"
(isEnabled Ext_raw_html opts && f `elem` ["html", "html4", "html5"]) let renderEmpty = mempty <$ report (InlineNotRendered il)
)) case () of
then return $ text str _ | plain -> renderEmpty
else do | f `elem` ["markdown", "markdown_github", "markdown_phpextra",
report $ InlineNotRendered il "markdown_mmd", "markdown_strict"] ->
return empty return $ text str
| f `elem` ["html", "html5", "html4"] ->
case () of
_ | isEnabled Ext_raw_html opts -> return $ text str
| isEnabled Ext_raw_attribute opts -> rawAttribInline
| otherwise -> renderEmpty
| f `elem` ["latex", "tex"] ->
case () of
_ | isEnabled Ext_raw_tex opts -> return $ text str
| isEnabled Ext_raw_attribute opts -> rawAttribInline
| otherwise -> renderEmpty
| isEnabled Ext_raw_attribute opts -> rawAttribInline
| otherwise -> renderEmpty
inlineToMarkdown opts (LineBreak) = do inlineToMarkdown opts (LineBreak) = do
plain <- asks envPlain plain <- asks envPlain
if plain || isEnabled Ext_hard_line_breaks opts if plain || isEnabled Ext_hard_line_breaks opts