From 7b259b9d2daa5b14465c18ebaf7fee2b5e0ebfde Mon Sep 17 00:00:00 2001 From: Alex Elzenaar Date: Fri, 25 Jan 2019 10:54:00 +1300 Subject: [PATCH 1/2] Don't auto link patterns surrounded by """. This allows the prevention of `[WikiWikiWeb](http://wiki.c2.com/?WikiWikiWeb)` being transformed to `WikiWikiWeb` when the pattern linker sees the second WikiCase word by surrounding it with triple quotes: so `[WikiWikiWeb](http://wiki.c2.com/?"""WikiWikiWeb""")` is now transformed to `WikiWikiWeb`. The function responsible, _do_link_patterns(), already checked for the cases that the pattern was inside square brackets, or was preceeded by `](` or suffixed by `")`, and skips expanding in these cases. This addition simply adds a check that the pattern is not surrounded by triple quotes; the triple quotes are stripped from the output. --- lib/markdown2.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index c572f99f..9e81931b 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -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): @@ -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): @@ -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 and : From 06601574e45439d3ed7949c9c4215821d1b25807 Mon Sep 17 00:00:00 2001 From: Alex Elzenaar Date: Fri, 25 Jan 2019 11:34:23 +1300 Subject: [PATCH 2/2] Add tests for triple quotes feature (from 7b259b9) --- test/tm-cases/link_patterns_escape.html | 9 +++++++++ test/tm-cases/link_patterns_escape.opts | 8 ++++++++ test/tm-cases/link_patterns_escape.text | 9 +++++++++ 3 files changed, 26 insertions(+) create mode 100644 test/tm-cases/link_patterns_escape.html create mode 100644 test/tm-cases/link_patterns_escape.opts create mode 100644 test/tm-cases/link_patterns_escape.text diff --git a/test/tm-cases/link_patterns_escape.html b/test/tm-cases/link_patterns_escape.html new file mode 100644 index 00000000..20c42eb4 --- /dev/null +++ b/test/tm-cases/link_patterns_escape.html @@ -0,0 +1,9 @@ +

Recipe 123 and a link to Komodo bug 234 are related.

+ +

This is a link which has a pattern inside the url that shouldn't expand.

+ +

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."""

diff --git a/test/tm-cases/link_patterns_escape.opts b/test/tm-cases/link_patterns_escape.opts new file mode 100644 index 00000000..440d5a6d --- /dev/null +++ b/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))), + ], +} + diff --git a/test/tm-cases/link_patterns_escape.text b/test/tm-cases/link_patterns_escape.text new file mode 100644 index 00000000..e4cf48af --- /dev/null +++ b/test/tm-cases/link_patterns_escape.text @@ -0,0 +1,9 @@ +"""Recipe 123""" and a link to """Komodo bug 234""" are related. + +This is a link which has a pattern inside the url that shouldn't expand. + +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."""