From d20152e01135a27decfd08dbda097fd1fda75354 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 18 Apr 2015 10:45:46 -0700 Subject: [PATCH] Markdown writer: improved escaping. `<` should not be escaped as `\<`, for compatibility with original Markdown. We now escape `<` and `>` with entities. Also, we now backslash-escape square brackets. Closes #2086. --- src/Text/Pandoc/Writers/Markdown.hs | 5 +++-- tests/Tests/Writers/Markdown.hs | 4 ++-- tests/writer.markdown | 22 +++++++++++----------- tests/writer.opml | 8 ++++---- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index dee4d56a4..ef87a8603 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -246,7 +246,8 @@ noteToMarkdown opts num blocks = do -- | Escape special characters for Markdown. escapeString :: WriterOptions -> String -> String escapeString opts = escapeStringUsing markdownEscapes - where markdownEscapes = backslashEscapes specialChars + where markdownEscapes = ('<', "<") : ('>', ">") : + backslashEscapes specialChars specialChars = (if isEnabled Ext_superscript opts then ('^':) @@ -257,7 +258,7 @@ escapeString opts = escapeStringUsing markdownEscapes (if isEnabled Ext_tex_math_dollars opts then ('$':) else id) $ - "\\`*_<>#" + "\\`*_[]#" -- | Construct table of contents from list of header blocks. tableOfContents :: WriterOptions -> [Block] -> Doc diff --git a/tests/Tests/Writers/Markdown.hs b/tests/Tests/Writers/Markdown.hs index dce40ddcb..8ffd7a8c0 100644 --- a/tests/Tests/Writers/Markdown.hs +++ b/tests/Tests/Writers/Markdown.hs @@ -84,13 +84,13 @@ shortcutLinkRefsTests = ] , "Reference link is followed by text in brackets" =: (para ((link "/url" "" "link") <> "[text in brackets]")) - =?> unlines [ "[link][][text in brackets]" + =?> unlines [ "[link][]\\[text in brackets\\]" , "" , " [link]: /url" ] , "Reference link is followed by space and text in brackets" =: (para ((link "/url" "" "link") <> " [text in brackets]")) - =?> unlines [ "[link][] [text in brackets]" + =?> unlines [ "[link][] \\[text in brackets\\]" , "" , " [link]: /url" ] diff --git a/tests/writer.markdown b/tests/writer.markdown index 7276b31c7..6a0b9801c 100644 --- a/tests/writer.markdown +++ b/tests/writer.markdown @@ -80,7 +80,7 @@ E-mail style: > > > nested -This should not be a block quote: 2 \> 1. +This should not be a block quote: 2 > 1. And a following paragraph. @@ -581,9 +581,9 @@ AT&T is another way to write it. This & that. -4 \< 5. +4 < 5. -6 \> 5. +6 > 5. Backslash: \\ @@ -597,15 +597,15 @@ Left brace: { Right brace: } -Left bracket: [ +Left bracket: \[ -Right bracket: ] +Right bracket: \] Left paren: ( Right paren: ) -Greater-than: \> +Greater-than: > Hash: \# @@ -652,7 +652,7 @@ Foo [bar](/url/). Foo [bar](/url/). -With [embedded [brackets]](/url/). +With [embedded \[brackets\]](/url/). [b](/url/) by itself should be a link. @@ -662,7 +662,7 @@ Indented [twice](/url). Indented [thrice](/url). -This should [not][] be a link. +This should \[not\]\[\] be a link. [not]: /url @@ -716,8 +716,8 @@ Footnotes ========= Here is a footnote reference,[^1] and another.[^2] This should *not* be a -footnote reference, because it contains a space.[\^my note] Here is an inline -note.[^3] +footnote reference, because it contains a space.\[\^my note\] Here is an +inline note.[^3] > Notes can go in quotes.[^4] @@ -740,7 +740,7 @@ This paragraph should not be part of the note, as it is not indented. [^3]: This is *easier* to type. Inline notes may contain [links](http://google.com) and `]` verbatim characters, as well as - [bracketed text]. + \[bracketed text\]. [^4]: In quote. diff --git a/tests/writer.opml b/tests/writer.opml index 8f79e842c..c10ca44c8 100644 --- a/tests/writer.opml +++ b/tests/writer.opml @@ -26,7 +26,7 @@ - + @@ -52,12 +52,12 @@ - + - + @@ -66,7 +66,7 @@ - +