[Utils] Add filters for spoiler markdown (#2401)

This also wraps some fields of the modlog with the same sanitization, as well as the `[p]names` command.
This commit is contained in:
Michael H
2019-02-15 19:34:38 -05:00
committed by Toby Harradine
parent 4b831a634a
commit d13bf37845
3 changed files with 66 additions and 12 deletions

View File

@@ -9,6 +9,8 @@ __all__ = [
"filter_mass_mentions",
"filter_various_mentions",
"normalize_smartquotes",
"escape_spoilers",
"escape_spoilers_and_mass_mentions",
]
# regexes
@@ -29,6 +31,10 @@ SMART_QUOTE_REPLACEMENT_DICT = {
SMART_QUOTE_REPLACE_RE = re.compile("|".join(SMART_QUOTE_REPLACEMENT_DICT.keys()))
SPOILER_CONTENT_RE = re.compile(
r"(?s)(?<!\\)(?P<OPEN>\|{2})(?P<SPOILERED>.*?)(?<!\\)(?P<CLOSE>\|{2})"
)
# convenience wrappers
def filter_urls(to_filter: str) -> str:
@@ -133,3 +139,37 @@ def normalize_smartquotes(to_normalize: str) -> str:
return SMART_QUOTE_REPLACEMENT_DICT.get(obj.group(0), "")
return SMART_QUOTE_REPLACE_RE.sub(replacement_for, to_normalize)
def escape_spoilers(content: str) -> str:
"""
Get a string with spoiler syntax escaped.
Parameters
----------
content : str
The string to escape.
Returns
-------
str
The escaped string.
"""
return SPOILER_CONTENT_RE.sub(r"\\\g<OPEN>\g<SPOILERED>\\\g<CLOSE>", content)
def escape_spoilers_and_mass_mentions(content: str) -> str:
"""
Get a string with spoiler syntax and mass mentions escaped
Parameters
----------
content : str
The string to escape.
Returns
-------
str
The escaped string.
"""
return escape_spoilers(filter_mass_mentions(content))