diff --git a/__init__.py b/__init__.py index de025c4..565e9f2 100644 --- a/__init__.py +++ b/__init__.py @@ -1 +1,42 @@ -from .jinja2content import * +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__)) + jinja2_env = Environment( + autoescape=True, + trim_blocks=True, + lstrip_blocks=True, + loader=ChoiceLoader([ + FileSystemLoader( + os.path.join(base_path, instance.settings['THEME'], 'templates') + ), + FileSystemLoader( + os.path.join(base_path, instance.settings['PATH']) + ) + ]), + extensions=instance.settings['JINJA_EXTENSIONS'], + ) + + if 'JINJA_FILTERS' in instance.settings: + 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 + + instance._content = jinja2_template.render(**kwargs) + + +def register(): + signals.content_object_init.connect(execjinja2) diff --git a/jinja2content.py b/jinja2content.py deleted file mode 100644 index d0f2aae..0000000 --- a/jinja2content.py +++ /dev/null @@ -1,35 +0,0 @@ -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__)) - jinja2_env = Environment( - trim_blocks=True, - lstrip_blocks=True, - loader=ChoiceLoader([ - FileSystemLoader( - os.path.join(base_path, instance.settings['THEME'], - 'templates') - ), - ]), - extensions=instance.settings['JINJA_EXTENSIONS'], - ) - 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 - - instance._content = jinja2_template.render(**kwargs) - - -def register(): - signals.content_object_init.connect(execjinja2)