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

 extra_jinja.py

View raw Download
text/x-script.python • 6.93 kiB
Python script, Unicode text, UTF-8 text executable
        
            
1
import math
2
import datetime
3
4
def init_filters(site):
5
@site.filter("first_paragraph")
6
def first_paragraph(value):
7
return value.split("\n\n")[0]
8
@site.filter("split")
9
def split(value, sep, maxsplit=-1):
10
return value.split(sep, maxsplit)
11
@site.filter("rsplit")
12
def rsplit(value, sep, maxsplit=-1):
13
return value.rsplit(sep, maxsplit)
14
@site.filter("partition")
15
def partition(value, sep):
16
return value.partition(sep)
17
@site.filter("rpartition")
18
def rpartition(value, sep):
19
return value.rpartition(sep)
20
@site.filter("lstrip")
21
def lstrip(value, chars=None):
22
return value.lstrip(chars)
23
@site.filter("rstrip")
24
def rstrip(value, chars=None):
25
return value.rstrip(chars)
26
@site.filter("strip")
27
def strip(value, chars=None):
28
return value.strip(chars)
29
@site.filter("removeprefix")
30
def removeprefix(value, prefix):
31
return value.removeprefix(prefix)
32
@site.filter("removesuffix")
33
def removesuffix(value, suffix):
34
return value.removesuffix(suffix)
35
@site.filter("remove")
36
def remove(value, string):
37
return value.replace(string, "")
38
@site.filter("strftime")
39
def strftime(value: datetime.datetime | datetime.date | datetime.time, format_):
40
return value.strftime(format_)
41
@site.filter("unixtime")
42
def unixtime(value: datetime.datetime | datetime.date | datetime.time):
43
return round(value.timestamp())
44
@site.filter("strptime")
45
def strptime(value, format_):
46
return datetime.datetime.strptime(value, format_)
47
@site.filter("round")
48
def round_(value, decimals=0):
49
return round(value, decimals)
50
@site.filter("floor")
51
def floor(value):
52
return math.floor(value)
53
@site.filter("ceiling")
54
def ceiling(value):
55
return math.ceil(value)
56
@site.filter("units")
57
def units(value, decimals=2, scale=1024,
58
suffixes=("B", "kiB", "MiB", "GiB", "TiB", "PiB")):
59
for unit in suffixes:
60
if value < scale:
61
break
62
value /= scale
63
if int(value) == value:
64
return int(value) + "\u202f" + unit
65
return round(value * 10 ** decimals) / 10 ** decimals + "\u202f" + unit
66
@site.filter("conditional")
67
def conditional(value, true_value, false_value):
68
return true_value if value else false_value
69
@site.filter("debug_log_value")
70
def debug_log_value(value):
71
print(value)
72
return value
73
@site.filter("harvester_protection")
74
def harvester_protection(value):
75
return "".join(f"&#x{ord(char):x};" for char in value)
76
@site.filter("pretty_number")
77
def pretty_number(value, separator="\u202f"):
78
return f"{value:,}".replace(",", separator)
79
@site.filter("hex")
80
def hex_(value):
81
return hex(value).removeprefix("0x")
82
@site.filter("oct")
83
def oct_(value):
84
return oct(value).removeprefix("0o")
85
@site.filter("bin")
86
def bin_(value):
87
return bin(value).removeprefix("0b")
88
@site.filter("join")
89
def join(value, separator=" "):
90
return separator.join(value)
91
@site.filter("replace")
92
def replace(value, old, new):
93
return value.replace(old, new)
94
@site.filter("file_stat")
95
def file_stat(value):
96
return value.stat()
97
@site.filter("path_cat")
98
def path_cat(*value):
99
return os.path.join(*value)
100
@site.filter("path_dirname")
101
def path_dirname(value):
102
return os.path.dirname(value)
103
@site.filter("path_basename")
104
def path_basename(value):
105
return os.path.basename(value)
106
@site.filter("path_splitext")
107
def path_splitext(value):
108
return os.path.splitext(value)
109
@site.filter("type")
110
def type_(value):
111
return type(value)
112
@site.filter("type_name")
113
def type_name(value):
114
return type(value).__name__
115
@site.filter("each_nth")
116
def nth(value, step, start=0):
117
return value[start::step]
118
@site.filter("key_list")
119
def key_list(value):
120
return list(value.keys())
121
@site.filter("value_list")
122
def value_list(value):
123
return list(value.values())
124
@site.filter("item_list")
125
def item_list(value):
126
return list(value.items())
127
@site.filter("remove_dupes")
128
def remove_dupes(value):
129
list = []
130
for i in value:
131
if i not in list:
132
list.append(i)
133
return list
134
@site.filter("percent")
135
def percent(value, maximum, decimals=2):
136
return round(value * maximum / 100, decimals) + "%"
137
@site.filter("percent_of")
138
def percent_of(value, total, decimals=2):
139
return round(value / total * 100, decimals) + "%"
140
@site.filter("permille")
141
def permille(value, maximum, decimals=2):
142
return round(value * maximum / 1000, decimals) + "‰"
143
@site.filter("permille_of")
144
def permille_of(value, total, decimals=2):
145
return round(value / total * 1000, decimals) + "‰"
146
@site.filter("timezone")
147
def timezone(value, timezone):
148
return value.astimezone(timezone)
149
150
def init_tests(site):
151
@site.test("instance_of")
152
def isinstance_(value, type_):
153
return isinstance(value, type_)
154
@site.test("only_chars")
155
def only_chars(value, chars):
156
return set(value).issubset(set(chars))
157
@site.test("empty")
158
def is_empty(value):
159
return not len(value)
160
@site.test("not_empty")
161
def is_not_empty(value):
162
return len(value)
163
@site.test("past_date")
164
def past_date(value):
165
return value < datetime.datetime.now()
166
@site.test("future_date")
167
def future_date(value):
168
return value > datetime.datetime.now()
169
@site.test("numeric")
170
def is_numeric(value):
171
try:
172
float(value)
173
return True
174
except ValueError:
175
return False
176
@site.test("startswith")
177
def startswith(value, prefix):
178
return value.startswith(prefix)
179
@site.test("endswith")
180
def endswith(value, suffix):
181
return value.endswith(suffix)
182
@site.test("matches_regex")
183
def matches_regex(value, pattern):
184
return re.match(pattern, value) is not None
185
@site.test("is_callable")
186
def is_callable(value):
187
return callable(value)
188
@site.test("all")
189
def all_(value):
190
return all(value)
191
@site.test("any")
192
def any_(value):
193
return any(value)
194
@site.test("longer_than")
195
def longer_than(value, length):
196
return len(value) > length
197
@site.test("shorter_than")
198
def shorter_than(value, length):
199
return len(value) < length
200
@site.test("weekend")
201
def weekend(value):
202
return value.weekday() >= 5
203
@site.test("weekday")
204
def weekday(value):
205
return value.weekday() < 5
206
@site.test("leap_year")
207
def leap_year(value):
208
return value.year % 4 == 0 and (value.year % 100 != 0 or value.year % 400 == 0)
209
@site.test("almost_equal")
210
def almost_equal(value, other, tolerance=1e-6):
211
return abs(value - other) < tolerance
212