git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: improved escaping.
d20152eJohn MacFarlane
src/Text/Pandoc/Writers/Markdown.hs+3−2
@@ -246,7 +246,8 @@ noteToMarkdown opts num blocks = do
246-- | Escape special characters for Markdown.
246-- | Escape special characters for Markdown.
247escapeString :: WriterOptions -> String -> String
247escapeString :: WriterOptions -> String -> String
248escapeString opts = escapeStringUsing markdownEscapes
248escapeString opts = escapeStringUsing markdownEscapes
249+ where markdownEscapes = ('<', "&lt;") : ('>', "&gt;") :
249− where markdownEscapes = backslashEscapes specialChars
250+ backslashEscapes specialChars
250 specialChars =
251 specialChars =
251 (if isEnabled Ext_superscript opts
252 (if isEnabled Ext_superscript opts
252 then ('^':)
253 then ('^':)
@@ -257,7 +258,7 @@ escapeString opts = escapeStringUsing markdownEscapes
257 (if isEnabled Ext_tex_math_dollars opts
258 (if isEnabled Ext_tex_math_dollars opts
258 then ('$':)
259 then ('$':)
259 else id) $
260 else id) $
260− "\\`*_<>#"
261+ "\\`*_[]#"
261
262
262-- | Construct table of contents from list of header blocks.
263-- | Construct table of contents from list of header blocks.
263tableOfContents :: WriterOptions -> [Block] -> Doc
264tableOfContents :: WriterOptions -> [Block] -> Doc
tests/Tests/Writers/Markdown.hs+2−2
@@ -84,13 +84,13 @@ shortcutLinkRefsTests =
84 ]
84 ]
85 , "Reference link is followed by text in brackets"
85 , "Reference link is followed by text in brackets"
86 =: (para ((link "/url" "" "link") <> "[text in brackets]"))
86 =: (para ((link "/url" "" "link") <> "[text in brackets]"))
87− =?> unlines [ "[link][][text in brackets]"
87+ =?> unlines [ "[link][]\\[text in brackets\\]"
88 , ""
88 , ""
89 , " [link]: /url"
89 , " [link]: /url"
90 ]
90 ]
91 , "Reference link is followed by space and text in brackets"
91 , "Reference link is followed by space and text in brackets"
92 =: (para ((link "/url" "" "link") <> " [text in brackets]"))
92 =: (para ((link "/url" "" "link") <> " [text in brackets]"))
93− =?> unlines [ "[link][] [text in brackets]"
93+ =?> unlines [ "[link][] \\[text in brackets\\]"
94 , ""
94 , ""
95 , " [link]: /url"
95 , " [link]: /url"
96 ]
96 ]
tests/writer.markdown+11−11
@@ -80,7 +80,7 @@ E-mail style:
80>
80>
81> > nested
81> > nested
82
82
83−This should not be a block quote: 2 \> 1.
83+This should not be a block quote: 2 &gt; 1.
84
84
85And a following paragraph.
85And a following paragraph.
86
86
@@ -581,9 +581,9 @@ AT&T is another way to write it.
581
581
582This & that.
582This & that.
583
583
584−4 \< 5.
584+4 &lt; 5.
585
585
586−6 \> 5.
586+6 &gt; 5.
587
587
588Backslash: \\
588Backslash: \\
589
589
@@ -597,15 +597,15 @@ Left brace: {
597
597
598Right brace: }
598Right brace: }
599
599
600−Left bracket: [
600+Left bracket: \[
601
601
602−Right bracket: ]
602+Right bracket: \]
603
603
604Left paren: (
604Left paren: (
605
605
606Right paren: )
606Right paren: )
607
607
608−Greater-than: \>
608+Greater-than: &gt;
609
609
610Hash: \#
610Hash: \#
611
611
@@ -652,7 +652,7 @@ Foo [bar](/url/).
652
652
653Foo [bar](/url/).
653Foo [bar](/url/).
654
654
655−With [embedded [brackets]](/url/).
655+With [embedded \[brackets\]](/url/).
656
656
657[b](/url/) by itself should be a link.
657[b](/url/) by itself should be a link.
658
658
@@ -662,7 +662,7 @@ Indented [twice](/url).
662
662
663Indented [thrice](/url).
663Indented [thrice](/url).
664
664
665−This should [not][] be a link.
665+This should \[not\]\[\] be a link.
666
666
667 [not]: /url
667 [not]: /url
668
668
@@ -716,8 +716,8 @@ Footnotes
716=========
716=========
717
717
718Here is a footnote reference,[^1] and another.[^2] This should *not* be a
718Here 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
719+footnote reference, because it contains a space.\[\^my note\] Here is an
720−note.[^3]
720+inline note.[^3]
721
721
722> Notes can go in quotes.[^4]
722> Notes can go in quotes.[^4]
723
723
@@ -740,7 +740,7 @@ This paragraph should not be part of the note, as it is not indented.
740
740
741[^3]: This is *easier* to type. Inline notes may contain
741[^3]: This is *easier* to type. Inline notes may contain
742 [links](http://google.com) and `]` verbatim characters, as well as
742 [links](http://google.com) and `]` verbatim characters, as well as
743− [bracketed text].
743+ \[bracketed text\].
744
744
745[^4]: In quote.
745[^4]: In quote.
746
746
tests/writer.opml+4−4
@@ -26,7 +26,7 @@
26</outline>
26</outline>
27<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;------------------------------------------------------------------------">
27<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;------------------------------------------------------------------------">
28</outline>
28</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;------------------------------------------------------------------------">
30</outline>
30</outline>
31<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;------------------------------------------------------------------------">
31<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;------------------------------------------------------------------------">
32</outline>
32</outline>
@@ -52,12 +52,12 @@
52</outline>
52</outline>
53<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;------------------------------------------------------------------------">
53<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;------------------------------------------------------------------------">
54</outline>
54</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;------------------------------------------------------------------------">
56</outline>
56</outline>
57<outline text="Links">
57<outline text="Links">
58 <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]().">
58 <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]().">
59 </outline>
59 </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;).">
61 </outline>
61 </outline>
62 <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).">
62 <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).">
63 </outline>
63 </outline>
@@ -66,7 +66,7 @@
66</outline>
66</outline>
67<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;------------------------------------------------------------------------">
67<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;------------------------------------------------------------------------">
68</outline>
68</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.">
70</outline>
70</outline>
71 </body>
71 </body>
72</opml>
72</opml>