-
Notifications
You must be signed in to change notification settings - Fork 6
/
filters.py
38 lines (27 loc) · 930 Bytes
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright 2018-2024 Threema GmbH, all rights reserved.
import re
import commonmark as CommonMark
from jinja2 import escape
MODEL_TYPE_RE = re.compile(r'^([^<]*)<([^>]*)>$')
def commonmark(value):
parser = CommonMark.Parser()
ast = parser.parse(value)
renderer = CommonMark.HtmlRenderer()
html = renderer.render(ast)
return html
def linkmodels(value: str, models, direct: bool = False):
linked_models = [m[0] for m in models]
match = MODEL_TYPE_RE.match(value)
if match:
(wrapper, model) = match.groups()
if direct:
link = 'model-{}.html'.format(escape(model).lower())
else:
link = '#model.{}'.format(escape(model))
if model in linked_models:
return '{0}<<a href="{1}">{2}</a>>'.format(
escape(wrapper),
link,
escape(model),
)
return escape(value)