diff --git a/CHANGES.md b/CHANGES.md index 679868cc..4aee683e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ ## python-markdown2 2.4.9 (not yet released) - [pull #500] Add `` tag to html-classes extra +- [pull #501] Fix link patterns extra matching against internal hashes - [pull #502] Replace deprecated `optparse` with `argparse` - [pull #506] Fix `_uniform_outdent` failing with empty strings (issue #505) diff --git a/lib/markdown2.py b/lib/markdown2.py index 86d597c1..50d97215 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -2533,6 +2533,9 @@ def _do_link_patterns(self, text): for regex, repl in self.link_patterns: replacements = [] for match in regex.finditer(text): + if any(self._match_overlaps_substr(text, match, h) for h in link_from_hash): + continue + if hasattr(repl, "__call__"): href = repl(match) else: @@ -2642,6 +2645,19 @@ def _uniform_indent(self, text, indent, include_empty_lines=False): for line in text.splitlines(True) ) + @staticmethod + def _match_overlaps_substr(text, match, substr): + ''' + Checks if a regex match overlaps with a substring in the given text. + ''' + for instance in re.finditer(re.escape(substr), text): + start, end = instance.span() + if start <= match.start() <= end: + return True + if start <= match.end() <= end: + return True + return False + class MarkdownWithExtras(Markdown): """A markdowner class that enables most extras: diff --git a/test/tm-cases/link_patterns_hash_matching_issue287.html b/test/tm-cases/link_patterns_hash_matching_issue287.html new file mode 100644 index 00000000..7cee86e2 --- /dev/null +++ b/test/tm-cases/link_patterns_hash_matching_issue287.html @@ -0,0 +1 @@ +

this is a test issue #1234 with a test commit (addeddd) made by test @username more text

diff --git a/test/tm-cases/link_patterns_hash_matching_issue287.opts b/test/tm-cases/link_patterns_hash_matching_issue287.opts new file mode 100644 index 00000000..21062d75 --- /dev/null +++ b/test/tm-cases/link_patterns_hash_matching_issue287.opts @@ -0,0 +1,7 @@ +{"extras": ["link-patterns"], + "link_patterns": [ + (re.compile("#(\d+)", re.I), r"https://github.com/pyfa-org/Pyfa/issues/\1"), + (re.compile("@(\w+)", re.I), r"https://github.com/\1"), + (re.compile("([0-9a-f]{6,40})", re.I), r"https://github.com/pyfa-org/Pyfa/commit/\1") + ] +} diff --git a/test/tm-cases/link_patterns_hash_matching_issue287.text b/test/tm-cases/link_patterns_hash_matching_issue287.text new file mode 100644 index 00000000..b0b5f4b9 --- /dev/null +++ b/test/tm-cases/link_patterns_hash_matching_issue287.text @@ -0,0 +1 @@ +this is a test issue #1234 with a test commit (addeddd) made by test @username more text