jinja_utils.py
Python script, ASCII text executable
1from app import app 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): 41return value.strftime(syntax) 42 43 44@app.template_filter("unixtime") 45def unixtime(value: datetime): 46return round(value.timestamp()) 47 48 49@app.template_filter("decode") 50def decode(value: bytes, codec: str = "UTF-8", errors: str = "strict"): 51return value.decode(codec, errors) 52 53 54@app.template_filter("markdown") 55def parse_markdown(value: str): 56return Markup(markdown.make_html(markdown.tokenise(value))) 57 58 59@app.template_filter("inline_markdown") 60def parse_inline_markdown(value: str): 61return Markup(markdown.make_html(markdown.parse_line(value))) 62 63 64@app.template_filter("parse_diff_location") 65def parse_diff_location(value: str): 66header = value.split("@@")[1].strip() 67return [tuple(int(j) for j in i.lstrip("-+").split(",")) for i in header.split(" ")] 68 69 70@app.template_filter("harvester_protection") 71def harvester_protection(value): 72return "".join(f"&#x{ord(char):x};" for char in value) 73 74 75@app.template_filter("sort") 76def sort(value): 77return sorted(value) 78