Markdown writer: update escaping rules for \.

We now escape `\` only if `raw_tex` is enabled or
it is followed by a non-alphanumeric.
This commit is contained in:
John MacFarlane 2022-03-07 10:46:07 -08:00
parent abffe63274
commit 9b5ec100e5
3 changed files with 15 additions and 20 deletions

View file

@ -68,8 +68,16 @@ escapeText opts = T.pack . go' . T.unpack
go [] = []
go (c:cs) =
case c of
_ | c `elem` ['\\','`','*','_','[',']'] ->
'\\':c:go cs
'[' -> '\\':c:go cs
']' -> '\\':c:go cs
'`' -> '\\':c:go cs
'*' -> '\\':c:go cs
'_' -> '\\':c:go cs
'\\' | isEnabled Ext_raw_tex opts -> '\\':c:go cs
| otherwise ->
case cs of -- don't escape \ if we don't have to:
d:_ | isAlphaNum d -> c:go cs
_ -> '\\':c:go cs
'>' | isEnabled Ext_all_symbols_escapable opts -> '\\':'>':go cs
| otherwise -> ">" ++ go cs
'<' | isEnabled Ext_all_symbols_escapable opts -> '\\':'<':go cs

View file

@ -1,24 +1,11 @@
# Raw TeX blocks in CommonMark with and without raw_tex
This command comes through as regular text:
```
% pandoc -f latex+raw_tex -t commonmark-raw_tex
\someunknowncommand
Hello.
% pandoc -f latex+raw_tex -t native
\maketitle
^D
Hello.
[ RawBlock (Format "latex") "\\maketitle" ]
```
```
% pandoc -f latex+raw_tex -t commonmark+raw_tex
\someunknowncommand
Hello.
^D
\someunknowncommand
Hello.
```
```
% pandoc -f latex -t native

View file

@ -50,7 +50,7 @@
</outline>
<outline text="Smart quotes, ellipses, dashes" _note="“Hello,” said the spider. “Shelob is my name.”&#10;&#10;A, B, and C are letters.&#10;&#10;Oak, elm, and beech are names of trees. So is pine.&#10;&#10;He said, “I want to go.”’ Were you alive in the 70s?&#10;&#10;Here is some quoted `code` and a “[quoted&#10;link](http://example.com/?foo=1&amp;bar=2)”.&#10;&#10;Some dashes: one—two — three—four — five.&#10;&#10;Dashes between numbers: 57, 25566, 19871999.&#10;&#10;Ellipses…and…and….&#10;&#10;------------------------------------------------------------------------">
</outline>
<outline text="LaTeX" _note="- &#10;- 2+2=4&#10;- *x* ∈ *y*&#10;- *α* ∧ *ω*&#10;- 223&#10;- *p*-Tree&#10;- Heres some display math:&#10; $$\\frac{d}{dx}f(x)=\\lim\_{h\\to 0}\\frac{f(x+h)-f(x)}{h}$$&#10;- Heres one that has a line break in it: *α*+*ω*×*x*².&#10;&#10;These shouldnt 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” is&#10; emphasized.)&#10;- Shoes ($20) and socks ($5).&#10;- Escaped `$`: $73 *this should be emphasized* 23$.&#10;&#10;Heres a LaTeX table:&#10;&#10;------------------------------------------------------------------------">
<outline text="LaTeX" _note="- &#10;- 2+2=4&#10;- *x* ∈ *y*&#10;- *α* ∧ *ω*&#10;- 223&#10;- *p*-Tree&#10;- Heres some display math:&#10; $$\frac{d}{dx}f(x)=\lim\_{h\to 0}\frac{f(x+h)-f(x)}{h}$$&#10;- Heres one that has a line break in it: *α*+*ω*×*x*².&#10;&#10;These shouldnt 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” is&#10; emphasized.)&#10;- Shoes ($20) and socks ($5).&#10;- Escaped `$`: $73 *this should be emphasized* 23$.&#10;&#10;Heres a LaTeX table:&#10;&#10;------------------------------------------------------------------------">
</outline>
<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;------------------------------------------------------------------------">
</outline>