git.iko.soy/typeable/pandoc
on master help sign in
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.

commitd20152e01135a27decfd08dbda097fd1fda75354 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 18 Apr 2015, 10:45 (12 years ago) parent0b80073
4 files changed+20−19
src/Text/Pandoc/Writers/Markdown.hs+3−2 tests/Tests/Writers/Markdown.hs+2−2 tests/writer.markdown+11−11 tests/writer.opml+4−4
src/Text/Pandoc/Writers/Markdown.hs+3−2
@@ -246,7 +246,8 @@ noteToMarkdown opts num blocks = do
246246-- | Escape special characters for Markdown.
247247escapeString :: WriterOptions -> String -> String
248248escapeString opts = escapeStringUsing markdownEscapes
249− where markdownEscapes = backslashEscapes specialChars
249+ where markdownEscapes = ('<', "&lt;") : ('>', "&gt;") :
250+ backslashEscapes specialChars
250251 specialChars =
251252 (if isEnabled Ext_superscript opts
252253 then ('^':)
@@ -257,7 +258,7 @@ escapeString opts = escapeStringUsing markdownEscapes
257258 (if isEnabled Ext_tex_math_dollars opts
258259 then ('$':)
259260 else id) $
260− "\\`*_<>#"
261+ "\\`*_[]#"
261262
262263-- | Construct table of contents from list of header blocks.
263264tableOfContents :: WriterOptions -> [Block] -> Doc
tests/Tests/Writers/Markdown.hs+2−2
@@ -84,13 +84,13 @@ shortcutLinkRefsTests =
8484 ]
8585 , "Reference link is followed by text in brackets"
8686 =: (para ((link "/url" "" "link") <> "[text in brackets]"))
87− =?> unlines [ "[link][][text in brackets]"
87+ =?> unlines [ "[link][]\\[text in brackets\\]"
8888 , ""
8989 , " [link]: /url"
9090 ]
9191 , "Reference link is followed by space and text in brackets"
9292 =: (para ((link "/url" "" "link") <> " [text in brackets]"))
93− =?> unlines [ "[link][] [text in brackets]"
93+ =?> unlines [ "[link][] \\[text in brackets\\]"
9494 , ""
9595 , " [link]: /url"
9696 ]
tests/writer.markdown+11−11
@@ -80,7 +80,7 @@ E-mail style:
8080>
8181> > nested
8282
83−This should not be a block quote: 2 \> 1.
83+This should not be a block quote: 2 &gt; 1.
8484
8585And a following paragraph.
8686
@@ -581,9 +581,9 @@ AT&T is another way to write it.
581581
582582This & that.
583583
584−4 \< 5.
584+4 &lt; 5.
585585
586−6 \> 5.
586+6 &gt; 5.
587587
588588Backslash: \\
589589
@@ -597,15 +597,15 @@ Left brace: {
597597
598598Right brace: }
599599
600−Left bracket: [
600+Left bracket: \[
601601
602−Right bracket: ]
602+Right bracket: \]
603603
604604Left paren: (
605605
606606Right paren: )
607607
608−Greater-than: \>
608+Greater-than: &gt;
609609
610610Hash: \#
611611
@@ -652,7 +652,7 @@ Foo [bar](/url/).
652652
653653Foo [bar](/url/).
654654
655−With [embedded [brackets]](/url/).
655+With [embedded \[brackets\]](/url/).
656656
657657[b](/url/) by itself should be a link.
658658
@@ -662,7 +662,7 @@ Indented [twice](/url).
662662
663663Indented [thrice](/url).
664664
665−This should [not][] be a link.
665+This should \[not\]\[\] be a link.
666666
667667 [not]: /url
668668
@@ -716,8 +716,8 @@ Footnotes
716716=========
717717
718718Here is a footnote reference,[^1] and another.[^2] This should *not* be a
719−footnote reference, because it contains a space.[\^my note] Here is an inline
720−note.[^3]
719+footnote reference, because it contains a space.\[\^my note\] Here is an
720+inline note.[^3]
721721
722722> Notes can go in quotes.[^4]
723723
@@ -740,7 +740,7 @@ This paragraph should not be part of the note, as it is not indented.
740740
741741[^3]: This is *easier* to type. Inline notes may contain
742742 [links](http://google.com) and `]` verbatim characters, as well as
743− [bracketed text].
743+ \[bracketed text\].
744744
745745[^4]: In quote.
746746
tests/writer.opml+4−4
@@ -26,7 +26,7 @@
2626</outline>
2727<outline text="Paragraphs" _note="Here’s a regular paragraph.&#10;&#10;In Markdown 1.0.0 and earlier. Version 8. This line turns into a list&#10;item. Because a hard-wrapped line in the middle of a paragraph looked&#10;like a list item.&#10;&#10;Here’s one with a bullet. \* criminey.&#10;&#10;There should be a hard line break\&#10;here.&#10;&#10;------------------------------------------------------------------------">
2828</outline>
29−<outline text="Block Quotes" _note="E-mail style:&#10;&#10;&gt; This is a block quote. It is pretty short.&#10;&#10;&gt; Code in a block quote:&#10;&gt;&#10;&gt; sub status {&#10;&gt; print &quot;working&quot;;&#10;&gt; }&#10;&gt;&#10;&gt; A list:&#10;&gt;&#10;&gt; 1. item one&#10;&gt; 2. item two&#10;&gt;&#10;&gt; Nested block quotes:&#10;&gt;&#10;&gt; &gt; nested&#10;&gt;&#10;&gt; &gt; nested&#10;&#10;This should not be a block quote: 2 \&gt; 1.&#10;&#10;And a following paragraph.&#10;&#10;------------------------------------------------------------------------">
29+<outline text="Block Quotes" _note="E-mail style:&#10;&#10;&gt; This is a block quote. It is pretty short.&#10;&#10;&gt; Code in a block quote:&#10;&gt;&#10;&gt; sub status {&#10;&gt; print &quot;working&quot;;&#10;&gt; }&#10;&gt;&#10;&gt; A list:&#10;&gt;&#10;&gt; 1. item one&#10;&gt; 2. item two&#10;&gt;&#10;&gt; Nested block quotes:&#10;&gt;&#10;&gt; &gt; nested&#10;&gt;&#10;&gt; &gt; nested&#10;&#10;This should not be a block quote: 2 &amp;gt; 1.&#10;&#10;And a following paragraph.&#10;&#10;------------------------------------------------------------------------">
3030</outline>
3131<outline text="Code Blocks" _note="Code:&#10;&#10; ---- (should be four hyphens)&#10;&#10; sub status {&#10; print &quot;working&quot;;&#10; }&#10;&#10; this code block is indented by one tab&#10;&#10;And:&#10;&#10; this code block is indented by two tabs&#10;&#10; These should not be escaped: \$ \\ \&gt; \[ \{&#10;&#10;------------------------------------------------------------------------">
3232</outline>
@@ -52,12 +52,12 @@
5252</outline>
5353<outline text="LaTeX" _note="- \cite[22-23]{smith.1899}&#10;- $2+2=4$&#10;- $x \in y$&#10;- $\alpha \wedge \omega$&#10;- $223$&#10;- $p$-Tree&#10;- Here’s some display math:&#10; $$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$&#10;- Here’s one that has a line break in it:&#10; $\alpha + \omega \times x^2$.&#10;&#10;These shouldn’t be math:&#10;&#10;- To get the famous equation, write `$e = mc^2$`.&#10;- \$22,000 is a *lot* of money. So is \$34,000. (It worked if “lot”&#10; is emphasized.)&#10;- Shoes (\$20) and socks (\$5).&#10;- Escaped `$`: \$73 *this should be emphasized* 23\$.&#10;&#10;Here’s a LaTeX table:&#10;&#10;\begin{tabular}{|l|l|}\hline&#10;Animal &amp; Number \\ \hline&#10;Dog &amp; 2 \\&#10;Cat &amp; 1 \\ \hline&#10;\end{tabular}&#10;&#10;------------------------------------------------------------------------">
5454</outline>
55−<outline text="Special Characters" _note="Here is some unicode:&#10;&#10;- I hat: Î&#10;- o umlaut: ö&#10;- section: §&#10;- set membership: ∈&#10;- copyright: ©&#10;&#10;AT&amp;T has an ampersand in their name.&#10;&#10;AT&amp;T is another way to write it.&#10;&#10;This &amp; that.&#10;&#10;4 \&lt; 5.&#10;&#10;6 \&gt; 5.&#10;&#10;Backslash: \\&#10;&#10;Backtick: \`&#10;&#10;Asterisk: \*&#10;&#10;Underscore: \_&#10;&#10;Left brace: {&#10;&#10;Right brace: }&#10;&#10;Left bracket: [&#10;&#10;Right bracket: ]&#10;&#10;Left paren: (&#10;&#10;Right paren: )&#10;&#10;Greater-than: \&gt;&#10;&#10;Hash: \#&#10;&#10;Period: .&#10;&#10;Bang: !&#10;&#10;Plus: +&#10;&#10;Minus: -&#10;&#10;------------------------------------------------------------------------">
55+<outline text="Special Characters" _note="Here is some unicode:&#10;&#10;- I hat: Î&#10;- o umlaut: ö&#10;- section: §&#10;- set membership: ∈&#10;- copyright: ©&#10;&#10;AT&amp;T has an ampersand in their name.&#10;&#10;AT&amp;T is another way to write it.&#10;&#10;This &amp; that.&#10;&#10;4 &amp;lt; 5.&#10;&#10;6 &amp;gt; 5.&#10;&#10;Backslash: \\&#10;&#10;Backtick: \`&#10;&#10;Asterisk: \*&#10;&#10;Underscore: \_&#10;&#10;Left brace: {&#10;&#10;Right brace: }&#10;&#10;Left bracket: \[&#10;&#10;Right bracket: \]&#10;&#10;Left paren: (&#10;&#10;Right paren: )&#10;&#10;Greater-than: &amp;gt;&#10;&#10;Hash: \#&#10;&#10;Period: .&#10;&#10;Bang: !&#10;&#10;Plus: +&#10;&#10;Minus: -&#10;&#10;------------------------------------------------------------------------">
5656</outline>
5757<outline text="Links">
5858 <outline text="Explicit" _note="Just a [URL](/url/).&#10;&#10;[URL and title](/url/ &quot;title&quot;).&#10;&#10;[URL and title](/url/ &quot;title preceded by two spaces&quot;).&#10;&#10;[URL and title](/url/ &quot;title preceded by a tab&quot;).&#10;&#10;[URL and title](/url/ &quot;title with &quot;quotes&quot; in it&quot;)&#10;&#10;[URL and title](/url/ &quot;title with single quotes&quot;)&#10;&#10;[with\_underscore](/url/with_underscore)&#10;&#10;[Email link](mailto:nobody@nowhere.net)&#10;&#10;[Empty]().">
5959 </outline>
60− <outline text="Reference" _note="Foo [bar](/url/).&#10;&#10;Foo [bar](/url/).&#10;&#10;Foo [bar](/url/).&#10;&#10;With [embedded [brackets]](/url/).&#10;&#10;[b](/url/) by itself should be a link.&#10;&#10;Indented [once](/url).&#10;&#10;Indented [twice](/url).&#10;&#10;Indented [thrice](/url).&#10;&#10;This should [not][] be a link.&#10;&#10; [not]: /url&#10;&#10;Foo [bar](/url/ &quot;Title with &quot;quotes&quot; inside&quot;).&#10;&#10;Foo [biz](/url/ &quot;Title with &quot;quote&quot; inside&quot;).">
60+ <outline text="Reference" _note="Foo [bar](/url/).&#10;&#10;Foo [bar](/url/).&#10;&#10;Foo [bar](/url/).&#10;&#10;With [embedded \[brackets\]](/url/).&#10;&#10;[b](/url/) by itself should be a link.&#10;&#10;Indented [once](/url).&#10;&#10;Indented [twice](/url).&#10;&#10;Indented [thrice](/url).&#10;&#10;This should \[not\]\[\] be a link.&#10;&#10; [not]: /url&#10;&#10;Foo [bar](/url/ &quot;Title with &quot;quotes&quot; inside&quot;).&#10;&#10;Foo [biz](/url/ &quot;Title with &quot;quote&quot; inside&quot;).">
6161 </outline>
6262 <outline text="With ampersands" _note="Here’s a [link with an ampersand in the&#10;URL](http://example.com/?foo=1&amp;bar=2).&#10;&#10;Here’s a link with an amersand in the link text:&#10;[AT&amp;T](http://att.com/ &quot;AT&amp;T&quot;).&#10;&#10;Here’s an [inline link](/script?foo=1&amp;bar=2).&#10;&#10;Here’s an [inline link in pointy braces](/script?foo=1&amp;bar=2).">
6363 </outline>
@@ -66,7 +66,7 @@
6666</outline>
6767<outline text="Images" _note="From “Voyage dans la Lune” by Georges Melies (1902):&#10;&#10;![lalune](lalune.jpg &quot;Voyage dans la Lune&quot;)&#10;&#10;Here is a movie ![movie](movie.jpg) icon.&#10;&#10;------------------------------------------------------------------------">
6868</outline>
69−<outline text="Footnotes" _note="Here is a footnote reference,[^1] and another.[^2] This should *not* be&#10;a footnote reference, because it contains a space.[\^my note] Here is an&#10;inline note.[^3]&#10;&#10;&gt; Notes can go in quotes.[^4]&#10;&#10;1. And in list items.[^5]&#10;&#10;This paragraph should not be part of the note, as it is not indented.&#10;&#10;[^1]: Here is the footnote. It can go anywhere after the footnote&#10; reference. It need not be placed at the end of the document.&#10;&#10;[^2]: Here’s the long note. This one contains multiple blocks.&#10;&#10; Subsequent blocks are indented to show that they belong to the&#10; footnote (as with list items).&#10;&#10; { &lt;code&gt; }&#10;&#10; If you want, you can indent every line, but you can also be lazy and&#10; just indent the first line of each block.&#10;&#10;[^3]: This is *easier* to type. Inline notes may contain&#10; [links](http://google.com) and `]` verbatim characters, as well as&#10; [bracketed text].&#10;&#10;[^4]: In quote.&#10;&#10;[^5]: In list.">
69+<outline text="Footnotes" _note="Here is a footnote reference,[^1] and another.[^2] This should *not* be&#10;a footnote reference, because it contains a space.\[\^my note\] Here is&#10;an inline note.[^3]&#10;&#10;&gt; Notes can go in quotes.[^4]&#10;&#10;1. And in list items.[^5]&#10;&#10;This paragraph should not be part of the note, as it is not indented.&#10;&#10;[^1]: Here is the footnote. It can go anywhere after the footnote&#10; reference. It need not be placed at the end of the document.&#10;&#10;[^2]: Here’s the long note. This one contains multiple blocks.&#10;&#10; Subsequent blocks are indented to show that they belong to the&#10; footnote (as with list items).&#10;&#10; { &lt;code&gt; }&#10;&#10; If you want, you can indent every line, but you can also be lazy and&#10; just indent the first line of each block.&#10;&#10;[^3]: This is *easier* to type. Inline notes may contain&#10; [links](http://google.com) and `]` verbatim characters, as well as&#10; \[bracketed text\].&#10;&#10;[^4]: In quote.&#10;&#10;[^5]: In list.">
7070</outline>
7171 </body>
7272</opml>