`InlineProcessor` for footnote markers in a document's body text.
| 334 | |
| 335 | |
| 336 | class FootnoteInlineProcessor(InlineProcessor): |
| 337 | """ `InlineProcessor` for footnote markers in a document's body text. """ |
| 338 | |
| 339 | def __init__(self, pattern: str, footnotes: FootnoteExtension): |
| 340 | super().__init__(pattern) |
| 341 | self.footnotes = footnotes |
| 342 | |
| 343 | def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]: |
| 344 | id = m.group(1) |
| 345 | if id in self.footnotes.footnotes.keys(): |
| 346 | self.footnotes.addFootnoteRef(id) |
| 347 | |
| 348 | if not self.footnotes.getConfig("USE_DEFINITION_ORDER"): |
| 349 | # Order by reference |
| 350 | footnote_num = self.footnotes.footnote_order.index(id) + 1 |
| 351 | else: |
| 352 | # Order by definition |
| 353 | footnote_num = list(self.footnotes.footnotes.keys()).index(id) + 1 |
| 354 | |
| 355 | sup = etree.Element("sup") |
| 356 | a = etree.SubElement(sup, "a") |
| 357 | sup.set('id', self.footnotes.makeFootnoteRefId(id, found=True)) |
| 358 | a.set('href', '#' + self.footnotes.makeFootnoteId(id)) |
| 359 | a.set('class', 'footnote-ref') |
| 360 | a.text = self.footnotes.getConfig("SUPERSCRIPT_TEXT").format(footnote_num) |
| 361 | return sup, m.start(0), m.end(0) |
| 362 | else: |
| 363 | return None, None, None |
| 364 | |
| 365 | |
| 366 | class FootnotePostTreeprocessor(Treeprocessor): |