Skip to content

Commit

Permalink
Use simplified regex for html placeholders (#1086)
Browse files Browse the repository at this point in the history
Co-authored-by: Reilly Raab <[email protected]>
  • Loading branch information
waylan and raabrp committed Dec 8, 2020
1 parent 8e7528f commit 5a7bbaf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/change_log/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
16 changes: 14 additions & 2 deletions markdown/postprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,21 @@ def run(self, text):
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

Expand Down
11 changes: 11 additions & 0 deletions tests/test_syntax/blocks/test_html_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

from markdown.test_tools import TestCase
import markdown


class TestHTMLBlocks(TestCase):
Expand Down Expand Up @@ -1606,3 +1607,13 @@ def test_hr_with_content(self):
"""
)
)

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)

0 comments on commit 5a7bbaf

Please sign in to comment.