Skip to content

Commit

Permalink
Merge pull request #322 from aelzenaar/master
Browse files Browse the repository at this point in the history
Don't auto link patterns surrounded by """.
  • Loading branch information
nicholasserra committed Mar 5, 2019
2 parents a3c378d + 0660157 commit 5667462
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/markdown2.py
Expand Up @@ -1772,7 +1772,7 @@ def _code_block_sub(self, match, is_fenced_code_block=False):
lexer_name = lexer_name[3:].strip()
codeblock = rest.lstrip("\n") # Remove lexer declaration line.
formatter_opts = self.extras['code-color'] or {}

# Use pygments only if not using the highlightjs-lang extra
if lexer_name and "highlightjs-lang" not in self.extras:
def unhash_code(codeblock):
Expand Down Expand Up @@ -2146,7 +2146,7 @@ def _encode_amps_and_angles(self, text):
def _encode_incomplete_tags(self, text):
if self.safe_mode not in ("replace", "escape"):
return text

return self._incomplete_tags_re.sub("<\\1", text)

def _encode_backslash_escapes(self, text):
Expand Down Expand Up @@ -2218,6 +2218,11 @@ def _do_link_patterns(self, text):
if text[start - 2:start] == '](' or text[end:end + 2] == '")':
continue

# Do not match against links which are escaped.
if text[start - 3:start] == '"""' and text[end:end + 3] == '"""':
text = text[:start - 3] + text[start:end] + text[end + 3:]
continue

escaped_href = (
href.replace('"', '"') # b/c of attr quote
# To avoid markdown <em> and <strong>:
Expand Down
9 changes: 9 additions & 0 deletions test/tm-cases/link_patterns_escape.html
@@ -0,0 +1,9 @@
<p>Recipe 123 and <a href="http://bugs.activestate.com/show_bug.cgi?id=234">a link to Komodo bug 234</a> are related.</p>

<p><a href="http://example.org/Recipe 123">This is a link which has a pattern inside the url that shouldn't expand.</a></p>

<p>Matched pattern is escaped: PEP 42 might be related too.</p>

<p>Triple quotes not surrounding a matched pattern: PEP """42""" might be related too.</p>

<p>"""This is some random text which should not be touched."""</p>
8 changes: 8 additions & 0 deletions test/tm-cases/link_patterns_escape.opts
@@ -0,0 +1,8 @@
{"extras": ["link-patterns"],
"link_patterns": [
(re.compile("recipe\s+(\d+)", re.I), r"http://code.activestate.com/recipes/\1/"),
(re.compile("(?:komodo\s+)?bug\s+(\d+)", re.I), r"http://bugs.activestate.com/show_bug.cgi?id=\1"),
(re.compile("PEP\s+(\d+)", re.I), lambda m: "http://www.python.org/dev/peps/pep-%04d/" % int(m.group(1))),
],
}

9 changes: 9 additions & 0 deletions test/tm-cases/link_patterns_escape.text
@@ -0,0 +1,9 @@
"""Recipe 123""" and <a href="http://bugs.activestate.com/show_bug.cgi?id=234">a link to """Komodo bug 234"""</a> are related.

<a href="http://example.org/"""Recipe 123"""">This is a link which has a pattern inside the url that shouldn't expand.</a>

Matched pattern is escaped: """PEP 42""" might be related too.

Triple quotes not surrounding a matched pattern: PEP """42""" might be related too.

"""This is some random text which should not be touched."""

0 comments on commit 5667462

Please sign in to comment.