1
Fork 0

Add OpenGraph etc meta tags

This commit is contained in:
Jake Howard 2016-06-12 11:34:20 +01:00
parent 42559f20cc
commit 73db3f277d
Signed by: jake
GPG key ID: 57AFB45680EDD477
10 changed files with 120 additions and 19 deletions

View file

@ -58,8 +58,8 @@ FEED_ATOM = 'feed.atom'
FEED_DOMAIN = SITEURL FEED_DOMAIN = SITEURL
# Setup plugins # Setup plugins
PLUGIN_PATHS = ["pelican_plugins"] PLUGIN_PATHS = ["pelican_plugins", "plugins"]
PLUGINS = ["sitemap", "filetime_from_git", "pelican-jinja2content"] PLUGINS = ["sitemap", "filetime_from_git", "pelican-jinja2content", "open_graph"]
SITEMAP = { SITEMAP = {
"format": "xml" "format": "xml"

58
plugins/open_graph.py Normal file
View file

@ -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)

View file

@ -1,16 +1,18 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="{{ DEFAULT_LANG }}">
<head> <head>
<meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta http-equiv="Content-Language" content="en" />
<meta name="description" content="" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
<meta name="author" content="" /> <link rel="alternate" type="application/atom+xml" href="/feed.atom" />
<link rel="shortcut icon" href="/static/img/logo-transparent.png">
<title>{% block htmltitle %}Page{% endblock%} - {{ SITENAME }}</title> {% block metadata %}{% endblock %}
<link rel="stylesheet" href="/static/css/index.css" type="text/css"> <title>{% block htmltitle %}Page{% endblock %} - {{ SITENAME }}</title>
<link rel="stylesheet" href="/static/css/index.css" type="text/css" />
<link rel="shortcut icon" href="/static/img/logo-transparent.png" type="image/png" />
</head> </head>
<body id="page-top"> <body id="page-top">
{% block navbar %} {% block navbar %}
@ -21,9 +23,9 @@
{% include "extras/footer.html" %} {% include "extras/footer.html" %}
<script src="/static/js/jquery.js"></script> <script src="/static/js/jquery.js" type="text/javascript"></script>
<script src="/static/js/libs.js"></script> <script src="/static/js/libs.js" type="text/javascript"></script>
<script src="/static/js/app.js"></script> <script src="/static/js/app.js" type="text/javascript"></script>
<noscript> <noscript>
<style>html, body { display: none }</style> <style>html, body { display: none }</style>

View file

@ -4,6 +4,12 @@
{{ article.title }} {{ article.title }}
{% endblock %} {% endblock %}
{% block metadata %}
{% for tag, value in page.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %}
{% block content %} {% block content %}
<section class="bg-primary"> <section class="bg-primary">
<div class="container text-center text-uppercase"> <div class="container text-center text-uppercase">

View file

@ -4,6 +4,12 @@
All {{ category }} All {{ category }}
{% endblock %} {% endblock %}
{% block metadata %}
{% for tag, value in page.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %}
{% block content %} {% block content %}
<section class="bg-primary"> <section class="bg-primary">
<div class="container"> <div class="container">

View file

@ -4,6 +4,12 @@
{{ page.title }} {{ page.title }}
{% endblock %} {% endblock %}
{% block metadata %}
{% for tag, value in page.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %}
{% block content %} {% block content %}
{{ page.content }} {{ page.content }}
{% endblock %} {% endblock %}

View file

@ -1,15 +1,20 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta http-equiv="Content-Language" content="en" />
<meta name="description" content="" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
<meta name="author" content="" /> <link rel="alternate" type="application/atom+xml" href="/feed.atom" />
<title>{{ page.title }} | {{ SITENAME }}</title> {% for tag, value in page.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
<link rel="stylesheet" href="/static/css/index.css" type="text/css"> <title>{{ page.title }} - {{ SITENAME }}</title>
<link rel="stylesheet" href="/static/css/index.css" type="text/css" />
<link rel="shortcut icon" href="/static/img/logo-transparent.png" type="image/png" />
</head> </head>
<body> <body>
{{ page.content }} {{ page.content }}

View file

@ -4,6 +4,12 @@
{{ page.html_title or page.title }} {{ page.html_title or page.title }}
{% endblock %} {% endblock %}
{% block metadata %}
{% for tag, value in page.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %}
{% block content %} {% block content %}
<section class="bg-primary"> <section class="bg-primary">
<div class="container text-center text-uppercase"> <div class="container text-center text-uppercase">

View file

@ -4,6 +4,12 @@
{{ page.title }} {{ page.title }}
{% endblock %} {% endblock %}
{% block metadata %}
{% for tag, value in page.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %}
{% block content %} {% block content %}
{{ page.content }} {{ page.content }}
{% endblock %} {% endblock %}

View file

@ -4,6 +4,12 @@
{{ article.title }} {{ article.title }}
{% endblock %} {% endblock %}
{% block metadata %}
{% for tag, value in article.ogtags %}
<meta property="{{ tag }}" content="{{ value|striptags|e }}" />
{% endfor %}
{% endblock %}
{% block content %} {% block content %}
<section class="bg-primary"> <section class="bg-primary">
<div class="container text-center text-uppercase"> <div class="container text-center text-uppercase">