diff --git a/files/helpers/owoify.py b/files/helpers/owoify.py new file mode 100644 index 000000000..abcca7bc1 --- /dev/null +++ b/files/helpers/owoify.py @@ -0,0 +1,54 @@ +from owoify.utility.interleave_arrays import interleave_arrays +from owoify.utility.presets import * +from owoify.structures.word import Word + +import re +import files.helpers.regex as help_re +import files.helpers.sanitize as sanitize + +# Includes, excerpts, and modifies some functions from: +# https://github.com/deadshot465/owoify-py @ owoify/owoify.py + +OWO_WORD_REGEX = re.compile(r'[^\s]+') +OWO_SPACE_REGEX = re.compile(r'\s+') + +OWO_EXCLUDE_PATTERNS = [ + re.compile(r'\]\('), # links []() and images ![]() + # NB: May not be effective when URL part contains literal spaces vs %20 + # Also relies on owoify replacements currently not affecting symbols. + sanitize.url_re, # bare links + re.compile(r':[!#@a-z0-9_\-]+:', flags=re.I|re.A), # emoji + help_re.mention_regex, # mentions + help_re.poll_regex, # polls + help_re.choice_regex, + help_re.command_regex, # markup commands +] + +def owoify(source: str) -> str: + word_matches = OWO_WORD_REGEX.findall(source) + space_matches = OWO_SPACE_REGEX.findall(source) + + words = [Word(s) for s in word_matches] + spaces = [Word(s) for s in space_matches] + + words = list(map(lambda w: owoify_map_token_custom(w), words)) + + result = interleave_arrays(words, spaces) + result_strings = list(map(lambda w: str(w), result)) + return ''.join(result_strings) + +def owoify_map_token_custom(token): + for pattern in OWO_EXCLUDE_PATTERNS: + # if pattern appears anywhere in token, do not owoify. + if pattern.search(token.word): + return token + + # Original Owoification Logic (sans cases for higher owo levels) + for func in SPECIFIC_WORD_MAPPING_LIST: + token = func(token) + + for func in OWO_MAPPING_LIST: + token = func(token) + # End Original Owoification Logic + + return token diff --git a/files/routes/awards.py b/files/routes/awards.py index 743d4b4d8..a72c9b07a 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -11,7 +11,7 @@ from .front import frontlist from flask import g, request from files.helpers.sanitize import filter_emojis_only from files.helpers.marsify import marsify -import owoify +from files.helpers.owoify import owoify from copy import deepcopy @app.get("/shop") @@ -351,7 +351,7 @@ def award_thing(v, thing_type, id): if author.owoify: author.owoify += 21600 else: author.owoify = int(time.time()) + 21600 body = thing.body - body = owoify.owoify(body) + body = owoify(body) if author.marsify: body = marsify(body) thing.body_html = sanitize(body, limit_pings=5) g.db.add(thing) @@ -359,7 +359,7 @@ def award_thing(v, thing_type, id): if author.marsify: author.marsify += 21600 else: author.marsify = int(time.time()) + 21600 body = thing.body - if author.owoify: body = owoify.owoify(body) + if author.owoify: body = owoify(body) body = marsify(body) thing.body_html = sanitize(body, limit_pings=5) g.db.add(thing) diff --git a/files/routes/comments.py b/files/routes/comments.py index 1d97e8f4d..d9db61363 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -15,7 +15,7 @@ from flask import * from files.__main__ import app, limiter from files.helpers.sanitize import filter_emojis_only from files.helpers.marsify import marsify -import owoify +from files.helpers.owoify import owoify import requests from shutil import copyfile from json import loads @@ -293,7 +293,7 @@ def comment(v): body_for_sanitize = body if v.owoify: - body_for_sanitize = owoify.owoify(body_for_sanitize) + body_for_sanitize = owoify(body_for_sanitize) if v.marsify: body_for_sanitize = marsify(body_for_sanitize) @@ -729,7 +729,7 @@ def edit_comment(cid, v): body_for_sanitize = body if v.owoify: - body_for_sanitize = owoify.owoify(body_for_sanitize) + body_for_sanitize = owoify(body_for_sanitize) if v.marsify: body_for_sanitize = marsify(body_for_sanitize)