1
Fork 0

restructure metatagging

This commit is contained in:
Jake Howard 2016-07-29 07:31:57 +01:00
parent f30747fc3d
commit 20dd4d4739
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,6 +1,9 @@
from pelican import signals
from .links import accounts
import os.path import os.path
from pelican import signals
ACCOUNTS = accounts()
def map_og_tag(instance, prop, og_tag, default=''): def map_og_tag(instance, prop, og_tag, default=''):
@ -13,6 +16,46 @@ def map_og_tag(instance, prop, og_tag, default=''):
def get_content_type(instance): def get_content_type(instance):
return type(instance).__name__ 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:url": os.path.join(instance.settings.get("SITEURL", ""), instance.url)
}
def get_og_tags(instance):
return {
"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: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")
}
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"),
"author": instance.metadata.get("author", instance.settings.get("AUTHOR")),
"canonical": instance.gettings.get("SITEURL")
}
def tag_item(instance): def tag_item(instance):
instance_type = get_content_type(instance) instance_type = get_content_type(instance)
@ -20,38 +63,34 @@ def tag_item(instance):
if instance_type not in ['Article', 'Page', 'Draft']: if instance_type not in ['Article', 'Page', 'Draft']:
return return
instance.ogtags = [ metatags = []
('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') for tag, value in get_twiter_tags(instance).items():
instance = map_og_tag(instance, 'image', 'og:image') metatags.append(
instance = map_og_tag(instance, 'summary', 'og:description') "<meta name='{0}' content='{1}' />".format(tag, value)
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') for tag, value in get_og_tags(instance).items():
instance = map_og_tag(instance, 'title', 'twitter:title') metatags.append(
instance = map_og_tag(instance, 'summary', 'twitter:description') "<meta property='{0}' content='{1}' />".format(tag, value)
)
if hasattr(instance, 'category'): for tag, value in get_schema_tags(instance).items():
instance.ogtags.append(('article:section', instance.category.name)) metatags.append(
"<meta itemprop='{0}' content='{1}' />".format(tag, value)
)
general_tags = get_general_tags(instance).items()
if hasattr(instance, 'tags'): if hasattr(instance, 'tags'):
for tag in instance.tags: for tag in instance.tags:
instance.ogtags.append(('article:tag', tag.name)) general_tags.append(('article:tag', tag.name))
instance.ogtags.append(('og:locale', instance.metadata.get('locale', 'en_GB'))) for tag, value in general_tags:
metatags.append(
"<meta name='{0}' content='{1}' />".format(tag, value)
)
# Add non-og tags instance.metatags = metatags
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(): def register():