diff options
author | Waylan Limberg <waylan.limberg@icloud.com> | 2020-12-08 11:30:45 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-08 11:30:45 -0500 |
commit | 5a7bbaf4c00224a393ddc818b7fc1917c8064840 (patch) | |
tree | a50254e4b3bd3dae7024ad699aad404d85f51b59 | |
parent | 8e7528fa5c98bf4652deb13206d6e6241d61630b (diff) | |
download | python-markdown-5a7bbaf4c00224a393ddc818b7fc1917c8064840.tar.gz |
Use simplified regex for html placeholders (#1086)
Co-authored-by: Reilly Raab <raabrp@gmail.com>
-rw-r--r-- | docs/change_log/index.md | 1 | ||||
-rw-r--r-- | markdown/postprocessors.py | 16 | ||||
-rw-r--r-- | tests/test_syntax/blocks/test_html_blocks.py | 11 |
3 files changed, 26 insertions, 2 deletions
diff --git a/docs/change_log/index.md b/docs/change_log/index.md index 0069c22..90ed1a3 100644 --- a/docs/change_log/index.md +++ b/docs/change_log/index.md @@ -8,6 +8,7 @@ Under development: version 3.3.4 (a bug-fix release). * Properly parse unclosed tags in code spans (#1066). * Properly parse processing instructions in md_in_html (#1070). * Properly parse code spans in md_in_html (#1069). +* Simplified regex for HTML placeholders (#928) addressing (#932). Oct 25, 2020: version 3.3.3 (a bug-fix release). diff --git a/markdown/postprocessors.py b/markdown/postprocessors.py index 2e68cd9..2e572f6 100644 --- a/markdown/postprocessors.py +++ b/markdown/postprocessors.py @@ -75,9 +75,21 @@ class RawHtmlPostprocessor(Postprocessor): self.md.htmlStash.get_placeholder(i))] = html replacements[self.md.htmlStash.get_placeholder(i)] = html + def substitute_match(m): + key = m.group(0) + + if key not in replacements: + if key[3:-4] in replacements: + return f'<p>{ replacements[key[3:-4]] }</p>' + else: + return key + + return replacements[key] + if replacements: - pattern = re.compile("|".join(re.escape(k) for k in replacements)) - processed_text = pattern.sub(lambda m: replacements[m.group(0)], text) + base_placeholder = util.HTML_PLACEHOLDER % r'([0-9]+)' + pattern = re.compile(f'<p>{ base_placeholder }</p>|{ base_placeholder }') + processed_text = pattern.sub(substitute_match, text) else: return text diff --git a/tests/test_syntax/blocks/test_html_blocks.py b/tests/test_syntax/blocks/test_html_blocks.py index 0fdb3e5..0ee47b6 100644 --- a/tests/test_syntax/blocks/test_html_blocks.py +++ b/tests/test_syntax/blocks/test_html_blocks.py @@ -21,6 +21,7 @@ License: BSD (see LICENSE.md for details). """ from markdown.test_tools import TestCase +import markdown class TestHTMLBlocks(TestCase): @@ -1606,3 +1607,13 @@ class TestHTMLBlocks(TestCase): """ ) ) + + def test_placeholder_in_source(self): + # This should never occur, but third party extensions could create weird edge cases. + md = markdown.Markdown() + # Ensure there is an htmlstash so relevant code (nested in `if replacements`) is run. + md.htmlStash.store('foo') + # Run with a placeholder which is not in the stash + placeholder = md.htmlStash.get_placeholder(md.htmlStash.html_counter + 1) + result = md.postprocessors['raw_html'].run(placeholder) + self.assertEqual(placeholder, result) |