|
28 | 28 |
|
29 | 29 | from __future__ import annotations
|
30 | 30 |
|
31 |
| -from collections import OrderedDict |
32 | 31 | from typing import TYPE_CHECKING, Any
|
33 | 32 | from . import util
|
34 | 33 | import re
|
@@ -73,37 +72,26 @@ class RawHtmlPostprocessor(Postprocessor):
|
73 | 72 |
|
74 | 73 | def run(self, text: str) -> str:
|
75 | 74 | """ Iterate over html stash and restore html. """
|
76 |
| - replacements = OrderedDict() |
77 |
| - for i in range(self.md.htmlStash.html_counter): |
78 |
| - html = self.stash_to_string(self.md.htmlStash.rawHtmlBlocks[i]) |
79 |
| - if self.isblocklevel(html): |
80 |
| - replacements["<p>{}</p>".format( |
81 |
| - self.md.htmlStash.get_placeholder(i))] = html |
82 |
| - replacements[self.md.htmlStash.get_placeholder(i)] = html |
83 |
| - |
84 | 75 | def substitute_match(m: re.Match[str]) -> str:
|
85 |
| - key = m.group(0) |
86 |
| - |
87 |
| - if key not in replacements: |
88 |
| - if key[3:-4] in replacements: |
89 |
| - return f'<p>{ replacements[key[3:-4]] }</p>' |
90 |
| - else: |
91 |
| - return key |
92 |
| - |
93 |
| - return replacements[key] |
94 |
| - |
95 |
| - if replacements: |
| 76 | + if key := m.group(1): |
| 77 | + wrapped = True |
| 78 | + else: |
| 79 | + key = m.group(2) |
| 80 | + wrapped = False |
| 81 | + if (key := int(key)) >= len(self.md.htmlStash.rawHtmlBlocks): |
| 82 | + return m.group(0) |
| 83 | + html = self.stash_to_string(self.md.htmlStash.rawHtmlBlocks[key]) |
| 84 | + if self.isblocklevel(html) or not wrapped: |
| 85 | + return pattern.sub(substitute_match, html) |
| 86 | + return pattern.sub(substitute_match, f"<p>{html}</p>") |
| 87 | + |
| 88 | + if self.md.htmlStash.html_counter: |
96 | 89 | base_placeholder = util.HTML_PLACEHOLDER % r'([0-9]+)'
|
97 |
| - pattern = re.compile(f'<p>{ base_placeholder }</p>|{ base_placeholder }') |
98 |
| - processed_text = pattern.sub(substitute_match, text) |
| 90 | + pattern = re.compile(f'<p>{base_placeholder}</p>|{base_placeholder}') |
| 91 | + return pattern.sub(substitute_match, text) |
99 | 92 | else:
|
100 | 93 | return text
|
101 | 94 |
|
102 |
| - if processed_text == text: |
103 |
| - return processed_text |
104 |
| - else: |
105 |
| - return self.run(processed_text) |
106 |
| - |
107 | 95 | def isblocklevel(self, html: str) -> bool:
|
108 | 96 | """ Check is block of HTML is block-level. """
|
109 | 97 | m = self.BLOCK_LEVEL_REGEX.match(html)
|
|
0 commit comments