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

Class HtmlInlineProcessor

markdown/inlinepatterns.py:522–559  ·  view source on GitHub ↗

Store raw inline html and return a placeholder.

Source from the content-addressed store, hash-verified

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

Callers 2

build_inlinepatternsFunction · 0.85
extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected