diff --git a/pelicanconf.py b/pelicanconf.py index d47fe65..c7abdfc 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -58,8 +58,8 @@ FEED_ATOM = 'feed.atom' FEED_DOMAIN = SITEURL # Setup plugins -PLUGIN_PATHS = ["pelican_plugins"] -PLUGINS = ["sitemap", "filetime_from_git", "pelican-jinja2content"] +PLUGIN_PATHS = ["pelican_plugins", "plugins"] +PLUGINS = ["sitemap", "filetime_from_git", "pelican-jinja2content", "open_graph"] SITEMAP = { "format": "xml" diff --git a/plugins/open_graph.py b/plugins/open_graph.py new file mode 100644 index 0000000..510de33 --- /dev/null +++ b/plugins/open_graph.py @@ -0,0 +1,58 @@ +import os.path + +from pelican import signals + + +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 tag_item(instance): + instance_type = get_content_type(instance) + + if instance_type not in ['Article', 'Page', 'Draft']: + return + + instance.ogtags = [ + ('og:type', instance_type), + ('og:url', os.path.join(instance.settings.get('SITEURL', ''), instance.url)), + ('twitter:url', os.path.join(instance.settings.get('SITEURL', ''), instance.url)), + ('twitter:card', 'summary'), + ('og:site_name', instance.settings.get('SITENAME', '')), + ] + + instance = map_og_tag(instance, 'title', 'og:title') + instance = map_og_tag(instance, 'image', 'og:image') + instance = map_og_tag(instance, 'summary', 'og:description') + instance = map_og_tag(instance, 'author', 'article:author', instance.settings.get('AUTHOR')) + instance = map_og_tag(instance, 'modified', 'article:modified_time') + instance = map_og_tag(instance, 'date', 'article:published_time') + + instance = map_og_tag(instance, 'image', 'twitter:image') + instance = map_og_tag(instance, 'title', 'twitter:title') + instance = map_og_tag(instance, 'summary', 'twitter:description') + + if hasattr(instance, 'category'): + instance.ogtags.append(('article:section', instance.category.name)) + + if hasattr(instance, 'tags'): + for tag in instance.tags: + instance.ogtags.append(('article:tag', tag.name)) + + instance.ogtags.append(('og:locale', instance.metadata.get('locale', 'en_GB'))) + + # Add non-og tags + instance = map_og_tag(instance, 'summary', 'description') + instance = map_og_tag(instance, 'author', 'author', instance.settings.get('AUTHOR')) + instance.ogtags.append(('canonical', instance.settings.get('SITEURL'))) + + +def register(): + signals.content_object_init.connect(tag_item) diff --git a/theme/templates/base.html b/theme/templates/base.html index 0f666f2..371c03b 100644 --- a/theme/templates/base.html +++ b/theme/templates/base.html @@ -1,16 +1,18 @@ - +
- + - - - - + + + -