1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
theorangeone.net-legacy/plugins/pelican-jinja2content.py

39 lines
1.2 KiB
Python
Raw Normal View History

2017-01-13 08:55:25 +00:00
import os
from pelican import signals
from pelican import contents
from jinja2 import Environment, ChoiceLoader, FileSystemLoader
def execjinja2(instance):
if type(instance) in (contents.Article, contents.Page):
base_path = os.path.dirname(os.path.abspath(__file__))
2017-01-13 08:58:23 +00:00
jinja2_env = Environment( # nosec
2017-01-13 08:55:25 +00:00
loader=ChoiceLoader([
FileSystemLoader(
os.path.join(base_path, instance.settings['THEME'], 'templates')
),
FileSystemLoader(
os.path.join(base_path, instance.settings['PATH'])
)
]),
**instance.settings['JINJA_ENVIRONMENT'],
)
jinja2_env.filters.update(instance.settings['JINJA_FILTERS'])
jinja2_template = jinja2_env.from_string(instance._content)
kwargs = instance._context
if type(instance) is contents.Article:
kwargs['article'] = instance
elif type(instance) is contents.Page:
kwargs['page'] = instance
2017-01-13 09:07:12 +00:00
kwargs['instance'] = instance # avoid using outside common elements
2017-01-13 08:55:25 +00:00
instance._content = jinja2_template.render(**kwargs)
def register():
signals.content_object_init.connect(execjinja2)