A Python snippet for pretty links in footnotes

So I was writing a post and I got fed up with manually numbering footnotes, and so I created this quick and dirty set of functions to enable footnote auto-numbering. It’s not as clever as I would prefer it to be, but it’s good ‘nuff.

Usage

Example

# This the greatest and best song in the world
> Could not remember the best song in the world. No, this is just a tribute. [^FN]

--
=> gemini://gemi.dev/cgi-bin/wp.cgi/view?Tribute+(song) [^FN] Tenacious D - Tribute

Would be rendered as:

# This the greatest and best song in the world
> Could not remember the best song in the world. No, this is just a tribute. ¹

--
=> gemini://gemi.dev/cgi-bin/wp.cgi/view?Tribute+(song) ¹ Tenacious D - Tribute

Limitations:

It processes footnotes in two passes: it first numbers the ones at the end of the document, then it numbers the ones in the document body. So make sure that the footnotes match up - that they are the right number and in the right order.

How I would improve it:

The Code:

(Python)

FOOTNOTE_INLINE = re.compile(r'(\[\^FN\])', re.M)
FOOTNOTE_END = re.compile(r'(?P=>\s\S+\s)(?P\[\^FN\])(?P.*)', re.M)

def footnote_numbering(n):
    retstr = ""
    subs = {
        "0": "⁰",
        "1" : "¹",
        "2" : "²",
        "3" : "³",
        "4" : "⁴",
        "5" : "⁵",
        "6" : "⁶",
        "7" : "⁷",
        "8" : "⁸",
        "9" : "⁹"
    }
    for c in str(n):
        retstr += subs[c]
    
    return retstr

endfn_no = 1
def process_end_footnotes(match):
    global endfn_no
    retstr = match.group("link")
    retstr +=  footnote_numbering(endfn_no)
    retstr += match.group("desc")
    endfn_no += 1
    return retstr

inlnfn_no = 1
def process_inline_footnotes(match):
    global inlnfn_no
    retstr = footnote_numbering(inlnfn_no)
    inlnfn_no += 1
    return retstr

def prettier_footnotes(body):
    body.content = re.sub(FOOTNOTE_END, process_end_footnotes, body.content)
    body.content = re.sub(FOOTNOTE_INLINE, process_inline_footnotes, body.content)
    return body
Proxy Information
Original URL
gemini://vigilia.cc/?p=projects%2Ffootnote-snippet.gmi
Status Code
Success (20)
Meta
text/gemini; charset=utf-8; lang=en-GB
Capsule Response Time
564.257534 milliseconds
Gemini-to-HTML Time
0.426029 milliseconds

This content has been proxied by September (3851b).