1
Fork 0

Use new tag system in templates

This commit is contained in:
Jake Howard 2016-07-29 08:09:02 +01:00
parent 20dd4d4739
commit 55e61f7875
Signed by: jake
GPG key ID: 57AFB45680EDD477
8 changed files with 38 additions and 48 deletions

View file

@ -54,7 +54,7 @@ FEED_DOMAIN = SITEURL
# Setup plugins # Setup plugins
PLUGIN_PATHS = ["pelican_plugins", "plugins"] PLUGIN_PATHS = ["pelican_plugins", "plugins"]
PLUGINS = ["sitemap", "filetime_from_git", "pelican-jinja2content", "open_graph", "autopages"] PLUGINS = ["sitemap", "filetime_from_git", "pelican-jinja2content", "metatags", "autopages"]
SITEMAP = { SITEMAP = {
"format": "xml" "format": "xml"

View file

@ -1,62 +1,56 @@
from pelican import signals from pelican import signals
from .links import accounts
import os.path import os.path
ACCOUNTS = accounts()
def map_og_tag(instance, prop, og_tag, default=''):
data = instance.metadata.get(prop, default)
if data:
instance.ogtags.append((og_tag, data))
return instance
def get_content_type(instance): def get_content_type(instance):
return type(instance).__name__ return type(instance).__name__
def get_twiter_tags(instance): def get_twiter_tags(instance):
return { return {
"twitter:card": "summary_large_image", "twitter:card": "summary_large_image",
"twitter:site": ACCOUNTS["twitter"].username, "twitter:site": instance.settings.get("ACCOUNTS")["twitter"].username,
"twitter:title": instance.metadata.get("title"), "twitter:title": instance.metadata.get("title", ""),
"twitter:description": instance.metadata.get("summary"), "twitter:description": instance.metadata.get("summary", ""),
"twitter:creator": ACCOUNTS["twitter"].username, "twitter:creator": instance.settings.get("ACCOUNTS")["twitter"].username,
"twitter:image": instance.metadata.get('image'), "twitter:image": instance.metadata.get("image", ""),
"twitter:image:alt": instance.metadata.get("summary"), "twitter:image:alt": instance.metadata.get("summary", ""),
"twitter:url": os.path.join(instance.settings.get("SITEURL", ""), instance.url) "twitter:url": os.path.join(instance.settings.get("SITEURL", ""), instance.url)
} }
def get_og_tags(instance): def get_og_tags(instance):
return { return {
"og:title": instance.metadata.get("title"), "og:title": instance.metadata.get("title", ""),
"og:type": get_content_type(instance).lower(), "og:type": get_content_type(instance).lower(),
"og:url": os.path.join(instance.settings.get("SITEURL"), instance.url), "og:url": os.path.join(instance.settings.get("SITEURL"), instance.url),
"og:image": instance.metadata.get("image"), "og:image": instance.metadata.get("image", ""),
"og:description": instance.metadata.get("description"), "og:description": instance.metadata.get("summary", ""),
"og:site_name": instance.settings.get("SITENAME"), "og:site_name": instance.settings.get("SITENAME"),
"og:locale": instance.metadata.get("locale", "en_GB") "og:locale": instance.metadata.get("locale", "en_GB")
} }
def get_schema_tags(instance): def get_schema_tags(instance):
return { return {
"name": instance.metadata.get("title"), "name": instance.metadata.get("title", ""),
"description": instance.metadata.get("description"), "description": instance.metadata.get("summary", ""),
"image": instance.metadata.get("image") "image": instance.metadata.get("image", "")
} }
def get_general_tags(instance): def get_general_tags(instance):
return { return {
"article:author": instance.settings.get("AUTHOR"), "article:author": instance.settings.get("AUTHOR"),
"article:modified_time": instance.metadata.get("modified"), "article:modified_time": instance.metadata.get("modified", ""), # Set build time as default?
"article:published_time": instance.metadata.get("date"), "article:published_time": instance.metadata.get("date", ""),
"article:section": instance.category.name if hasattr(instance, "category") else "" "article:section": instance.category.name if hasattr(instance, "category") else "",
"description": instance.metadata.get("description"), "description": instance.metadata.get("summary", ""),
"author": instance.metadata.get("author", instance.settings.get("AUTHOR")), "author": instance.metadata.get("author", instance.settings.get("AUTHOR")),
"canonical": instance.gettings.get("SITEURL") "canonical": instance.settings.get("SITEURL")
} }
def tag_item(instance): def tag_item(instance):
instance_type = get_content_type(instance) instance_type = get_content_type(instance)
@ -66,16 +60,22 @@ def tag_item(instance):
metatags = [] metatags = []
for tag, value in get_twiter_tags(instance).items(): for tag, value in get_twiter_tags(instance).items():
if not value:
continue
metatags.append( metatags.append(
"<meta name='{0}' content='{1}' />".format(tag, value) "<meta name='{0}' content='{1}' />".format(tag, value)
) )
for tag, value in get_og_tags(instance).items(): for tag, value in get_og_tags(instance).items():
if not value:
continue
metatags.append( metatags.append(
"<meta property='{0}' content='{1}' />".format(tag, value) "<meta property='{0}' content='{1}' />".format(tag, value)
) )
for tag, value in get_schema_tags(instance).items(): for tag, value in get_schema_tags(instance).items():
if not value:
continue
metatags.append( metatags.append(
"<meta itemprop='{0}' content='{1}' />".format(tag, value) "<meta itemprop='{0}' content='{1}' />".format(tag, value)
) )
@ -86,11 +86,13 @@ def tag_item(instance):
general_tags.append(('article:tag', tag.name)) general_tags.append(('article:tag', tag.name))
for tag, value in general_tags: for tag, value in general_tags:
if not value:
continue
metatags.append( metatags.append(
"<meta name='{0}' content='{1}' />".format(tag, value) "<meta name='{0}' content='{1}' />".format(tag, value)
) )
instance.metatags = metatags instance.metatags = '\n'.join(metatags)
def register(): def register():

View file

@ -5,9 +5,7 @@
{% endblock %} {% endblock %}
{% block metadata %} {% block metadata %}
{% for tag, value in article.ogtags %} {{ article.metatags }}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View file

@ -5,9 +5,7 @@
{% endblock %} {% endblock %}
{% block metadata %} {% block metadata %}
{% for tag, value in article.ogtags %} {{ article.metatags }}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View file

@ -5,9 +5,7 @@
{% endblock %} {% endblock %}
{% block metadata %} {% block metadata %}
{% for tag, value in page.ogtags %} {{ page.metatags }}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View file

@ -5,9 +5,7 @@
{% endblock %} {% endblock %}
{% block metadata %} {% block metadata %}
{% for tag, value in page.ogtags %} {{ article.metatags }}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %} {% endblock %}
{% block navbar %} {% block navbar %}

View file

@ -5,9 +5,7 @@
{% endblock %} {% endblock %}
{% block metadata %} {% block metadata %}
{% for tag, value in page.ogtags %} {{ page.metatags }}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View file

@ -5,9 +5,7 @@
{% endblock %} {% endblock %}
{% block metadata %} {% block metadata %}
{% for tag, value in article.ogtags %} {{ article.metatags }}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}