Skip to content

Commit

Permalink
Markdown writer: Cleaner (code)blocks with single class
Browse files Browse the repository at this point in the history
When a block only has a single class and no other attributes,
it is not necessary to wrap the class attribute in curly braces –
the class name can be placed after the opening mark as is.

This will result in bit cleaner output when pandoc is used
as a markdown pretty-printer.
  • Loading branch information
jtojnar committed Apr 24, 2021
1 parent ac62b08 commit 5f21098
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/Text/Pandoc/Writers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ attrsToMarkdown attribs = braces $ hsep [attribId, attribClasses, attribKeys]
escAttrChar '\\' = literal "\\\\"
escAttrChar c = literal $ T.singleton c

-- | (Code) blocks with a single class can just use it standalone,
-- no need to bother with curly braces.
classOrAttrsToMarkdown :: Attr -> Doc Text
classOrAttrsToMarkdown ("",[cls],_) = literal cls
classOrAttrsToMarkdown attrs = attrsToMarkdown attrs

linkAttributes :: WriterOptions -> Attr -> Doc Text
linkAttributes opts attr =
if isEnabled Ext_link_attributes opts && attr /= nullAttr
Expand Down Expand Up @@ -344,7 +350,7 @@ blockToMarkdown' opts (Div attrs ils) = do
case () of
_ | isEnabled Ext_fenced_divs opts &&
attrs /= nullAttr ->
nowrap (literal ":::" <+> attrsToMarkdown attrs) $$
nowrap (literal ":::" <+> classOrAttrsToMarkdown attrs) $$
chomp contents $$
literal ":::" <> blankline
| isEnabled Ext_native_divs opts ||
Expand Down Expand Up @@ -513,7 +519,7 @@ blockToMarkdown' opts (CodeBlock attribs str) = do
backticks = endline '`'
tildes = endline '~'
attrs = if isEnabled Ext_fenced_code_attributes opts
then nowrap $ " " <> attrsToMarkdown attribs
then nowrap $ " " <> classOrAttrsToMarkdown attribs
else case attribs of
(_,cls:_,_) -> " " <> literal cls
_ -> empty
Expand Down
4 changes: 2 additions & 2 deletions test/command/5304.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
````
% pandoc -f markdown -t markdown
``` {.markdown}
``` markdown
`«sträng»`
`` «sträng» ``
Expand All @@ -11,7 +11,7 @@
...
```
^D
``` {.markdown}
``` markdown
`«sträng»`
`` «sträng» ``
Expand Down
4 changes: 2 additions & 2 deletions test/command/5519.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
~~~
% pandoc -t markdown
``````{.attr}
`````` attr
```
code
```
``````
^D
```` {.attr}
```` attr
```
code
```
Expand Down
4 changes: 2 additions & 2 deletions test/command/6925.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ a
\end{thm2}
\end{document}
^D
::: {.thm}
::: thm
**Theorem 1**. *a*
![image](1.png)
:::
::: {.thm2}
::: thm2
**Theorem 1**. a
![image](1.png)
Expand Down
4 changes: 2 additions & 2 deletions test/command/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
## b
::: {.interior}
::: interior
# C
## cc
# D
:::
::: {.blue}
::: blue
# E
## e
Expand Down

0 comments on commit 5f21098

Please sign in to comment.