1
|
|
|
""" |
2
|
|
|
Custom template tag filters. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
import bleach |
6
|
|
|
import markdown |
7
|
|
|
from bleach_whitelist import markdown_tags, markdown_attrs, print_tags |
8
|
|
|
|
9
|
|
|
from django import template |
10
|
|
|
from django.utils.safestring import mark_safe |
11
|
|
|
from django.contrib.messages import constants as messages |
12
|
|
|
|
13
|
|
|
register = template.Library() |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@register.filter(name='is_list') |
17
|
|
|
def is_list(variable): |
18
|
|
|
return isinstance(variable, list) |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@register.filter(name='markdown2html') |
22
|
|
|
def markdown2html(md_str): |
23
|
|
|
""" |
24
|
|
|
Returns markdown string as HTML. |
25
|
|
|
""" |
26
|
|
|
if md_str is None: |
27
|
|
|
md_str = '' |
28
|
|
|
|
29
|
|
|
rendered_md = markdown.markdown(md_str, |
30
|
|
|
extensions=[ |
31
|
|
|
'markdown.extensions.fenced_code', |
32
|
|
|
'markdown.extensions.nl2br', |
33
|
|
|
'markdown.extensions.tables', |
34
|
|
|
]) |
35
|
|
|
html = bleach.clean(rendered_md, |
36
|
|
|
markdown_tags + print_tags, |
37
|
|
|
markdown_attrs) |
38
|
|
|
return mark_safe(html) # nosec:B308:blacklist |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
@register.filter(name='message_icon') |
42
|
|
|
def message_icon(msg): |
43
|
|
|
""" |
44
|
|
|
Returns the string class name of a message icon |
45
|
|
|
which feeds directly into Patternfly. |
46
|
|
|
""" |
47
|
|
|
icons = { |
48
|
|
|
messages.ERROR: 'error-circle-o', |
49
|
|
|
messages.WARNING: 'warning-triangle-o', |
50
|
|
|
messages.SUCCESS: 'ok', |
51
|
|
|
messages.INFO: 'info', |
52
|
|
|
} |
53
|
|
|
return 'pficon-' + icons[msg.level] |
54
|
|
|
|