jgm/pandoc — pull request #1786 by nishidayuya (2014-12-06 07:50:52+00:00)
Title: Add --prefer-fenced-code-blocks option

I like `fenced code blocks`. It makes me to paste many codes without indenting. So, I want to use `fenced code blocks` if there are no attributes too. Before this pull-request, Pandoc uses `indented code blocks` if there are no attributes.

Same mind is here: https://groups.google.com/forum/#!topic/pandoc-discuss/Wl0R4Ja0xus

This pull-request adds `--prefer-fenced-code-blocks` option to resolve it.

---

Input file is:

``` sh
yuya@yoshiyuki|18:34:00|0% cat /tmp/a.textile

no attributes.

some attributes.

```

Output without `--prefer-fenced-code-blocks`:

yuya@yoshiyuki|18:34:11|0% ./dist/build/pandoc/pandoc -t markdown /tmp/a.textile

no attributes.

``` {#foo-id .bar-class baz="quux"}
some attributes.
```

Output with `--prefer-fenced-code-blocks`:

yuya@yoshiyuki|18:34:45|0% ./dist/build/pandoc/pandoc --prefer-fenced-code-blocks -t markdown /tmp/a.textile
```
no attributes.
```

``` {#foo-id .bar-class baz="quux"}
some attributes.
```

---

My mind history:

1. Pandoc is written in Haskell. I didn't know it. So I enjoyed Haskell book.
2. I checked first delimited code blocks commit ( 8889ae8b5b091eb9d35bdf4698d39c562cb2e374). and I think `indented code blocks` is prefered than `fenced code blocks`. I must keep this behavior.
3. In `Ext_backtick_code_blocks` or `Ext_fenced_code_blocks`, I tried following changes:
``` diff
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index f06f1d6..2863cde 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -391,13 +391,12 @@ blockToMarkdown opts (CodeBlock (_,classes,_) str)
| "haskell" `elem` classes && "literate" `elem` classes &&
isEnabled Ext_literate_haskell opts =
return $ prefixed "> " (text str) <> blankline
-blockToMarkdown opts (CodeBlock attribs str) = return $
- case attribs == nullAttr of
- False | isEnabled Ext_backtick_code_blocks opts ->
- backticks <> attrs <> cr <> text str <> cr <> backticks <> blankline
- | isEnabled Ext_fenced_code_blocks opts ->
- tildes <> attrs <> cr <> text str <> cr <> tildes <> blankline
- _ -> nest (writerTabStop opts) (text str) <> blankline
+blockToMarkdown opts (CodeBlock attribs str)
+ | isEnabled Ext_backtick_code_blocks opts =
+ return $ backticks <> attrs <> cr <> text str <> cr <> backticks <> blankline
+ | isEnabled Ext_fenced_code_blocks opts =
+ return $ tildes <> attrs <> cr <> text str <> cr <> tildes <> blankline
+ | True = return $ nest (writerTabStop opts) (text str) <> blankline
where tildes = text $ case [ln | ln <- lines str, all (=='~') ln] of
[] -> "~~~~"
xs -> case maximum $ map length xs of
@@ -409,7 +408,9 @@ blockToMarkdown opts (CodeBlock attribs str) = return $
n | n < 3 -> "```"
| otherwise -> replicate (n+1) '`'
attrs = if isEnabled Ext_fenced_code_attributes opts
- then nowrap $ " " <> attrsToMarkdown attribs
+ then nowrap $ if attribs == nullAttr
+ then ""
+ else " " <> attrsToMarkdown attribs
else case attribs of
(_,(cls:_),_) -> " " <> text cls
_ -> empty
```
But `pandocExtensions` uses both options (`Ext_fenced_code_blocks` and `Ext_backtick_code_blocks`), so these changes makes no hybrid way (`indented code blocks` and `fenced code blocks`). These changes were discarded.
4. I tried to add option (this PR changes). I thought following option names and select `--prefer-fenced-code-blocks`
* `--default-delimited-code-blocks`
* `--default-fenced-code-blocks`
* `--prefer-fenced-code-blocks`
* `--prefer-fenced`