Recursively iterate over elements, run regex on text and wrap matches in `abbr` tags.
(self, el: etree.Element, parent: etree.Element | None = None)
| 103 | return abbr |
| 104 | |
| 105 | def iter_element(self, el: etree.Element, parent: etree.Element | None = None) -> None: |
| 106 | ''' Recursively iterate over elements, run regex on text and wrap matches in `abbr` tags. ''' |
| 107 | for child in reversed(el): |
| 108 | self.iter_element(child, el) |
| 109 | if text := el.text: |
| 110 | if not isinstance(text, AtomicString): |
| 111 | for m in reversed(list(self.RE.finditer(text))): |
| 112 | if self.abbrs[m.group(0)]: |
| 113 | abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), text[m.end():]) |
| 114 | el.insert(0, abbr) |
| 115 | text = text[:m.start()] |
| 116 | el.text = text |
| 117 | if parent is not None and el.tail: |
| 118 | tail = el.tail |
| 119 | index = list(parent).index(el) + 1 |
| 120 | if not isinstance(tail, AtomicString): |
| 121 | for m in reversed(list(self.RE.finditer(tail))): |
| 122 | abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), tail[m.end():]) |
| 123 | parent.insert(index, abbr) |
| 124 | tail = tail[:m.start()] |
| 125 | el.tail = tail |
| 126 | |
| 127 | def run(self, root: etree.Element) -> etree.Element | None: |
| 128 | ''' Step through tree to find known abbreviations. ''' |