-
Notifications
You must be signed in to change notification settings - Fork 1
/
jinja2content.py
71 lines (55 loc) · 2.18 KB
/
jinja2content.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
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
"""
jinja2content.py
----------------
DONT EDIT THIS FILE
Pelican plugin that processes Markdown files as jinja templates.
"""
from jinja2 import Environment, FileSystemLoader, ChoiceLoader
import os
from pelican import signals
from pelican.readers import MarkdownReader, HTMLReader, RstReader
from pelican.utils import pelican_open
from tempfile import NamedTemporaryFile
class JinjaContentMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# will look first in 'JINJA2CONTENT_TEMPLATES', by default the
# content root path, then in the theme's templates
local_dirs = self.settings.get('JINJA2CONTENT_TEMPLATES', ['.'])
local_dirs = [os.path.join(self.settings['PATH'], folder)
for folder in local_dirs]
theme_dir = os.path.join(self.settings['THEME'], 'templates')
loaders = [FileSystemLoader(_dir) for _dir
in local_dirs + [theme_dir]]
if 'JINJA_ENVIRONMENT' in self.settings: # pelican 3.7
jinja_environment = self.settings['JINJA_ENVIRONMENT']
else:
jinja_environment = {
'trim_blocks': True,
'lstrip_blocks': True,
'extensions': self.settings['JINJA_EXTENSIONS']
}
self.env = Environment(
loader=ChoiceLoader(loaders),
**jinja_environment)
def read(self, source_path):
with pelican_open(source_path) as text:
text = self.env.from_string(text).render()
with NamedTemporaryFile(delete=False) as f:
f.write(text.encode())
f.close()
content, metadata = super().read(f.name)
os.unlink(f.name)
return content, metadata
class JinjaMarkdownReader(JinjaContentMixin, MarkdownReader):
pass
class JinjaRstReader(JinjaContentMixin, RstReader):
pass
class JinjaHTMLReader(JinjaContentMixin, HTMLReader):
pass
def add_reader(readers):
for Reader in [JinjaMarkdownReader, JinjaRstReader, JinjaHTMLReader]:
for ext in Reader.file_extensions:
readers.reader_classes[ext] = Reader
def register():
signals.readers_init.connect(add_reader)