From 4147020aa49b65d08afcbff07d4f7d97acc7c4fb Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 2 Apr 2020 20:19:37 -0700 Subject: [PATCH] Add markdown rules for redditor, subreddit. --- voussoir.net/writing/vmarkdown.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/voussoir.net/writing/vmarkdown.py b/voussoir.net/writing/vmarkdown.py index 23b78cb..afcc2d9 100644 --- a/voussoir.net/writing/vmarkdown.py +++ b/voussoir.net/writing/vmarkdown.py @@ -118,6 +118,8 @@ class VoussoirGrammar(mistune.InlineGrammar): supers = re.compile(r'(?:(\^+)([^\s]+))|(?:(\^+)\((.+?)\))') footnote_link = re.compile(r'\[footnote_link\]') footnote_text = re.compile(r'\[footnote_text\]') + subreddit = re.compile(r'/r/([^\s]+)') + redditor = re.compile(r'/u/([^\s]+)') # This `text` override is based on this article: # 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 @@ -133,6 +135,8 @@ class VoussoirLexer(mistune.InlineLexer): default_rules.insert(0, 'supers') default_rules.insert(0, 'footnote_link') default_rules.insert(0, 'footnote_text') + default_rules.insert(0, 'subreddit') + default_rules.insert(0, 'redditor') def __init__(self, renderer, **kwargs): rules = VoussoirGrammar() @@ -170,6 +174,12 @@ class VoussoirLexer(mistune.InlineLexer): text = self.output(text) return f'{"" * carets}{text}{"" * carets}' + def output_subreddit(self, m): + return f'{m.group(1)}' + + def output_redditor(self, m): + return f'{m.group(1)}' + footnote_link_index = 1 footnote_text_index = 1