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