Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions markdown/inlinepatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def build_inlinepatterns(md_instance, **kwargs):
NOIMG = r'(?<!\!)'

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

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

def handleMatch(self, m):
el = util.etree.Element(self.tag)
el.text = util.AtomicString(m.group(3).strip())
return el
if m.group(4):
el = util.etree.Element(self.tag)
el.text = util.AtomicString(m.group(4).strip())
return el
else:
return m.group(2).replace('\\\\', self.ESCAPED_BSLASH)


class DoubleTagPattern(SimpleTagPattern):
Expand Down
21 changes: 20 additions & 1 deletion tests/extensions/extra/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,23 @@ <h2>Table Tests</h2>
<p>| Column1 | Column2 |
| ------- || ------- |
| row1 | row1 |
| row2 | row2 |</p>
| row2 | row2 |</p>
<p>Test escaped code in Table</p>
<table>
<thead>
<tr>
<th>Should not be code</th>
<th>Should be code</th>
</tr>
</thead>
<tbody>
<tr>
<td>`Not code`</td>
<td>\<code>code</code></td>
</tr>
<tr>
<td>\`Not code\`</td>
<td>\\<code>code</code></td>
</tr>
</tbody>
</table>
7 changes: 7 additions & 0 deletions tests/extensions/extra/tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ Escaped pipes in format row should not be a table
| ------- \|| ------- |
| row1 | row1 |
| row2 | row2 |

Test escaped code in Table

Should not be code | Should be code
------------------ | --------------
\`Not code\` | \\`code`
\\\`Not code\\\` | \\\\`code`
5 changes: 3 additions & 2 deletions tests/misc/backtick-escape.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<p>\`This should not be in code.\`
`This also should not be in code.`
<p>`This should not be in code.`
\<code>This should be in code.\\</code>
\`This should not be in code.\`
`And finally this should not be in code.`</p>
5 changes: 3 additions & 2 deletions tests/misc/backtick-escape.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
\\`This should not be in code.\\`
\`This also should not be in code.\`
\`This should not be in code.\`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that makes no sense to me. Seems appropriate to change it. Unfortunately, the old commit message is less that helpful and it references an old issue tracker which is no longer online. This is why I like commit messages that stand on their own. But I digress...

This change might break some peoples' pages, but I'm okay with that as it is clearly an unexpected behavior and should be considered a bug.

\\`This should be in code.\\`
\\\`This should not be in code.\\\`
\`And finally this should not be in code.`