Created
June 1, 2022 01:40
-
-
Save Gouvernathor/8f2aea03f898b3efbc8324577377add0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def formatter(s): | |
def replacement(match): | |
pre, bangs, suf = match.group(1, 2, 3) | |
# pre : the part before the first bang | |
# bangs : the bang (if any) and the characters going with it | |
# suf : the colon (if any) and the characters going with it | |
if not bangs: | |
return eval("f\"{" + pre + suf + "}\"") | |
conversion = set(bangs[1:]) # the first character is always a bang | |
sra = conversion - set("tiqulc") | |
conversion = conversion - sra | |
if sra: | |
sra = "!" + "".join(sra) | |
value = eval("f\"{" + pre + (sra or "") + suf + "}\"") | |
if "t" in conversion: | |
value = renpy.translation.translate_string(value) | |
if "i" in conversion: | |
try: | |
value = substitute(value, translate=False)[0] | |
except RecursionError: | |
raise ValueError("Substitution {!r} refers to itself in a loop.".format(value)) | |
if "q" in conversion: | |
value = value.replace("{", "{{") | |
if "u" in conversion: | |
value = value.upper() | |
if "l" in conversion: | |
value = value.lower() | |
if "c" in conversion and value: | |
value = value[0].capitalize() + value[1:] | |
return value | |
return re.sub(r"\[([^!:\n]+)((?:![^!:\n]+)?)((?::[^!:\n]+)?)\]", replacement, s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment