Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom filters #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 0 additions & 35 deletions jinja2content.py

This file was deleted.