-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinja2.py
More file actions
105 lines (91 loc) · 3.63 KB
/
jinja2.py
File metadata and controls
105 lines (91 loc) · 3.63 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from django.templatetags.static import static
from django.urls import reverse
from django_jinja_markdown.templatetags.md import markdown
from jinja2 import Environment
from django.core.management.base import BaseCommand
from jssg.templatetags.filter_opengraph_metadata import filter_opengraph_metadata
from jssg.templatetags.functions_url import url_for_slug, url_for_slug_path
from django.conf import settings
import importlib
from django_jinja_markdown.extensions import MarkdownExtension
class JFMEMarkdownExtension(MarkdownExtension):
def _markdown_support(self, caller):
"""
Parse template with markdown.
:param caller: - caller of method;
:return: - parsed template.
"""
from textwrap import dedent # this import should be in the file head, of course
return self.environment.markdowner.convert(dedent(caller())).strip()
def environment(**options):
env = Environment(**options)
env.globals.update(
{
"static": static,
"url": reverse,
"markdown": markdown,
"url_for_slug": url_for_slug,
"url_for_slug_path" : url_for_slug_path
}
)
env.filters.update(
{
"filter_opengraph_metadata" : filter_opengraph_metadata
}
)
# HACK - 2024-08-28 - D.A. - We remove the "nl" extension because
# it inserts <br/> tags on every end of line.
env.markdowner.inlinePatterns.deregister('nl')
for templatetag in settings.JFME_ADDITIONAL_JINJA2_FUNCTIONS :
module_name, function_name = settings.JFME_ADDITIONAL_JINJA2_FUNCTIONS[templatetag].rsplit('.', 1)
try :
module = importlib.import_module(module_name)
function = getattr(module, function_name)
except(Exception) :
command = BaseCommand()
command.stdout.write(
command.style.ERROR(
"Error: JFME_ADDITIONAL_JINJA2_FUNCTIONS: couldn't import '" + settings.JFME_ADDITIONAL_JINJA2_FUNCTIONS[templatetag] +"'"
)
)
else :
if templatetag not in env.globals :
env.globals.update(
{
templatetag: function
}
)
else :
command = BaseCommand()
command.stdout.write(
command.style.ERROR(
"Error: JFME_ADDITIONAL_JINJA2_FUNCTIONS: '" + templatetag + "' function already exists"
)
)
for templatetag in settings.JFME_ADDITIONAL_JINJA2_FILTERS :
module_name, function_name = settings.JFME_ADDITIONAL_JINJA2_FILTERS[templatetag].rsplit('.', 1)
try :
module = importlib.import_module(module_name)
function = getattr(module, function_name)
except(Exception) :
command = BaseCommand()
command.stdout.write(
command.style.ERROR(
"Error: JFME_ADDITIONAL_JINJA2_FILTERS: couldn't import '" + settings.JFME_ADDITIONAL_JINJA2_FILTERS[templatetag] +"'"
)
)
else :
if templatetag not in env.filters :
env.filters.update(
{
templatetag: function
}
)
else :
command = BaseCommand()
command.stdout.write(
command.style.ERROR(
"Error: JFME_ADDITIONAL_JINJA2_FILTERS: '" + templatetag + "' filter already exists"
)
)
return env