1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
theorangeone.net-legacy/plugins/open_graph.py

59 lines
2 KiB
Python
Raw Normal View History

2016-06-12 11:34:20 +01:00
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)