262 lines
9.5 KiB
Python
262 lines
9.5 KiB
Python
from .babycode_parser import Parser
|
|
from markupsafe import Markup, escape
|
|
import re
|
|
|
|
BABYCODE_VERSION = 3
|
|
|
|
NAMED_COLORS = [
|
|
'black', 'silver', 'gray', 'white', 'maroon', 'red',
|
|
'purple', 'fuchsia', 'green', 'lime', 'olive', 'yellow',
|
|
'navy', 'blue', 'teal', 'aqua', 'aliceblue', 'antiquewhite',
|
|
'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black',
|
|
'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue',
|
|
'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson',
|
|
'cyan', 'aqua', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray',
|
|
'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange',
|
|
'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
|
|
'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray',
|
|
'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia',
|
|
'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green',
|
|
'greenyellow', 'grey', 'gray', 'honeydew', 'hotpink', 'indianred',
|
|
'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
|
|
'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray',
|
|
'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
|
|
'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen',
|
|
'linen', 'magenta', 'fuchsia', 'maroon', 'mediumaquamarine', 'mediumblue',
|
|
'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise',
|
|
'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite',
|
|
'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered',
|
|
'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip',
|
|
'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple',
|
|
'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon',
|
|
'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue',
|
|
'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue',
|
|
'tan', 'teal', 'thistle', 'tomato', 'transparent', 'turquoise',
|
|
'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen',
|
|
]
|
|
|
|
def is_tag(e, tag=None):
|
|
if e is None:
|
|
return False
|
|
if isinstance(e, str):
|
|
return False
|
|
if e['type'] != 'bbcode':
|
|
return False
|
|
|
|
if tag is None:
|
|
return True
|
|
|
|
return e['name'] == tag
|
|
|
|
def is_text(e):
|
|
return isinstance(e, str)
|
|
|
|
def tag_code(children, attr, surrounding):
|
|
is_inline = children.find('\n') == -1
|
|
if is_inline:
|
|
return f"<code class=\"inline-code\">{children}</code>"
|
|
else:
|
|
t = children.strip()
|
|
button = f"<button type=button class=\"copy-code\" value=\"{t}\">Copy</button>"
|
|
return f"<pre><span class=\"copy-code-container\">{button}</span><code>{t}</code></pre>"
|
|
|
|
def tag_list(children):
|
|
list_body = re.sub(r" +\n", "<br>", children.strip())
|
|
list_body = re.sub(r"\n\n+", "\1", list_body)
|
|
return " ".join([f"<li>{x}</li>" for x in list_body.split("\1") if x])
|
|
|
|
def tag_color(children, attr, surrounding):
|
|
hex_re = r"^#?([0-9a-f]{6}|[0-9a-f]{3})$"
|
|
potential_color = attr.lower().strip()
|
|
|
|
if potential_color in NAMED_COLORS:
|
|
return f"<span style='color: {potential_color};'>{children}</span>"
|
|
|
|
m = re.match(hex_re, potential_color)
|
|
if m:
|
|
return f"<span style='color: #{m.group(1)};'>{children}</span>"
|
|
|
|
# return just the way it was if we can't parse it
|
|
return f"[color={attr}]{children}[/color]"
|
|
|
|
def tag_spoiler(children, attr, surrounding):
|
|
spoiler_name = attr if attr else "Spoiler"
|
|
content = f"<div class='accordion-content post-accordion-content hidden'>{children}</div>"
|
|
container = f"""<div class='accordion hidden'><div class='accordion-header'><button type='button' class='accordion-toggle'>+</button><span>{spoiler_name}</span></div>{content}</div>"""
|
|
return container
|
|
|
|
def tag_image(children, attr, surrounding):
|
|
img = f"<img class=\"post-image\" src=\"{attr}\" alt=\"{children}\">"
|
|
if not is_tag(surrounding[0], 'img'):
|
|
img = f"<div class=post-img-container>{img}"
|
|
if not is_tag(surrounding[1], 'img'):
|
|
img = f"{img}</div>"
|
|
return img
|
|
|
|
TAGS = {
|
|
"b": lambda children, attr, _: f"<strong>{children}</strong>",
|
|
"i": lambda children, attr, _: f"<em>{children}</em>",
|
|
"s": lambda children, attr, _: f"<del>{children}</del>",
|
|
"u": lambda children, attr, _: f"<u>{children}</u>",
|
|
|
|
"img": tag_image,
|
|
"url": lambda children, attr, _: f"<a href={attr}>{children}</a>",
|
|
"quote": lambda children, attr, _: f"<blockquote>{children}</blockquote>",
|
|
"code": tag_code,
|
|
"ul": lambda children, attr, _: f"<ul>{tag_list(children)}</ul>",
|
|
"ol": lambda children, attr, _: f"<ol>{tag_list(children)}</ol>",
|
|
|
|
"big": lambda children, attr, _: f"<span style='font-size: 2rem;'>{children}</span>",
|
|
"small": lambda children, attr, _: f"<span style='font-size: 0.75rem;'>{children}</span>",
|
|
"color": tag_color,
|
|
|
|
"center": lambda children, attr, _: f"<div style='text-align: center;'>{children}</div>",
|
|
"right": lambda children, attr, _: f"<div style='text-align: right;'>{children}</div>",
|
|
|
|
"spoiler": tag_spoiler,
|
|
}
|
|
|
|
# [img] is considered block for the purposes of collapsing whitespace,
|
|
# despite being potentially inline (since the resulting <img> tag is inline, but creates a block container around itself and sibling images).
|
|
# [code] has a special case in is_inline().
|
|
INLINE_TAGS = {
|
|
'b', 'i', 's', 'u', 'color', 'big', 'small', 'url'
|
|
}
|
|
|
|
def make_emoji(name, code):
|
|
return f' <img class=emoji src="/static/emoji/{name}.png" alt="{name}" title=":{code}:">'
|
|
|
|
EMOJI = {
|
|
'angry': make_emoji('angry', 'angry'),
|
|
|
|
'(': make_emoji('frown', '('),
|
|
|
|
'D': make_emoji('grin', 'D'),
|
|
|
|
'imp': make_emoji('imp', 'imp'),
|
|
|
|
'angryimp': make_emoji('impangry', 'angryimp'),
|
|
'impangry': make_emoji('impangry', 'impangry'),
|
|
|
|
'lobster': make_emoji('lobster', 'lobster'),
|
|
|
|
'|': make_emoji('neutral', '|'),
|
|
|
|
'pensive': make_emoji('pensive', 'pensive'),
|
|
|
|
'scissors': make_emoji('scissors', 'scissors'),
|
|
|
|
')': make_emoji('smile', ')'),
|
|
|
|
'smiletear': make_emoji('smiletear', 'smiletear'),
|
|
'crytear': make_emoji('smiletear', 'crytear'),
|
|
|
|
',': make_emoji('sob', ','),
|
|
'T': make_emoji('sob', 'T'),
|
|
'cry': make_emoji('sob', 'cry'),
|
|
'sob': make_emoji('sob', 'sob'),
|
|
|
|
'o': make_emoji('surprised', 'o'),
|
|
'O': make_emoji('surprised', 'O'),
|
|
|
|
'hmm': make_emoji('think', 'hmm'),
|
|
'think': make_emoji('think', 'think'),
|
|
'thinking': make_emoji('think', 'thinking'),
|
|
|
|
'P': make_emoji('tongue', 'P'),
|
|
'p': make_emoji('tongue', 'p'),
|
|
|
|
'weary': make_emoji('weary', 'weary'),
|
|
|
|
';': make_emoji('wink', ';'),
|
|
'wink': make_emoji('wink', 'wink'),
|
|
}
|
|
|
|
TEXT_ONLY = ["code"]
|
|
|
|
def break_lines(text):
|
|
text = re.sub(r" +\n", "<br>", text)
|
|
text = re.sub(r"\n\n+", "<br><br>", text)
|
|
return text
|
|
|
|
def is_inline(e):
|
|
if e is None:
|
|
return False # i think
|
|
|
|
if is_text(e):
|
|
return True
|
|
|
|
if is_tag(e):
|
|
if is_tag(e, 'code'): # special case, since [code] can be inline OR block
|
|
return '\n' not in e['children']
|
|
|
|
return e['name'] in INLINE_TAGS
|
|
|
|
return e['type'] != 'rule'
|
|
|
|
def should_collapse(text, surrounding):
|
|
if not isinstance(text, str):
|
|
return False
|
|
|
|
if not text:
|
|
return True
|
|
|
|
if not text.strip() and '\n' not in text:
|
|
return not is_inline(surrounding[0]) and not is_inline(surrounding[1])
|
|
|
|
return False
|
|
|
|
def babycode_to_html(s):
|
|
subj = escape(s.strip().replace('\r\n', '\n').replace('\r', '\n'))
|
|
parser = Parser(subj)
|
|
parser.valid_bbcode_tags = TAGS.keys()
|
|
parser.bbcode_tags_only_text_children = TEXT_ONLY
|
|
parser.valid_emotes = EMOJI.keys()
|
|
|
|
uncollapsed = parser.parse()
|
|
elements = []
|
|
for i in range(len(uncollapsed)):
|
|
e = uncollapsed[i]
|
|
surrounding = (
|
|
uncollapsed[i - 1] if i-1 >= 0 else None,
|
|
uncollapsed[i + 1] if i+1 < len(uncollapsed) else None
|
|
)
|
|
if not should_collapse(e, surrounding):
|
|
elements.append(e)
|
|
|
|
out = ""
|
|
def fold(element, nobr, surrounding):
|
|
if isinstance(element, str):
|
|
if nobr:
|
|
return element
|
|
return break_lines(element)
|
|
|
|
match element['type']:
|
|
case "bbcode":
|
|
c = ""
|
|
for i in range(len(element['children'])):
|
|
child = element['children'][i]
|
|
_surrounding = (
|
|
element['children'][i - 1] if i-1 >= 0 else None,
|
|
element['children'][i + 1] if i+1 < len(element['children']) else None
|
|
)
|
|
_nobr = element['name'] == "code" or element['name'] == "ul" or element['name'] == "ol"
|
|
c = c + Markup(fold(child, _nobr, _surrounding))
|
|
res = TAGS[element['name']](c, element['attr'], surrounding)
|
|
return res
|
|
case "link":
|
|
return f"<a href=\"{element['url']}\">{element['url']}</a>"
|
|
case 'emote':
|
|
return EMOJI[element['name']]
|
|
case "rule":
|
|
return "<hr>"
|
|
|
|
for i in range(len(elements)):
|
|
e = elements[i]
|
|
surrounding = (
|
|
elements[i - 1] if i-1 >= 0 else None,
|
|
elements[i + 1] if i+1 < len(elements) else None
|
|
)
|
|
out = out + fold(e, False, surrounding)
|
|
return out
|