Remove footnote references from a copy of the element, if any are present.
(root: etree.Element)
| 171 | |
| 172 | |
| 173 | def remove_fnrefs(root: etree.Element) -> etree.Element: |
| 174 | """ Remove footnote references from a copy of the element, if any are present. """ |
| 175 | # Remove footnote references, which look like this: `<sup id="fnref:1">...</sup>`. |
| 176 | # If there are no `sup` elements, then nothing to do. |
| 177 | if next(root.iter('sup'), None) is None: |
| 178 | return root |
| 179 | root = deepcopy(root) |
| 180 | # Find parent elements that contain `sup` elements. |
| 181 | for parent in root.findall('.//sup/..'): |
| 182 | carry_text = "" |
| 183 | for child in reversed(parent): # Reversed for the ability to mutate during iteration. |
| 184 | # Remove matching footnote references but carry any `tail` text to preceding elements. |
| 185 | if child.tag == 'sup' and child.get('id', '').startswith('fnref'): |
| 186 | carry_text = f'{child.tail or ""}{carry_text}' |
| 187 | parent.remove(child) |
| 188 | elif carry_text: |
| 189 | child.tail = f'{child.tail or ""}{carry_text}' |
| 190 | carry_text = "" |
| 191 | if carry_text: |
| 192 | parent.text = f'{parent.text or ""}{carry_text}' |
| 193 | return root |
| 194 | |
| 195 | |
| 196 | def nest_toc_tokens(toc_list): |