From c1a6ddd5f1e53a11afabee8d6df2bd5064fb74ea Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 5 Sep 2020 13:07:21 -0700 Subject: [PATCH] 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. --- voussoir.net/writing/requirements.txt | 1 + voussoir.net/writing/vmarkdown.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/voussoir.net/writing/requirements.txt b/voussoir.net/writing/requirements.txt index 63e4fe2..5f09385 100644 --- a/voussoir.net/writing/requirements.txt +++ b/voussoir.net/writing/requirements.txt @@ -1,5 +1,6 @@ bs4 mistune pygments +regex requests voussoirkit diff --git a/voussoir.net/writing/vmarkdown.py b/voussoir.net/writing/vmarkdown.py index 4852898..39ce658 100644 --- a/voussoir.net/writing/vmarkdown.py +++ b/voussoir.net/writing/vmarkdown.py @@ -11,6 +11,7 @@ import pygments.formatters import pygments.lexers import pygments.token import re +import regex import requests import string import sys @@ -118,7 +119,11 @@ class VoussoirInlineGrammar(mistune.InlineGrammar): rarr = re.compile(r'-->') mdash = re.compile(r'--') 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_text = re.compile(r'\[footnote_text\]') 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, 'rarr') 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_text') default_rules.insert(0, 'subreddit') @@ -177,6 +183,12 @@ class VoussoirInline(mistune.InlineLexer): text = self.output(text) return f'{"" * carets}{text}{"" * 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): return f'{m.group(0)}'