jinja_utils.py
Python script, ASCII text executable
1from app import app, _, n_ 2import flask 3from datetime import datetime 4import markdown 5from markupsafe import Markup 6from urllib.parse import urlencode 7 8 9@app.template_global() 10def modify_query(**new_values): 11args = flask.request.args.copy() 12 13for key, value in new_values.items(): 14args[key] = value 15 16return f"{flask.request.path}?{urlencode(args)}" 17 18 19@app.template_filter("split") 20def split(value: str, separator=" ", maxsplit: int = -1): 21return value.split(separator, maxsplit) 22 23 24@app.template_filter("splitlines") 25def splitlines(value: str, keepends: bool = False): 26return value.splitlines(keepends=keepends) 27 28 29@app.template_filter("lstrip") 30def lstrip(value: str, characters=None): 31return value.lstrip(characters) 32 33 34@app.template_filter("rstrip") 35def rstrip(value: str, characters=None): 36return value.rstrip(characters) 37 38 39@app.template_filter("strftime") 40def strftime(value: datetime, syntax: str): 41# Split the strftime syntax into placeholders and literals. 42placeholders = { 43"%a", "%A", "%w", "%d", "%-d", "%b", "%B", "%m", "%-m", "%y", "%Y", 44"%H", "%-H", "%I", "%-I", "%p", "%M", "%-M", "%S", "%-S", "%f", "%z", 45"%Z", "%j", "%-j", "%U", "%W", "%c", "%x", "%X", "%%", "%G", "%u", "%V", 46"%-V", "%s", "%:z", "%e" 47} 48 49tokens = [] 50i = 0 51length = len(syntax) 52while i < length: 53if syntax[i] == "%": 54if i+1 < length and syntax[i:i + 2] in placeholders: 55tokens.append(syntax[i:i + 2]) 56i += 2 57elif i+1 < length and syntax[i:i + 3] in placeholders: 58tokens.append(syntax[i:i + 3]) 59i += 3 60else: 61tokens.append(syntax[i]) 62i += 1 63else: 64tokens.append(syntax[i]) 65i += 1 66 67new_tokens = [] 68 69for token in tokens: 70match token: 71case "%A": 72# Translate the full weekday name. 73weekday_number = value.weekday() 74weekday_names = [ 75_("Monday"), 76_("Tuesday"), 77_("Wednesday"), 78_("Thursday"), 79_("Friday"), 80_("Saturday"), 81_("Sunday") 82] 83new_tokens.append(weekday_names[weekday_number]) 84case "%a": 85# Translate the abbreviated weekday name. 86weekday_number = value.weekday() 87weekday_names = [ 88_("Mon"), 89_("Tue"), 90_("Wed"), 91_("Thu"), 92_("Fri"), 93_("Sat"), 94_("Sun") 95] 96new_tokens.append(weekday_names[weekday_number]) 97case "%B": 98# Translate the full month name. 99month_number = value.month 100month_names = [ 101_("January"), 102_("February"), 103_("March"), 104_("April"), 105_("May"), 106_("June"), 107_("July"), 108_("August"), 109_("September"), 110_("October"), 111_("November"), 112_("December") 113] 114new_tokens.append(month_names[month_number - 1]) 115case "%b": 116# Translate the abbreviated month name. 117month_number = value.month 118month_names = [ 119_("Jan"), 120_("Feb"), 121_("Mar"), 122_("Apr"), 123_("May (short)"), 124_("Jun"), 125_("Jul"), 126_("Aug"), 127_("Sep"), 128_("Oct"), 129_("Nov"), 130_("Dec") 131] 132new_tokens.append(month_names[month_number - 1]) 133case "%p": 134# Translate the AM/PM indicator. 135if value.hour < 12: 136new_tokens.append(_("am")) 137else: 138new_tokens.append(_("pm")) 139case _: 140new_tokens.append(token) 141 142print([value.strftime(token) if token in placeholders else token for token in new_tokens], syntax) 143syntax = "".join([value.strftime(token) if token in placeholders else token for token in new_tokens]) 144 145return value.strftime(syntax) 146 147 148@app.template_filter("unixtime") 149def unixtime(value: datetime): 150return round(value.timestamp()) 151 152 153@app.template_filter("decode") 154def decode(value: bytes, codec: str = "UTF-8", errors: str = "strict"): 155return value.decode(codec, errors) 156 157 158@app.template_filter("markdown") 159def parse_markdown(value: str): 160return Markup(markdown.make_html(markdown.tokenise(value))) 161 162 163@app.template_filter("inline_markdown") 164def parse_inline_markdown(value: str): 165return Markup(markdown.make_html(markdown.parse_line(value))) 166 167 168@app.template_filter("parse_diff_location") 169def parse_diff_location(value: str): 170header = value.split("@@")[1].strip() 171return [tuple(int(j) for j in i.lstrip("-+").split(",")) for i in header.split(" ")] 172 173 174@app.template_filter("harvester_protection") 175def harvester_protection(value): 176return "".join(f"&#x{ord(char):x};" for char in value) 177 178 179@app.template_filter("sort") 180def sort(value): 181return sorted(value) 182