MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / HtmlInlineProcessor

Class HtmlInlineProcessor

markdown/inlinepatterns.py:523–560  ·  view source on GitHub ↗

Store raw inline html and return a placeholder.

Source from the content-addressed store, hash-verified

521
522
523class HtmlInlineProcessor(InlineProcessor):
524 """ Store raw inline html and return a placeholder. """
525 def handleMatch(self, m: re.Match[str], data: str) -> tuple[str, int, int]:
526 """ Store the text of `group(1)` of a pattern and return a placeholder string. """
527 rawhtml = self.backslash_unescape(self.unescape(m.group(1)))
528 place_holder = self.md.htmlStash.store(rawhtml)
529 return place_holder, m.start(0), m.end(0)
530
531 def unescape(self, text: str) -> str:
532 """ Return unescaped text given text with an inline placeholder. """
533 try:
534 stash = self.md.treeprocessors['inline'].stashed_nodes
535 except KeyError: # pragma: no cover
536 return text
537
538 def get_stash(m: re.Match[str]) -> str:
539 id = m.group(1)
540 value = stash.get(id)
541 if value is not None:
542 if isinstance(value, etree.Element):
543 # Ensure we don't have a placeholder inside a placeholder
544 return self.unescape(self.md.serializer(value))
545 else:
546 return r'\%s' % value
547
548 return util.INLINE_PLACEHOLDER_RE.sub(get_stash, text)
549
550 def backslash_unescape(self, text: str) -> str:
551 """ Return text with backslash escapes undone (backslashes are restored). """
552 try:
553 RE = self.md.treeprocessors['unescape'].RE
554 except KeyError: # pragma: no cover
555 return text
556
557 def _unescape(m: re.Match[str]) -> str:
558 return chr(int(m.group(1)))
559
560 return RE.sub(_unescape, text)
561
562
563class EvaluatedText(str):

Callers 2

build_inlinepatternsFunction · 0.85
extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected