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
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 = {
"format": "xml"

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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