jinja_utils.py
Python script, ASCII text executable
1from app import app 2from datetime import datetime 3import markdown 4from markupsafe import Markup 5 6 7@app.template_filter("split") 8def split(value: str, separator=" ", maxsplit: int = -1): 9return value.split(separator, maxsplit) 10 11 12@app.template_filter("splitlines") 13def splitlines(value: str, keepends: bool = False): 14return value.splitlines(keepends=keepends) 15 16 17@app.template_filter("lstrip") 18def lstrip(value: str, characters=None): 19return value.lstrip(characters) 20 21 22@app.template_filter("rstrip") 23def rstrip(value: str, characters=None): 24return value.rstrip(characters) 25 26 27@app.template_filter("strftime") 28def strftime(value: datetime, syntax: str): 29return value.strftime(syntax) 30 31 32@app.template_filter("unixtime") 33def unixtime(value: datetime): 34return round(value.timestamp()) 35 36 37@app.template_filter("decode") 38def decode(value: bytes, codec: str = "UTF-8", errors: str = "strict"): 39return value.decode(codec, errors) 40 41 42@app.template_filter("markdown") 43def parse_markdown(value: str): 44return Markup(markdown.make_html(markdown.tokenise(value))) 45 46 47@app.template_filter("inline_markdown") 48def parse_inline_markdown(value: str): 49return Markup(markdown.make_html(markdown.parse_line(value))) 50 51 52@app.template_filter("parse_diff_location") 53def parse_diff_location(value: str): 54header = value.split("@@")[1].strip() 55return [tuple(int(j) for j in i.lstrip("-+").split(",")) for i in header.split(" ")] 56 57 58@app.template_filter("harvester_protection") 59def harvester_protection(value): 60return "".join(f"&#x{ord(char):x};" for char in value) 61 62 63@app.template_filter("sort") 64def sort(value): 65return sorted(value) 66