Skip to content

Commit 4a3d1a6

Browse files
facelessuserwaylan
authored andcommitted
Better inline code escaping (#533)
This aims to escape code in a more expected fashion. This handles when backticks are escaped and when the escapes before backticks are escaped.
1 parent c70b2c4 commit 4a3d1a6

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

markdown/inlinepatterns.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def build_inlinepatterns(md_instance, **kwargs):
103103
NOIMG = r'(?<!\!)'
104104

105105
# `e=f()` or ``e=f("`")``
106-
BACKTICK_RE = r'(?<!\\)(`+)(.+?)(?<!`)\2(?!`)'
106+
BACKTICK_RE = r'(?:(?<!\\)((?:\\{2})+)(?=`+)|(?<!\\)(`+)(.+?)(?<!`)\3(?!`))'
107107

108108
# \<
109109
ESCAPE_RE = r'\\(.)'
@@ -302,12 +302,16 @@ class BacktickPattern(Pattern):
302302
""" Return a `<code>` element containing the matching text. """
303303
def __init__(self, pattern):
304304
Pattern.__init__(self, pattern)
305-
self.tag = "code"
305+
self.ESCAPED_BSLASH = '%s%s%s' % (util.STX, ord('\\'), util.ETX)
306+
self.tag = 'code'
306307

307308
def handleMatch(self, m):
308-
el = util.etree.Element(self.tag)
309-
el.text = util.AtomicString(m.group(3).strip())
310-
return el
309+
if m.group(4):
310+
el = util.etree.Element(self.tag)
311+
el.text = util.AtomicString(m.group(4).strip())
312+
return el
313+
else:
314+
return m.group(2).replace('\\\\', self.ESCAPED_BSLASH)
311315

312316

313317
class DoubleTagPattern(SimpleTagPattern):

tests/extensions/extra/tables.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,23 @@ <h2>Table Tests</h2>
356356
<p>| Column1 | Column2 |
357357
| ------- || ------- |
358358
| row1 | row1 |
359-
| row2 | row2 |</p>
359+
| row2 | row2 |</p>
360+
<p>Test escaped code in Table</p>
361+
<table>
362+
<thead>
363+
<tr>
364+
<th>Should not be code</th>
365+
<th>Should be code</th>
366+
</tr>
367+
</thead>
368+
<tbody>
369+
<tr>
370+
<td>`Not code`</td>
371+
<td>\<code>code</code></td>
372+
</tr>
373+
<tr>
374+
<td>\`Not code\`</td>
375+
<td>\\<code>code</code></td>
376+
</tr>
377+
</tbody>
378+
</table>

tests/extensions/extra/tables.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ Escaped pipes in format row should not be a table
121121
| ------- \|| ------- |
122122
| row1 | row1 |
123123
| row2 | row2 |
124+
125+
Test escaped code in Table
126+
127+
Should not be code | Should be code
128+
------------------ | --------------
129+
\`Not code\` | \\`code`
130+
\\\`Not code\\\` | \\\\`code`

tests/misc/backtick-escape.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
<p>\`This should not be in code.\`
2-
`This also should not be in code.`
1+
<p>`This should not be in code.`
2+
\<code>This should be in code.\\</code>
3+
\`This should not be in code.\`
34
`And finally this should not be in code.`</p>

tests/misc/backtick-escape.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
\\`This should not be in code.\\`
2-
\`This also should not be in code.\`
1+
\`This should not be in code.\`
2+
\\`This should be in code.\\`
3+
\\\`This should not be in code.\\\`
34
\`And finally this should not be in code.`

0 commit comments

Comments
 (0)