git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: improve handling of raw blocks/inline.
00ec47bJohn MacFarlane
src/Text/Pandoc/Writers/Markdown.hs+55−34
@@ -444,30 +444,35 @@ blockToMarkdown' opts (LineBlock lns) =
444 mdLines <- mapM (inlineListToMarkdown opts) lns
444 mdLines <- mapM (inlineListToMarkdown opts) lns
445 return $ (vcat $ map (hang 2 (text "| ")) mdLines) <> blankline
445 return $ (vcat $ map (hang 2 (text "| ")) mdLines) <> blankline
446 else blockToMarkdown opts $ linesToPara lns
446 else blockToMarkdown opts $ linesToPara lns
447−blockToMarkdown' opts b@(RawBlock f str)
447+blockToMarkdown' opts b@(RawBlock f str) = do
448+ plain <- asks envPlain
449+ let Format fmt = f
450+ let rawAttribBlock = return $
451+ (text "```{=" <> text fmt <> "}") $$
452+ text str $$
453+ (text "```" <> text "\n")
454+ let renderEmpty = mempty <$ report (BlockNotRendered b)
455+ case () of
456+ _ | plain -> renderEmpty
448− | f `elem` ["markdown", "markdown_github", "markdown_phpextra",
457+ | f `elem` ["markdown", "markdown_github", "markdown_phpextra",
449− "markdown_mmd", "markdown_strict"]
458+ "markdown_mmd", "markdown_strict"] ->
450− = return $ text str <> text "\n"
459+ return $ text str <> text "\n"
451− | f `elem` ["html", "html5", "html4"] && isEnabled Ext_raw_html opts = do
460+ | f `elem` ["html", "html5", "html4"] ->
452− plain <- asks envPlain
461+ case () of
453− return $ if plain
454− then empty
455− else if isEnabled Ext_markdown_attribute opts
462+ _ | isEnabled Ext_markdown_attribute opts -> return $
456− then text (addMarkdownAttribute str) <> text "\n"
463+ text (addMarkdownAttribute str) <> text "\n"
464+ | isEnabled Ext_raw_html opts -> return $
457− else text str <> text "\n"
465+ text str <> text "\n"
466+ | isEnabled Ext_raw_attribute opts -> rawAttribBlock
467+ | otherwise -> renderEmpty
468+ | f `elem` ["latex", "tex"] ->
469+ case () of
458− | f `elem` ["latex", "tex"] && isEnabled Ext_raw_tex opts = do
470+ _ | isEnabled Ext_raw_tex opts -> return $
459− plain <- asks envPlain
460− return $ if plain
461− then empty
462− else text str <> text "\n"
471+ text str <> text "\n"
463− | f == "plain" = do
472+ | isEnabled Ext_raw_attribute opts -> rawAttribBlock
464− plain <- asks envPlain
473+ | otherwise -> renderEmpty
465− return $ if plain
474+ | isEnabled Ext_raw_attribute opts -> rawAttribBlock
466− then text str <> text "\n"
467− else empty
468− | otherwise = do
475+ | otherwise -> renderEmpty
469− report $ BlockNotRendered b
470− return empty
471blockToMarkdown' opts HorizontalRule = do
476blockToMarkdown' opts HorizontalRule = do
472 return $ blankline <> text (replicate (writerColumns opts) '-') <> blankline
477 return $ blankline <> text (replicate (writerColumns opts) '-') <> blankline
473blockToMarkdown' opts (Header level attr inlines) = do
478blockToMarkdown' opts (Header level attr inlines) = do
@@ -1115,17 +1120,33 @@ inlineToMarkdown opts (Math DisplayMath str) =
1115 | otherwise -> (\x -> cr <> x <> cr) `fmap`
1120 | otherwise -> (\x -> cr <> x <> cr) `fmap`
1116 (texMathToInlines DisplayMath str >>= inlineListToMarkdown opts)
1121 (texMathToInlines DisplayMath str >>= inlineListToMarkdown opts)
1117inlineToMarkdown opts il@(RawInline f str) = do
1122inlineToMarkdown opts il@(RawInline f str) = do
1123+ let tickGroups = filter (\s -> '`' `elem` s) $ group str
1124+ let numticks = if null tickGroups
1125+ then 1
1126+ else 1 + maximum (map length tickGroups)
1118 plain <- asks envPlain
1127 plain <- asks envPlain
1119− if (plain && f == "plain") || (not plain &&
1128+ let Format fmt = f
1129+ let rawAttribInline = return $
1130+ text (replicate numticks '`') <> text str <>
1131+ text (replicate numticks '`') <> text "{=" <> text fmt <> text "}"
1132+ let renderEmpty = mempty <$ report (InlineNotRendered il)
1133+ case () of
1134+ _ | plain -> renderEmpty
1120− ( f `elem` ["markdown", "markdown_github", "markdown_phpextra",
1135+ | f `elem` ["markdown", "markdown_github", "markdown_phpextra",
1121− "markdown_mmd", "markdown_strict"] ||
1136+ "markdown_mmd", "markdown_strict"] ->
1122− (isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex")) ||
1137+ return $ text str
1123− (isEnabled Ext_raw_html opts && f `elem` ["html", "html4", "html5"])
1138+ | f `elem` ["html", "html5", "html4"] ->
1124− ))
1139+ case () of
1140+ _ | isEnabled Ext_raw_html opts -> return $ text str
1141+ | isEnabled Ext_raw_attribute opts -> rawAttribInline
1142+ | otherwise -> renderEmpty
1143+ | f `elem` ["latex", "tex"] ->
1144+ case () of
1125− then return $ text str
1145+ _ | isEnabled Ext_raw_tex opts -> return $ text str
1126− else do
1146+ | isEnabled Ext_raw_attribute opts -> rawAttribInline
1127− report $ InlineNotRendered il
1147+ | otherwise -> renderEmpty
1128− return empty
1148+ | isEnabled Ext_raw_attribute opts -> rawAttribInline
1149+ | otherwise -> renderEmpty
1129inlineToMarkdown opts (LineBreak) = do
1150inlineToMarkdown opts (LineBreak) = do
1130 plain <- asks envPlain
1151 plain <- asks envPlain
1131 if plain || isEnabled Ext_hard_line_breaks opts
1152 if plain || isEnabled Ext_hard_line_breaks opts