From 830e9dbc17396444b501c4e4eddd9a96a511a03b Mon Sep 17 00:00:00 2001 From: prokaktus Date: Mon, 19 Jun 2017 02:17:41 +0300 Subject: [PATCH] add jinja2 support --- AUTHORS.rst | 1 + docs/models.rst | 21 ++++++++++++++ meta/jinja2.py | 27 ++++++++++++++++++ meta/jinja2/meta/meta.html | 56 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 meta/jinja2.py create mode 100644 meta/jinja2/meta/meta.html diff --git a/AUTHORS.rst b/AUTHORS.rst index 1e78f6f..0f1862b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -27,6 +27,7 @@ Contributors * Leonardo Cavallucci * m-vdb * Marco Federighi +* Maxim Filipenko * Mirat Can Bayrak * Murat Aydos * mvergerdelbove diff --git a/docs/models.rst b/docs/models.rst index d21dc62..a808111 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -112,6 +112,27 @@ Usage +#. For Jinja2 support, configure your environment:: + + + from jinja2 import Environment + from meta.jinja2 import MetaProxy + + ... + env = Environment(**options) + env.globals.update({ + ... + 'meta_global': MetaProxy() + }) + + + And put macro into your templates:: + + {% from 'meta/meta.html' import meta_macro with context %} + {{ meta_macro() }} + + Jinja2 support is experimental for now, so feel free to tell about any issues. + Note ++++ diff --git a/meta/jinja2.py b/meta/jinja2.py new file mode 100644 index 0000000..a1106c2 --- /dev/null +++ b/meta/jinja2.py @@ -0,0 +1,27 @@ +from functools import wraps + +from django.core.exceptions import ImproperlyConfigured + +from .templatetags import meta + +try: + from jinja2 import Markup +except ImportError: + Markup = None + + +class MetaProxy(object): + + def __init__(self, *args, **kwargs): + if Markup is None: + raise ImproperlyConfigured('Jinja2 package is not installed') + + def __getattr__(self, item): + f = getattr(meta, item) + + @wraps(f) + def wrapped(*args, **kwargs): + tag = f(*args, **kwargs) + return Markup(tag) + + return wrapped diff --git a/meta/jinja2/meta/meta.html b/meta/jinja2/meta/meta.html new file mode 100644 index 0000000..28b023a --- /dev/null +++ b/meta/jinja2/meta/meta.html @@ -0,0 +1,56 @@ +{% macro meta_macro() -%} + {% if meta is defined %} + {% if meta.description %}{{ meta_global.meta('description', meta.description) }}{% endif %} + {% if meta.keywords %}{{ meta_global.meta_list('keywords', meta.keywords) }}{% endif %} + {% if meta.extra_props %}{{ meta_global.meta_extras(meta.extra_props) }}{% endif %} + {% if meta.extra_custom_props %}{{ meta_global.custom_meta_extras(meta.extra_custom_props) }}{% endif %} + {% if meta.use_title_tag %} + {% if meta.title %}{{ meta_global.title_prop(meta.title) }}{% endif %} + {% endif %} + {% if meta.use_og %} + {% if meta.title %}{{ meta_global.og_prop('title', meta.title) }}{% endif %} + {% if meta.url %}{{ meta_global.og_prop('url', meta.url) }}{% endif %} + {% if meta.og_description %}{{ meta_global.og_prop('description', meta.og_description) }} + {% elif meta.description %}{{ meta_global.og_prop('description', meta.description) }}{% endif %} + {% if meta.image %}{{ meta_global.og_prop('image', meta.image) }}{% endif %} + {% if meta.og_type %}{{ meta_global.og_prop('type', meta.og_type) }} + {% elif meta.object_type %}{{ meta_global.og_prop('type', meta.object_type) }}{% endif %} + {% if meta.site_name %}{{ meta_global.og_prop('site_name', meta.site_name) }}{% endif %} + {% if meta.og_author_url %}{{ meta_global.generic_prop('article', 'author', meta.og_author_url) }} + {% elif meta.og_author %}{{ meta_global.generic_prop('article', 'author', meta.og_author) }}{% endif %} + {% if meta.published_time %}{{ meta_global.generic_prop('article', 'published_time', meta.published_time) }}{% endif %} + {% if meta.modified_time %}{{ meta_global.generic_prop('article', 'modified_time', meta.modified_time) }}{% endif %} + {% if meta.expiration_time %}{{ meta_global.generic_prop('article', 'expiration_time', meta.expiration_time) }}{% endif %} + {% if meta.og_publisher %}{{ meta_global.generic_prop('article', 'publisher', meta.og_publisher) }}{% endif %} + {% if meta.og_app_id %}{{ meta_global.facebook_prop('app_id', meta.og_app_id) }} + {% elif meta.facebook_app_id %}{{ meta_global.facebook_prop('app_id', meta.facebook_app_id) }}{% endif %} + {% if meta.fb_pages %}{{ meta_global.facebook_prop('pages', meta.fb_pages) }}{% endif %} + {% if meta.og_profile_id %}{{ meta_global.facebook_prop('profile_id', meta.og_profile_id) }}{% endif %} + {% if meta.tag %}{{ meta_global.generic_prop('article', 'tag', meta.tag) }}{% endif %} + {% if meta.locale %}{{ meta_global.og_prop('locale', meta.locale) }}{% endif %} + {% endif %} + {% if meta.use_twitter %} + {{ meta_global.twitter_prop('domain', meta.get_domain) }} + {% if meta.twitter_type %}{{ meta_global.twitter_prop('card', meta.twitter_type) }} + {% elif meta.twitter_card %}{{ meta_global.twitter_prop('card', meta.twitter_card) }}{% endif %} + {% if meta.title %}{{ meta_global.twitter_prop('title', meta.title) }}{% endif %} + {% if meta.url %}{{ meta_global.twitter_prop('url', meta.url) }}{% endif %} + {% if meta.twitter_description %}{{ meta_global.twitter_prop('description', meta.twitter_description) }} + {% elif meta.description %}{{ meta_global.twitter_prop('description', meta.description) }}{% endif %} + {% if meta.image %}{{ meta_global.twitter_prop('image', meta.image) }}{% endif %} + {% if meta.twitter_creator %}{{ meta_global.twitter_prop('creator', meta.twitter_creator) }}{% endif %} + {% if meta.twitter_site %}{{ meta_global.twitter_prop('site', meta.twitter_site) }}{% endif %} + {% endif %} + {% if meta.use_googleplus %} + {% if meta.gplus_author %}{% endif %} + {% if meta.gplus_publisher %}{% endif %} + {% if meta.title %}{{ meta_global.googleplus_prop('name', meta.title) }}{% endif %} + {% if meta.published_time %}{{ meta_global.googleplus_prop('datePublished', meta.published_time) }}{% endif %} + {% if meta.modified_time %}{{ meta_global.googleplus_prop('dateModified', meta.modified_time) }}{% endif %} + {% if meta.url %}{{ meta_global.googleplus_prop('url', meta.url) }}{% endif %} + {% if meta.gplus_description %}{{ meta_global.googleplus_prop('description', meta.gplus_description) }} + {% elif meta.description %}{{ meta_global.googleplus_prop('description', meta.description) }}{% endif %} + {% if meta.image %}{{ meta_global.googleplus_prop('image', meta.image) }}{% endif %} + {% endif %} + {% endif %} +{%- endmacro %}