Skip to content

Optimize raw HTML post-processor #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* DRY fix in `abbr` extension by introducing method `create_element` (#1483).
* Clean up test directory some removing some redundant tests and port
non-redundant cases to the newer test framework.
* Improved performance of the raw HTML post-processor (#1510).

### Fixed

Expand Down
40 changes: 14 additions & 26 deletions markdown/postprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

from __future__ import annotations

from collections import OrderedDict
from typing import TYPE_CHECKING, Any
from . import util
import re
Expand Down Expand Up @@ -73,37 +72,26 @@ class RawHtmlPostprocessor(Postprocessor):

def run(self, text: str) -> str:
""" Iterate over html stash and restore html. """
replacements = OrderedDict()
for i in range(self.md.htmlStash.html_counter):
html = self.stash_to_string(self.md.htmlStash.rawHtmlBlocks[i])
if self.isblocklevel(html):
replacements["<p>{}</p>".format(
self.md.htmlStash.get_placeholder(i))] = html
replacements[self.md.htmlStash.get_placeholder(i)] = html

def substitute_match(m: re.Match[str]) -> str:
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:
if key := m.group(1):
wrapped = True
else:
key = m.group(2)
wrapped = False
if (key := int(key)) >= self.md.htmlStash.html_counter:
return m.group(0)
html = self.stash_to_string(self.md.htmlStash.rawHtmlBlocks[key])
if not wrapped or self.isblocklevel(html):
return pattern.sub(substitute_match, html)
return pattern.sub(substitute_match, f"<p>{html}</p>")

if self.md.htmlStash.html_counter:
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)
return pattern.sub(substitute_match, text)
else:
return text

if processed_text == text:
return processed_text
else:
return self.run(processed_text)

def isblocklevel(self, html: str) -> bool:
""" Check is block of HTML is block-level. """
m = self.BLOCK_LEVEL_REGEX.match(html)
Expand Down
Loading