Remove footnote references from a copy of the element, if any are present.
(root: etree.Element)
| 149 | |
| 150 | |
| 151 | def remove_fnrefs(root: etree.Element) -> etree.Element: |
| 152 | """ Remove footnote references from a copy of the element, if any are present. """ |
| 153 | # Remove footnote references, which look like this: `<sup id="fnref:1">...</sup>`. |
| 154 | # If there are no `sup` elements, then nothing to do. |
| 155 | if next(root.iter('sup'), None) is None: |
| 156 | return root |
| 157 | root = deepcopy(root) |
| 158 | # Find parent elements that contain `sup` elements. |
| 159 | for parent in root.findall('.//sup/..'): |
| 160 | carry_text = "" |
| 161 | for child in reversed(parent): # Reversed for the ability to mutate during iteration. |
| 162 | # Remove matching footnote references but carry any `tail` text to preceding elements. |
| 163 | if child.tag == 'sup' and child.get('id', '').startswith('fnref'): |
| 164 | carry_text = f'{child.tail or ""}{carry_text}' |
| 165 | parent.remove(child) |
| 166 | elif carry_text: |
| 167 | child.tail = f'{child.tail or ""}{carry_text}' |
| 168 | carry_text = "" |
| 169 | if carry_text: |
| 170 | parent.text = f'{parent.text or ""}{carry_text}' |
| 171 | return root |
| 172 | |
| 173 | |
| 174 | def nest_toc_tokens(toc_list): |
no outgoing calls
no test coverage detected
searching dependent graphs…