Somewhat improve superscript parentheses, including nested parens.

I was able to get a single nested pair inside the super, but I
can't quite figure out how to get arbitrary nesting. This is good
enough for me to put a link in a super though, which is what I needed.
This commit is contained in:
Ethan Dalool 2020-09-05 13:07:21 -07:00
parent 18e8cb1efb
commit c1a6ddd5f1
2 changed files with 15 additions and 2 deletions

View file

@ -1,5 +1,6 @@
bs4 bs4
mistune mistune
pygments pygments
regex
requests requests
voussoirkit voussoirkit

View file

@ -11,6 +11,7 @@ import pygments.formatters
import pygments.lexers import pygments.lexers
import pygments.token import pygments.token
import re import re
import regex
import requests import requests
import string import string
import sys import sys
@ -118,7 +119,11 @@ class VoussoirInlineGrammar(mistune.InlineGrammar):
rarr = re.compile(r'-->') rarr = re.compile(r'-->')
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_one = re.compile(r'(\^+)([^\s\(]+)')
_not_parens = r'[^()]+'
_paren_pair = r'\(.+?\)'
_supered = rf'(?:{_not_parens}|{_paren_pair}|(?1))'
supers_many = regex.compile(rf'(?V1)(\^+)\(({_supered}*)\)')
footnote_link = re.compile(r'\[footnote_link\]') footnote_link = re.compile(r'\[footnote_link\]')
footnote_text = re.compile(r'\[footnote_text\]') footnote_text = re.compile(r'\[footnote_text\]')
subreddit = re.compile(r'\/r\/[A-Za-z0-9_]+') subreddit = re.compile(r'\/r\/[A-Za-z0-9_]+')
@ -135,7 +140,8 @@ class VoussoirInline(mistune.InlineLexer):
default_rules.insert(0, 'larr') default_rules.insert(0, 'larr')
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_one')
default_rules.insert(0, 'supers_many')
default_rules.insert(0, 'footnote_link') default_rules.insert(0, 'footnote_link')
default_rules.insert(0, 'footnote_text') default_rules.insert(0, 'footnote_text')
default_rules.insert(0, 'subreddit') default_rules.insert(0, 'subreddit')
@ -177,6 +183,12 @@ class VoussoirInline(mistune.InlineLexer):
text = self.output(text) text = self.output(text)
return f'{"<sup>" * carets}{text}{"</sup>" * carets}' return f'{"<sup>" * carets}{text}{"</sup>" * carets}'
def output_supers_one(self, m):
return self.output_supers(m)
def output_supers_many(self, m):
return self.output_supers(m)
def output_subreddit(self, m): def output_subreddit(self, m):
return f'<a href="https://old.reddit.com{m.group(0)}">{m.group(0)}</a>' return f'<a href="https://old.reddit.com{m.group(0)}">{m.group(0)}</a>'