From 1b7f3070fbd7790932d7a2a4a6f23894d86a416c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 13 Jan 2017 17:35:32 +0000 Subject: [PATCH] Fix common instance variable --- pelicanconf.py | 5 +++- plugins/include_with.py | 53 +++++++++++++++++++++++++++++++++++ theme/templates/article.html | 2 +- theme/templates/blog.html | 2 +- theme/templates/page.html | 2 +- theme/templates/projects.html | 2 +- 6 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 plugins/include_with.py diff --git a/pelicanconf.py b/pelicanconf.py index 8e49039..e014f9d 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -101,5 +101,8 @@ JINJA_FILTERS = { JINJA_ENVIRONMENT = { 'trim_blocks': True, 'lstrip_blocks': True, - 'extensions': {} + 'extensions': [ + 'jinja2.ext.with_', + 'plugins.include_with.IncludeWith' + ] } diff --git a/plugins/include_with.py b/plugins/include_with.py new file mode 100644 index 0000000..3380bda --- /dev/null +++ b/plugins/include_with.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +""" + # Jinja-IncludeWith + + A Jinja2 preprocessor extension that let you update the `include` + context like this: + + {% include "something.html" with foo=bar %} + {% include "something.html" with a=3, b=2+2, c='yes' %} + + You **must** also include 'jinja2.ext.with_' in the extensions list. + + :copyright: [Juan-Pablo Scaletti] (http://jpscaletti.com). + :license: [MIT] (http://www.opensource.org/licenses/mit-license.php). + + Copied from https://github.com/jpscaletti/jinja-includewith due to not pip-installable + +""" +import re + +from jinja2.ext import Extension + + +__version__ = '0.1' + +rx = re.compile(r'\{\%\s*include\s+(?P[^\s]+)\s+with\s+(?P.*?)\s*\%\}', + re.IGNORECASE) + + +class IncludeWith(Extension): + + def preprocess(self, source, name, filename=None): + lastpos = 0 + while 1: + m = rx.search(source, lastpos) + if not m: + break + + lastpos = m.end() + d = m.groupdict() + context = d['context'].strip() + if context == 'context': + continue + + source = ''.join([ + source[:m.start()], + '{% with ', context, ' %}', + '{% include ', d['tmpl'].strip(), ' %}', + '{% endwith %}', + source[m.end():] + ]) + + return source diff --git a/theme/templates/article.html b/theme/templates/article.html index 3c284f7..82d27a4 100644 --- a/theme/templates/article.html +++ b/theme/templates/article.html @@ -9,7 +9,7 @@ {% endblock %} {% block content %} - {% include 'extras/header.html' with context %} + {% include 'extras/header.html' with instance=article %}
{{ article.content }} diff --git a/theme/templates/blog.html b/theme/templates/blog.html index 20b657c..a6ed1d1 100644 --- a/theme/templates/blog.html +++ b/theme/templates/blog.html @@ -9,7 +9,7 @@ {% endblock %} {% block content %} - {% include 'extras/header.html' with context %} + {% include 'extras/header.html' with instance=article %}

diff --git a/theme/templates/page.html b/theme/templates/page.html index 1aa0531..2e16bbd 100644 --- a/theme/templates/page.html +++ b/theme/templates/page.html @@ -10,7 +10,7 @@ {% block content %} {% if not page.no_container %} - {% include 'extras/header.html' with context %} + {% include 'extras/header.html' with instance=page %}

{{ page.content }} diff --git a/theme/templates/projects.html b/theme/templates/projects.html index 73d4962..486b0c6 100644 --- a/theme/templates/projects.html +++ b/theme/templates/projects.html @@ -9,7 +9,7 @@ {% endblock %} {% block content %} - {% include 'extras/header.html' with context %} + {% include 'extras/header.html' with instance=article %}