You're looking at it

Homepage: https://roundabout-host.com

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 • 5.31 kiB
Python script, ASCII text executable
        
            
1
from app import app, _, n_
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
# Split the strftime syntax into placeholders and literals.
42
placeholders = {
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
49
tokens = []
50
i = 0
51
length = len(syntax)
52
while i < length:
53
if syntax[i] == "%":
54
if i+1 < length and syntax[i:i + 2] in placeholders:
55
tokens.append(syntax[i:i + 2])
56
i += 2
57
elif i+1 < length and syntax[i:i + 3] in placeholders:
58
tokens.append(syntax[i:i + 3])
59
i += 3
60
else:
61
tokens.append(syntax[i])
62
i += 1
63
else:
64
tokens.append(syntax[i])
65
i += 1
66
67
new_tokens = []
68
69
for token in tokens:
70
match token:
71
case "%A":
72
# Translate the full weekday name.
73
weekday_number = value.weekday()
74
weekday_names = [
75
_("Monday"),
76
_("Tuesday"),
77
_("Wednesday"),
78
_("Thursday"),
79
_("Friday"),
80
_("Saturday"),
81
_("Sunday")
82
]
83
new_tokens.append(weekday_names[weekday_number])
84
case "%a":
85
# Translate the abbreviated weekday name.
86
weekday_number = value.weekday()
87
weekday_names = [
88
_("Mon"),
89
_("Tue"),
90
_("Wed"),
91
_("Thu"),
92
_("Fri"),
93
_("Sat"),
94
_("Sun")
95
]
96
new_tokens.append(weekday_names[weekday_number])
97
case "%B":
98
# Translate the full month name.
99
month_number = value.month
100
month_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
]
114
new_tokens.append(month_names[month_number - 1])
115
case "%b":
116
# Translate the abbreviated month name.
117
month_number = value.month
118
month_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
]
132
new_tokens.append(month_names[month_number - 1])
133
case "%p":
134
# Translate the AM/PM indicator.
135
if value.hour < 12:
136
new_tokens.append(_("am"))
137
else:
138
new_tokens.append(_("pm"))
139
case _:
140
new_tokens.append(token)
141
142
syntax = "".join([value.strftime(token) if token in placeholders else token for token in new_tokens])
143
144
return value.strftime(syntax)
145
146
147
@app.template_filter("unixtime")
148
def unixtime(value: datetime):
149
return round(value.timestamp())
150
151
152
@app.template_filter("decode")
153
def decode(value: bytes, codec: str = "UTF-8", errors: str = "strict"):
154
return value.decode(codec, errors)
155
156
157
@app.template_filter("markdown")
158
def parse_markdown(value: str):
159
return Markup(markdown.make_html(markdown.tokenise(value)))
160
161
162
@app.template_filter("inline_markdown")
163
def parse_inline_markdown(value: str):
164
return Markup(markdown.make_html(markdown.parse_line(value)))
165
166
167
@app.template_filter("parse_diff_location")
168
def parse_diff_location(value: str):
169
header = value.split("@@")[1].strip()
170
return [tuple(int(j) for j in i.lstrip("-+").split(",")) for i in header.split(" ")]
171
172
173
@app.template_filter("harvester_protection")
174
def harvester_protection(value):
175
return "".join(f"&#x{ord(char):x};" for char in value)
176
177
178
@app.template_filter("sort")
179
def sort(value):
180
return sorted(value)
181