MCPcopy Index your code
hub / github.com/Python-Markdown/markdown / FootnotePostTreeprocessor

Class FootnotePostTreeprocessor

markdown/extensions/footnotes.py:354–404  ·  view source on GitHub ↗

Amend footnote div with duplicates.

Source from the content-addressed store, hash-verified

352
353
354class FootnotePostTreeprocessor(Treeprocessor):
355 """ Amend footnote div with duplicates. """
356
357 def __init__(self, footnotes: FootnoteExtension):
358 self.footnotes = footnotes
359
360 def add_duplicates(self, li: etree.Element, duplicates: int) -> None:
361 """ Adjust current `li` and add the duplicates: `fnref2`, `fnref3`, etc. """
362 for link in li.iter('a'):
363 # Find the link that needs to be duplicated.
364 if link.attrib.get('class', '') == 'footnote-backref':
365 ref, rest = link.attrib['href'].split(self.footnotes.get_separator(), 1)
366 # Duplicate link the number of times we need to
367 # and point the to the appropriate references.
368 links = []
369 for index in range(2, duplicates + 1):
370 sib_link = copy.deepcopy(link)
371 sib_link.attrib['href'] = '%s%d%s%s' % (ref, index, self.footnotes.get_separator(), rest)
372 links.append(sib_link)
373 self.offset += 1
374 # Add all the new duplicate links.
375 el = list(li)[-1]
376 for link in links:
377 el.append(link)
378 break
379
380 def get_num_duplicates(self, li: etree.Element) -> int:
381 """ Get the number of duplicate refs of the footnote. """
382 fn, rest = li.attrib.get('id', '').split(self.footnotes.get_separator(), 1)
383 link_id = '{}ref{}{}'.format(fn, self.footnotes.get_separator(), rest)
384 return self.footnotes.found_refs.get(link_id, 0)
385
386 def handle_duplicates(self, parent: etree.Element) -> None:
387 """ Find duplicate footnotes and format and add the duplicates. """
388 for li in list(parent):
389 # Check number of duplicates footnotes and insert
390 # additional links if needed.
391 count = self.get_num_duplicates(li)
392 if count > 1:
393 self.add_duplicates(li, count)
394
395 def run(self, root: etree.Element) -> None:
396 """ Crawl the footnote div and add missing duplicate footnotes. """
397 self.offset = 0
398 for div in root.iter('div'):
399 if div.attrib.get('class', '') == 'footnote':
400 # Footnotes should be under the first ordered list under
401 # the footnote div. So once we find it, quit.
402 for ol in div.iter('ol'):
403 self.handle_duplicates(ol)
404 break
405
406
407class FootnoteTreeprocessor(Treeprocessor):

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…