from .get import *
from mistletoe.span_token import SpanToken
from mistletoe.html_renderer import HTMLRenderer
import re
from flask import g
# add token/rendering for @username mentions
class UserMention(SpanToken):
pattern = re.compile("(^|\s|\n)@((\w|-){1,25})")
parse_inner = False
def __init__(self, match_obj):
self.target = (match_obj.group(1), match_obj.group(2))
class SubMention(SpanToken):
pattern = re.compile("(^|\s|\n)r/(\w{3,25})")
parse_inner = False
def __init__(self, match_obj):
self.target = (match_obj.group(1), match_obj.group(2))
class RedditorMention(SpanToken):
pattern = re.compile("(^|\s|\n)u/((\w|-){3,25})")
parse_inner = False
def __init__(self, match_obj):
self.target = (match_obj.group(1), match_obj.group(2))
class SubMention2(SpanToken):
pattern = re.compile("(^|\s|\n)/r/(\w{3,25})")
parse_inner = False
def __init__(self, match_obj):
self.target = (match_obj.group(1), match_obj.group(2))
class RedditorMention2(SpanToken):
pattern = re.compile("(^|\s|\n)/u/((\w|-){3,25})")
parse_inner = False
def __init__(self, match_obj):
self.target = (match_obj.group(1), match_obj.group(2))
class CustomRenderer(HTMLRenderer):
def __init__(self, **kwargs):
super().__init__(UserMention,
SubMention,
RedditorMention,
SubMention2,
RedditorMention2,
)
for i in kwargs:
self.__dict__[i] = kwargs[i]
def render_user_mention(self, token):
space = token.target[0]
target = token.target[1]
user = get_user(target, graceful=True)
try:
if g.v.admin_level == 0 and g.v.any_block_exists(user):
return f"{space}@{target}"
except BaseException: pass
if not user: return f"{space}@{target}"
return f'''{space}@{user.username}'''
def render_sub_mention(self, token):
space = token.target[0]
target = token.target[1]
return f'{space}r/{target}'
def render_redditor_mention(self, token):
space = token.target[0]
target = token.target[1]
return f'{space}u/{target}'
class Renderer(HTMLRenderer):
def __init__(self, **kwargs):
super().__init__(UserMention,
SubMention,
RedditorMention,
SubMention2,
RedditorMention2,
)
for i in kwargs:
self.__dict__[i] = kwargs[i]
def render_user_mention(self, token):
space = token.target[0]
target = token.target[1]
user = get_user(target, graceful=True)
try:
if g.v.admin_level == 0 and g.v.any_block_exists(user):
return f"{space}@{target}"
except BaseException:
pass
if not user: return f"{space}@{target}"
return f'{space}@{user.username}'
def render_sub_mention(self, token):
space = token.target[0]
target = token.target[1]
return f'{space}r/{target}'
def render_redditor_mention(self, token):
space = token.target[0]
target = token.target[1]
return f'{space}u/{target}'