aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-05-22 15:21:15 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2019-05-22 15:21:15 -0700
commitaef71894ceedb774c49b18db15fbde34396cfb85 (patch)
tree2e3f28938041a3c0ca562297bca4e790f4615f11 /src
parentc17ace8c094be5ed7a9f30b344793e139a413945 (diff)
downloadpandoc-aef71894ceedb774c49b18db15fbde34396cfb85.tar.gz
Markdown writer: Ensure the code fence is long enough.
Previously too few backticks were used when the code block contained an indented line of backticks. (Ditto tildes.) Cloess #5519.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 72d083734..b542d4193 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -23,7 +23,8 @@ import Control.Monad.State.Strict
import Data.Char (isPunctuation, isSpace, isAlphaNum)
import Data.Default
import qualified Data.HashMap.Strict as H
-import Data.List (find, group, intersperse, sortBy, stripPrefix, transpose)
+import Data.List (find, group, intersperse, sortBy, stripPrefix, transpose,
+ isPrefixOf)
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Monoid (Any (..))
@@ -544,16 +545,14 @@ blockToMarkdown' opts (CodeBlock attribs str) = return $
| isEnabled Ext_fenced_code_blocks opts ->
tildes <> attrs <> cr <> text str <> cr <> tildes <> blankline
_ -> nest (writerTabStop opts) (text str) <> blankline
- where tildes = text $ case [ln | ln <- lines str, all (=='~') ln] of
- [] -> "~~~~"
- xs -> case maximum $ map length xs of
- n | n < 3 -> "~~~~"
- | otherwise -> replicate (n+1) '~'
- backticks = text $ case [ln | ln <- lines str, all (=='`') ln] of
- [] -> "```"
- xs -> case maximum $ map length xs of
- n | n < 3 -> "```"
- | otherwise -> replicate (n+1) '`'
+ where endline c = text $ case [length ln
+ | ln <- map trim (lines str)
+ , [c,c,c] `isPrefixOf` ln
+ , all (== c) ln] of
+ [] -> replicate 3 c
+ xs -> replicate (maximum xs + 1) c
+ backticks = endline '`'
+ tildes = endline '~'
attrs = if isEnabled Ext_fenced_code_attributes opts
then nowrap $ " " <> attrsToMarkdown attribs
else case attribs of