From 55e61f78750d96209663ccef95374d8bd66a4e66 Mon Sep 17 00:00:00 2001 From: RealOrangeOne Date: Fri, 29 Jul 2016 08:09:02 +0100 Subject: [PATCH] Use new tag system in templates --- pelicanconf.py | 2 +- plugins/{open_graph.py => metatags.py} | 60 +++++++++++++------------- theme/templates/article.html | 4 +- theme/templates/blog.html | 4 +- theme/templates/category.html | 4 +- theme/templates/page-home.html | 4 +- theme/templates/page.html | 4 +- theme/templates/projects.html | 4 +- 8 files changed, 38 insertions(+), 48 deletions(-) rename plugins/{open_graph.py => metatags.py} (62%) diff --git a/pelicanconf.py b/pelicanconf.py index 64a0ad1..ab16a3e 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -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" diff --git a/plugins/open_graph.py b/plugins/metatags.py similarity index 62% rename from plugins/open_graph.py rename to plugins/metatags.py index 878ef34..3213b72 100644 --- a/plugins/open_graph.py +++ b/plugins/metatags.py @@ -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( "".format(tag, value) ) for tag, value in get_og_tags(instance).items(): + if not value: + continue metatags.append( "".format(tag, value) ) for tag, value in get_schema_tags(instance).items(): + if not value: + continue metatags.append( "".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( "".format(tag, value) ) - instance.metatags = metatags + instance.metatags = '\n'.join(metatags) def register(): diff --git a/theme/templates/article.html b/theme/templates/article.html index d4b44b7..3c284f7 100644 --- a/theme/templates/article.html +++ b/theme/templates/article.html @@ -5,9 +5,7 @@ {% endblock %} {% block metadata %} - {% for tag, value in article.ogtags %} - - {% endfor %} + {{ article.metatags }} {% endblock %} {% block content %} diff --git a/theme/templates/blog.html b/theme/templates/blog.html index 5514565..20b657c 100644 --- a/theme/templates/blog.html +++ b/theme/templates/blog.html @@ -5,9 +5,7 @@ {% endblock %} {% block metadata %} - {% for tag, value in article.ogtags %} - - {% endfor %} + {{ article.metatags }} {% endblock %} {% block content %} diff --git a/theme/templates/category.html b/theme/templates/category.html index bbd76a7..efdd86a 100644 --- a/theme/templates/category.html +++ b/theme/templates/category.html @@ -5,9 +5,7 @@ {% endblock %} {% block metadata %} - {% for tag, value in page.ogtags %} - - {% endfor %} + {{ page.metatags }} {% endblock %} {% block content %} diff --git a/theme/templates/page-home.html b/theme/templates/page-home.html index 750084e..e33e109 100644 --- a/theme/templates/page-home.html +++ b/theme/templates/page-home.html @@ -5,9 +5,7 @@ {% endblock %} {% block metadata %} - {% for tag, value in page.ogtags %} - - {% endfor %} + {{ article.metatags }} {% endblock %} {% block navbar %} diff --git a/theme/templates/page.html b/theme/templates/page.html index b5c8f53..1aa0531 100644 --- a/theme/templates/page.html +++ b/theme/templates/page.html @@ -5,9 +5,7 @@ {% endblock %} {% block metadata %} - {% for tag, value in page.ogtags %} - - {% endfor %} + {{ page.metatags }} {% endblock %} {% block content %} diff --git a/theme/templates/projects.html b/theme/templates/projects.html index e8fb995..0fa4cef 100644 --- a/theme/templates/projects.html +++ b/theme/templates/projects.html @@ -5,9 +5,7 @@ {% endblock %} {% block metadata %} - {% for tag, value in article.ogtags %} - - {% endfor %} + {{ article.metatags }} {% endblock %} {% block content %}