Reorder list items in the footnotes div.
| 428 | |
| 429 | |
| 430 | class FootnoteReorderingProcessor(Treeprocessor): |
| 431 | """ Reorder list items in the footnotes div. """ |
| 432 | |
| 433 | def __init__(self, footnotes: FootnoteExtension): |
| 434 | self.footnotes = footnotes |
| 435 | |
| 436 | def run(self, root: etree.Element) -> None: |
| 437 | if not self.footnotes.footnotes: |
| 438 | return |
| 439 | if self.footnotes.footnote_order != list(self.footnotes.footnotes.keys()): |
| 440 | for div in root.iter('div'): |
| 441 | if div.attrib.get('class', '') == 'footnote': |
| 442 | self.reorder_footnotes(div) |
| 443 | break |
| 444 | |
| 445 | def reorder_footnotes(self, parent: etree.Element) -> None: |
| 446 | old_list = parent.find('ol') |
| 447 | parent.remove(old_list) |
| 448 | items = old_list.findall('li') |
| 449 | |
| 450 | def order_by_id(li) -> int: |
| 451 | id = li.attrib.get('id', '').split(self.footnotes.get_separator(), 1)[-1] |
| 452 | return ( |
| 453 | self.footnotes.footnote_order.index(id) |
| 454 | if id in self.footnotes.footnote_order |
| 455 | else len(self.footnotes.footnotes) |
| 456 | ) |
| 457 | |
| 458 | items = sorted(items, key=order_by_id) |
| 459 | |
| 460 | new_list = etree.SubElement(parent, 'ol') |
| 461 | |
| 462 | for index, item in enumerate(items, start=1): |
| 463 | backlink = item.find('.//a[@class="footnote-backref"]') |
| 464 | backlink.set("title", self.footnotes.getConfig("BACKLINK_TITLE").format(index)) |
| 465 | new_list.append(item) |
| 466 | |
| 467 | |
| 468 | class FootnotePostprocessor(Postprocessor): |
no outgoing calls
no test coverage detected
searching dependent graphs…