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/plain • 6.38 kiB
Python script, ASCII text executable
        
            
1
"""
2
This module provides filters and other utilities for the Jinja2 templating engine.
3
4
Roundabout - git hosting for everyone <https://roundabout-host.com>
5
Copyright (C) 2023-2025 Roundabout developers <root@roundabout-host.com>
6
7
This program is free software: you can redistribute it and/or modify
8
it under the terms of the GNU Affero General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU Affero General Public License for more details.
16
17
You should have received a copy of the GNU Affero General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""
20
21
from app import app, _, n_
22
import flask
23
from datetime import datetime
24
import markdown
25
from markupsafe import Markup
26
from urllib.parse import urlencode
27
28
29
@app.template_global()
30
def modify_query(**new_values):
31
args = flask.request.args.copy()
32
33
for key, value in new_values.items():
34
args[key] = value
35
36
return f"{flask.request.path}?{urlencode(args)}"
37
38
39
@app.template_filter("split")
40
def split(value: str, separator=" ", maxsplit: int = -1):
41
return value.split(separator, maxsplit)
42
43
44
@app.template_filter("splitlines")
45
def splitlines(value: str, keepends: bool = False):
46
return value.splitlines(keepends=keepends)
47
48
49
@app.template_filter("lstrip")
50
def lstrip(value: str, characters=None):
51
return value.lstrip(characters)
52
53
54
@app.template_filter("rstrip")
55
def rstrip(value: str, characters=None):
56
return value.rstrip(characters)
57
58
59
@app.template_filter("strftime")
60
def strftime(value: datetime, syntax: str):
61
# Split the strftime syntax into placeholders and literals.
62
placeholders = {
63
"%a", "%A", "%w", "%d", "%-d", "%b", "%B", "%m", "%-m", "%y", "%Y",
64
"%H", "%-H", "%I", "%-I", "%p", "%M", "%-M", "%S", "%-S", "%f", "%z",
65
"%Z", "%j", "%-j", "%U", "%W", "%c", "%x", "%X", "%%", "%G", "%u", "%V",
66
"%-V", "%s", "%:z", "%e"
67
}
68
69
tokens = []
70
i = 0
71
length = len(syntax)
72
while i < length:
73
if syntax[i] == "%":
74
if i+1 < length and syntax[i:i + 2] in placeholders:
75
tokens.append(syntax[i:i + 2])
76
i += 2
77
elif i+1 < length and syntax[i:i + 3] in placeholders:
78
tokens.append(syntax[i:i + 3])
79
i += 3
80
else:
81
tokens.append(syntax[i])
82
i += 1
83
else:
84
tokens.append(syntax[i])
85
i += 1
86
87
new_tokens = []
88
89
for token in tokens:
90
match token:
91
case "%A":
92
# Translate the full weekday name.
93
weekday_number = value.weekday()
94
weekday_names = [
95
_("Monday"),
96
_("Tuesday"),
97
_("Wednesday"),
98
_("Thursday"),
99
_("Friday"),
100
_("Saturday"),
101
_("Sunday")
102
]
103
new_tokens.append(weekday_names[weekday_number])
104
case "%a":
105
# Translate the abbreviated weekday name.
106
weekday_number = value.weekday()
107
weekday_names = [
108
_("Mon"),
109
_("Tue"),
110
_("Wed"),
111
_("Thu"),
112
_("Fri"),
113
_("Sat"),
114
_("Sun")
115
]
116
new_tokens.append(weekday_names[weekday_number])
117
case "%B":
118
# Translate the full month name.
119
month_number = value.month
120
month_names = [
121
_("January"),
122
_("February"),
123
_("March"),
124
_("April"),
125
_("May"),
126
_("June"),
127
_("July"),
128
_("August"),
129
_("September"),
130
_("October"),
131
_("November"),
132
_("December")
133
]
134
new_tokens.append(month_names[month_number - 1])
135
case "%b":
136
# Translate the abbreviated month name.
137
month_number = value.month
138
month_names = [
139
_("Jan"),
140
_("Feb"),
141
_("Mar"),
142
_("Apr"),
143
_("May (short)"),
144
_("Jun"),
145
_("Jul"),
146
_("Aug"),
147
_("Sep"),
148
_("Oct"),
149
_("Nov"),
150
_("Dec")
151
]
152
new_tokens.append(month_names[month_number - 1])
153
case "%p":
154
# Translate the AM/PM indicator.
155
if value.hour < 12:
156
new_tokens.append(_("am"))
157
else:
158
new_tokens.append(_("pm"))
159
case _:
160
new_tokens.append(token)
161
162
syntax = "".join([value.strftime(token) if token in placeholders else token for token in new_tokens])
163
164
return value.strftime(syntax)
165
166
167
@app.template_filter("unixtime")
168
def unixtime(value: datetime):
169
return round(value.timestamp())
170
171
172
@app.template_filter("decode")
173
def decode(value: bytes, codec: str = "UTF-8", errors: str = "strict"):
174
return value.decode(codec, errors)
175
176
177
@app.template_filter("markdown")
178
def parse_markdown(value: str):
179
return Markup(markdown.make_html(markdown.tokenise(value)))
180
181
182
@app.template_filter("inline_markdown")
183
def parse_inline_markdown(value: str):
184
return Markup(markdown.make_html(markdown.parse_line(value)))
185
186
187
@app.template_filter("parse_diff_location")
188
def parse_diff_location(value: str):
189
header = value.split("@@")[1].strip()
190
return [tuple(int(j) for j in i.lstrip("-+").split(",")) for i in header.split(" ")]
191
192
193
@app.template_filter("harvester_protection")
194
def harvester_protection(value):
195
return "".join(f"&#x{ord(char):x};" for char in value)
196
197
198
@app.template_filter("sort")
199
def sort(value):
200
return sorted(value)
201
202
@app.template_filter("profile_url")
203
def profile_url(value):
204
if "@" in value:
205
host = value.split("@")[1]
206
user = value.split("@")[0]
207
return f"http://{host}/users/{user}"
208
else:
209
return f"/{value}"
210