Restore raw html to the document.
| 66 | |
| 67 | |
| 68 | class RawHtmlPostprocessor(Postprocessor): |
| 69 | """ Restore raw html to the document. """ |
| 70 | |
| 71 | BLOCK_LEVEL_REGEX = re.compile(r'^\<\/?([^ >]+)') |
| 72 | |
| 73 | def run(self, text: str) -> str: |
| 74 | """ Iterate over html stash and restore html. """ |
| 75 | def substitute_match(m: re.Match[str]) -> str: |
| 76 | if key := m.group(1): |
| 77 | wrapped = True |
| 78 | else: |
| 79 | key = m.group(2) |
| 80 | wrapped = False |
| 81 | if (key := int(key)) >= self.md.htmlStash.html_counter: |
| 82 | return m.group(0) |
| 83 | html = self.stash_to_string(self.md.htmlStash.rawHtmlBlocks[key]) |
| 84 | if not wrapped or self.isblocklevel(html): |
| 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: |
| 89 | base_placeholder = util.HTML_PLACEHOLDER % r'([0-9]+)' |
| 90 | pattern = re.compile(f'<p>{ base_placeholder }</p>|{ base_placeholder }') |
| 91 | return pattern.sub(substitute_match, text) |
| 92 | else: |
| 93 | return text |
| 94 | |
| 95 | def isblocklevel(self, html: str) -> bool: |
| 96 | """ Check is block of HTML is block-level. """ |
| 97 | m = self.BLOCK_LEVEL_REGEX.match(html) |
| 98 | if m: |
| 99 | if m.group(1)[0] in ('!', '?', '@', '%'): |
| 100 | # Comment, PHP etc... |
| 101 | return True |
| 102 | return self.md.is_block_level(m.group(1)) |
| 103 | return False |
| 104 | |
| 105 | def stash_to_string(self, text: str) -> str: |
| 106 | """ Convert a stashed object to a string. """ |
| 107 | return str(text) |
| 108 | |
| 109 | |
| 110 | class AndSubstitutePostprocessor(Postprocessor): |
no outgoing calls
no test coverage detected
searching dependent graphs…