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

Function remove_fnrefs

markdown/extensions/toc.py:173–193  ·  view source on GitHub ↗

Remove footnote references from a copy of the element, if any are present.

(root: etree.Element)

Source from the content-addressed store, hash-verified

171
172
173def 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
196def nest_toc_tokens(toc_list):

Callers 1

runMethod · 0.85

Calls 1

removeMethod · 0.80

Tested by

no test coverage detected