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

Class UnescapeTreeprocessor

markdown/treeprocessors.py:454–476  ·  view source on GitHub ↗

Restore escaped chars

Source from the content-addressed store, hash-verified

452
453
454class UnescapeTreeprocessor(Treeprocessor):
455 """ Restore escaped chars """
456
457 RE = re.compile(r'{}(\d+){}'.format(util.STX, util.ETX))
458
459 def _unescape(self, m: re.Match[str]) -> str:
460 return chr(int(m.group(1)))
461
462 def unescape(self, text: str) -> str:
463 return self.RE.sub(self._unescape, text)
464
465 def run(self, root: etree.Element) -> None:
466 """ Loop over all elements and unescape all text. """
467 for elem in root.iter():
468 # Unescape text content
469 if elem.text and not elem.tag == 'code':
470 elem.text = self.unescape(elem.text)
471 # Unescape tail content
472 if elem.tail:
473 elem.tail = self.unescape(elem.tail)
474 # Unescape attribute values
475 for key, value in elem.items():
476 elem.set(key, self.unescape(value))

Callers 2

build_treeprocessorsFunction · 0.85
unescapeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected