-
Notifications
You must be signed in to change notification settings - Fork 8
/
open_graph.py
125 lines (98 loc) · 3.59 KB
/
open_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*- #
"""Open Graph.
This plugin adds Open Graph Protocol tags to articles.
Use like this in your base.html template:
.. code-block:: jinja2
{% if article %}
{% for tag in article.ogtags %}
<meta property="{{tag[0]}}" content="{{tag[1]|striptags|e}}" />
{% endfor %}
{% endif %}
{% if page %}
{% for tag in page.ogtags %}
<meta property="{{tag[0]}}" content="{{tag[1]|striptags|e}}" />
{% endfor %}
{% endif %}
"""
from __future__ import unicode_literals
import os.path
from bs4 import BeautifulSoup
from pelican import generators
from pelican import signals
from pelican.utils import strftime
def open_graph_tag_articles(content_generators):
"""Get different Pelican objects."""
for generator in content_generators:
if isinstance(generator, generators.ArticlesGenerator):
for article in (
generator.articles +
generator.translations +
generator.drafts):
open_graph_tag(article)
elif isinstance(generator, generators.PagesGenerator):
for page in generator.pages:
open_graph_tag(page)
return True
def open_graph_tag(item): # pylint: disable=too-many-branches
"""Open Graph items."""
ogtags = [('og:title', item.title),
('og:type', 'article')]
image = item.metadata.get('og_image', '')
if image:
ogtags.append(('og:image', image))
else:
soup = BeautifulSoup(
item.content,
'html.parser')
img_links = soup.find_all('img')
if img_links:
img_src = img_links[0].get('src')
if "http" not in img_src:
if item.settings.get('SITEURL', ''):
img_src = item.settings.get('SITEURL', '') + "/" + img_src
ogtags.append(('og:image', img_src))
url = os.path.join(
item.settings.get(
'SITEURL',
''),
item.url)
ogtags.append(('og:url', url))
default_summary = item.summary
description = item.metadata.get('og_description', default_summary)
ogtags.append(('og:description', description))
default_locale = item.settings.get('LOCALE', [])
if len(default_locale[0]) > 3:
default_locale = default_locale[0]
ogtags.append(
('og:locale', item.metadata.get('og_locale', default_locale)))
ogtags.append(('og:site_name', item.settings.get('SITENAME', '')))
if hasattr(item, 'date'):
ogtags.append(('article:published_time',
strftime(item.date, "%Y-%m-%d")))
if hasattr(item, 'modified'):
ogtags.append(('article:modified_time', strftime(
item.modified, "%Y-%m-%d")))
if hasattr(item, 'related_posts'):
for related_post in item.related_posts:
url = os.path.join(
item.settings.get(
'SITEURL',
''),
related_post.url)
ogtags.append(('og:see_also', url))
author_fb_profiles = item.settings.get('AUTHOR_FB_ID', {})
if author_fb_profiles:
for author in item.authors:
if author.name in author_fb_profiles:
ogtags.append(
('article:author', author_fb_profiles[author.name]))
ogtags.append(('article:section', item.category.name))
try:
for tag in item.tags:
ogtags.append(('article:tag', tag.name))
except AttributeError:
pass
item.ogtags = ogtags
def register():
"""Register pelican plugin."""
signals.all_generators_finalized.connect(open_graph_tag_articles)