By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 jinja_utils.py

View raw Download
text/x-script.python • 1.87 kiB
Python script, ASCII text executable
        
            
1
from app import app
2
import flask
3
from datetime import datetime
4
import markdown
5
from markupsafe import Markup
6
from urllib.parse import urlencode
7
8
9
@app.template_global()
10
def modify_query(**new_values):
11
args = flask.request.args.copy()
12
13
for key, value in new_values.items():
14
args[key] = value
15
16
return f"{flask.request.path}?{urlencode(args)}"
17
18
19
@app.template_filter("split")
20
def split(value: str, separator=" ", maxsplit: int = -1):
21
return value.split(separator, maxsplit)
22
23
24
@app.template_filter("splitlines")
25
def splitlines(value: str, keepends: bool = False):
26
return value.splitlines(keepends=keepends)
27
28
29
@app.template_filter("lstrip")
30
def lstrip(value: str, characters=None):
31
return value.lstrip(characters)
32
33
34
@app.template_filter("rstrip")
35
def rstrip(value: str, characters=None):
36
return value.rstrip(characters)
37
38
39
@app.template_filter("strftime")
40
def strftime(value: datetime, syntax: str):
41
return value.strftime(syntax)
42
43
44
@app.template_filter("unixtime")
45
def unixtime(value: datetime):
46
return round(value.timestamp())
47
48
49
@app.template_filter("decode")
50
def decode(value: bytes, codec: str = "UTF-8", errors: str = "strict"):
51
return value.decode(codec, errors)
52
53
54
@app.template_filter("markdown")
55
def parse_markdown(value: str):
56
return Markup(markdown.make_html(markdown.tokenise(value)))
57
58
59
@app.template_filter("inline_markdown")
60
def parse_inline_markdown(value: str):
61
return Markup(markdown.make_html(markdown.parse_line(value)))
62
63
64
@app.template_filter("parse_diff_location")
65
def parse_diff_location(value: str):
66
header = value.split("@@")[1].strip()
67
return [tuple(int(j) for j in i.lstrip("-+").split(",")) for i in header.split(" ")]
68
69
70
@app.template_filter("harvester_protection")
71
def harvester_protection(value):
72
return "".join(f"&#x{ord(char):x};" for char in value)
73
74
75
@app.template_filter("sort")
76
def sort(value):
77
return sorted(value)
78