git.iko.soy/typeable/pandoc
on master help sign in
CommonMark writer: do not throw away attributes when Ext_attributes is enabled

Ext_attributes covers at least the following: - Ext_fenced_code_attributes - Ext_header_attributes - Ext_inline_code_attributes - Ext_link_attributes

commit7a3ee9d3d83b73cb53de80e01a9968ebf8f7cf12 authorJan Tojnar <jtojnar@gmail.com> dateSat 5 Jun 2021, 15:53 (5 years ago) parentc6f8c38
2 files changed+17−13
src/Text/Pandoc/Writers/Markdown.hs+5−3 src/Text/Pandoc/Writers/Markdown/Inline.hs+12−10
src/Text/Pandoc/Writers/Markdown.hs+5−3
@@ -366,7 +366,7 @@ blockToMarkdown' opts (Plain inlines) = do
366366-- title beginning with fig: indicates figure
367367blockToMarkdown' opts (Para [Image attr alt (src,tgt@(T.stripPrefix "fig:" -> Just tit))])
368368 | isEnabled Ext_raw_html opts &&
369− not (isEnabled Ext_link_attributes opts) &&
369+ not (isEnabled Ext_link_attributes opts || isEnabled Ext_attributes opts) &&
370370 attr /= nullAttr = -- use raw HTML
371371 (<> blankline) . literal . T.strip <$>
372372 writeHtml5String opts{ writerTemplate = Nothing }
@@ -431,7 +431,8 @@ blockToMarkdown' opts (Header level attr inlines) = do
431431 && id' == autoId -> empty
432432 (id',_,_) | isEnabled Ext_mmd_header_identifiers opts ->
433433 space <> brackets (literal id')
434− _ | isEnabled Ext_header_attributes opts ->
434+ _ | isEnabled Ext_header_attributes opts ||
435+ isEnabled Ext_attributes opts ->
435436 space <> attrsToMarkdown attr
436437 | otherwise -> empty
437438 contents <- inlineListToMarkdown opts $
@@ -490,7 +491,8 @@ blockToMarkdown' opts (CodeBlock attribs str) = do
490491 endline c = literal $ T.replicate (endlineLen c) $ T.singleton c
491492 backticks = endline '`'
492493 tildes = endline '~'
493− attrs = if isEnabled Ext_fenced_code_attributes opts
494+ attrs = if isEnabled Ext_fenced_code_attributes opts ||
495+ isEnabled Ext_attributes opts
494496 then nowrap $ " " <> classOrAttrsToMarkdown attribs
495497 else case attribs of
496498 (_,cls:_,_) -> " " <> literal cls
src/Text/Pandoc/Writers/Markdown/Inline.hs+12−10
@@ -117,7 +117,7 @@ attrsToMarkdown attribs = braces $ hsep [attribId, attribClasses, attribKeys]
117117
118118linkAttributes :: WriterOptions -> Attr -> Doc Text
119119linkAttributes opts attr =
120− if isEnabled Ext_link_attributes opts && attr /= nullAttr
120+ if (isEnabled Ext_link_attributes opts || isEnabled Ext_attributes opts) && attr /= nullAttr
121121 then attrsToMarkdown attr
122122 else empty
123123
@@ -394,13 +394,15 @@ inlineToMarkdown opts (Quoted DoubleQuote lst) = do
394394 then "&ldquo;" <> contents <> "&rdquo;"
395395 else "“" <> contents <> "”"
396396inlineToMarkdown opts (Code attr str) = do
397− let tickGroups = filter (T.any (== '`')) $ T.group str
398− let longest = maybe 0 maximum $ nonEmpty $ map T.length tickGroups
399− let marker = T.replicate (longest + 1) "`"
400− let spacer = if longest == 0 then "" else " "
401− let attrs = if isEnabled Ext_inline_code_attributes opts && attr /= nullAttr
402− then attrsToMarkdown attr
403− else empty
397+ let tickGroups = filter (T.any (== '`')) $ T.group str
398+ let longest = maybe 0 maximum $ nonEmpty $ map T.length tickGroups
399+ let marker = T.replicate (longest + 1) "`"
400+ let spacer = if longest == 0 then "" else " "
401+ let attrsEnabled = isEnabled Ext_inline_code_attributes opts ||
402+ isEnabled Ext_attributes opts
403+ let attrs = if attrsEnabled && attr /= nullAttr
404+ then attrsToMarkdown attr
405+ else empty
404406 variant <- asks envVariant
405407 case variant of
406408 PlainText -> return $ literal str
@@ -559,7 +561,7 @@ inlineToMarkdown opts lnk@(Link attr txt (src, tit)) = do
559561 else "[" <> reftext <> "]"
560562 in return $ first <> second
561563 | isEnabled Ext_raw_html opts
562− , not (isEnabled Ext_link_attributes opts)
564+ , not (isEnabled Ext_link_attributes opts || isEnabled Ext_attributes opts)
563565 , attr /= nullAttr -> -- use raw HTML to render attributes
564566 literal . T.strip <$>
565567 writeHtml5String opts{ writerTemplate = Nothing }
@@ -569,7 +571,7 @@ inlineToMarkdown opts lnk@(Link attr txt (src, tit)) = do
569571 linkAttributes opts attr
570572inlineToMarkdown opts img@(Image attr alternate (source, tit))
571573 | isEnabled Ext_raw_html opts &&
572− not (isEnabled Ext_link_attributes opts) &&
574+ not (isEnabled Ext_link_attributes opts || isEnabled Ext_attributes opts) &&
573575 attr /= nullAttr = -- use raw HTML
574576 literal . T.strip <$>
575577 writeHtml5String opts{ writerTemplate = Nothing } (Pandoc nullMeta [Plain [img]])