Add auto-incrementing [footnote_link] and [footnote_text].

This commit is contained in:
Ethan Dalool 2020-03-23 16:47:04 -07:00
parent 8ad213e924
commit d45ca1ef38

View file

@ -116,6 +116,8 @@ class VoussoirGrammar(mistune.InlineGrammar):
mdash = re.compile(r'--') mdash = re.compile(r'--')
category_tag = re.compile(r'\[tag:([\w\.]+)\]') category_tag = re.compile(r'\[tag:([\w\.]+)\]')
supers = re.compile(r'(?:(\^+)([^\s]+))|(?:(\^+)\((.+?)\))') supers = re.compile(r'(?:(\^+)([^\s]+))|(?:(\^+)\((.+?)\))')
footnote_link = re.compile(r'\[footnote_link\]')
footnote_text = re.compile(r'\[footnote_text\]')
# This `text` override is based on this article: # This `text` override is based on this article:
# https://ana-balica.github.io/2015/12/21/mistune-custom-lexers-we-are-going-deeper/ # https://ana-balica.github.io/2015/12/21/mistune-custom-lexers-we-are-going-deeper/
# in which we have to keep adding characters to the recognized list every # in which we have to keep adding characters to the recognized list every
@ -129,6 +131,8 @@ class VoussoirLexer(mistune.InlineLexer):
default_rules.insert(0, 'rarr') default_rules.insert(0, 'rarr')
default_rules.insert(0, 'category_tag') default_rules.insert(0, 'category_tag')
default_rules.insert(0, 'supers') default_rules.insert(0, 'supers')
default_rules.insert(0, 'footnote_link')
default_rules.insert(0, 'footnote_text')
def __init__(self, renderer, **kwargs): def __init__(self, renderer, **kwargs):
rules = VoussoirGrammar() rules = VoussoirGrammar()
@ -139,6 +143,18 @@ class VoussoirLexer(mistune.InlineLexer):
tagname = qualname.split('.')[-1] tagname = qualname.split('.')[-1]
return f'<a class="tag_link" data-qualname="{qualname}">[{tagname}]</a>' return f'<a class="tag_link" data-qualname="{qualname}">[{tagname}]</a>'
def output_footnote_link(self, m):
global footnote_link_index
ret = f'[{footnote_link_index}]'
footnote_link_index += 1
return ret
def output_footnote_text(self, m):
global footnote_text_index
ret = f'[{footnote_text_index}]'
footnote_text_index += 1
return ret
def output_mdash(self, m): def output_mdash(self, m):
return '&mdash;' return '&mdash;'
@ -154,6 +170,8 @@ class VoussoirLexer(mistune.InlineLexer):
text = self.output(text) text = self.output(text)
return f'{"<sup>" * carets}{text}{"</sup>" * carets}' return f'{"<sup>" * carets}{text}{"</sup>" * carets}'
footnote_link_index = 1
footnote_text_index = 1
renderer = VoussoirRenderer() renderer = VoussoirRenderer()
inline = VoussoirLexer(renderer) inline = VoussoirLexer(renderer)
@ -537,6 +555,11 @@ def markdown(
image_cache=None, image_cache=None,
return_soup=False, return_soup=False,
): ):
global footnote_link_index
global footnote_text_index
footnote_link_index = 1
footnote_text_index = 1
css = cat_files(css) css = cat_files(css)
body = VMARKDOWN(md) body = VMARKDOWN(md)