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