Restore escaped chars
| 452 | |
| 453 | |
| 454 | class 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)) |
no outgoing calls
no test coverage detected