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

Class FootnoteReorderingProcessor

markdown/extensions/footnotes.py:452–487  ·  view source on GitHub ↗

Reorder list items in the footnotes div.

Source from the content-addressed store, hash-verified

450
451
452class FootnoteReorderingProcessor(Treeprocessor):
453 """ Reorder list items in the footnotes div. """
454
455 def __init__(self, footnotes: FootnoteExtension):
456 self.footnotes = footnotes
457
458 def run(self, root: etree.Element) -> None:
459 if not self.footnotes.footnotes:
460 return
461 if self.footnotes.footnote_order != list(self.footnotes.footnotes.keys()):
462 for div in root.iter('div'):
463 if div.attrib.get('class', '') == 'footnote':
464 self.reorder_footnotes(div)
465 break
466
467 def reorder_footnotes(self, parent: etree.Element) -> None:
468 old_list = parent.find('ol')
469 parent.remove(old_list)
470 items = old_list.findall('li')
471
472 def order_by_id(li) -> int:
473 id = li.attrib.get('id', '').split(self.footnotes.get_separator(), 1)[-1]
474 return (
475 self.footnotes.footnote_order.index(id)
476 if id in self.footnotes.footnote_order
477 else len(self.footnotes.footnotes)
478 )
479
480 items = sorted(items, key=order_by_id)
481
482 new_list = etree.SubElement(parent, 'ol')
483
484 for index, item in enumerate(items, start=1):
485 backlink = item.find('.//a[@class="footnote-backref"]')
486 backlink.set("title", self.footnotes.getConfig("BACKLINK_TITLE").format(index))
487 new_list.append(item)
488
489
490class FootnotePostprocessor(Postprocessor):

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected