restructure metatagging
This commit is contained in:
parent
f30747fc3d
commit
20dd4d4739
1 changed files with 64 additions and 25 deletions
|
@ -1,6 +1,9 @@
|
|||
from pelican import signals
|
||||
from .links import accounts
|
||||
import os.path
|
||||
|
||||
from pelican import signals
|
||||
|
||||
ACCOUNTS = accounts()
|
||||
|
||||
|
||||
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):
|
||||
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):
|
||||
instance_type = get_content_type(instance)
|
||||
|
@ -20,38 +63,34 @@ def tag_item(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', '')),
|
||||
]
|
||||
metatags = []
|
||||
|
||||
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')
|
||||
for tag, value in get_twiter_tags(instance).items():
|
||||
metatags.append(
|
||||
"<meta name='{0}' content='{1}' />".format(tag, value)
|
||||
)
|
||||
|
||||
instance = map_og_tag(instance, 'image', 'twitter:image')
|
||||
instance = map_og_tag(instance, 'title', 'twitter:title')
|
||||
instance = map_og_tag(instance, 'summary', 'twitter:description')
|
||||
for tag, value in get_og_tags(instance).items():
|
||||
metatags.append(
|
||||
"<meta property='{0}' content='{1}' />".format(tag, value)
|
||||
)
|
||||
|
||||
if hasattr(instance, 'category'):
|
||||
instance.ogtags.append(('article:section', instance.category.name))
|
||||
for tag, value in get_schema_tags(instance).items():
|
||||
metatags.append(
|
||||
"<meta itemprop='{0}' content='{1}' />".format(tag, value)
|
||||
)
|
||||
|
||||
general_tags = get_general_tags(instance).items()
|
||||
if hasattr(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 = 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')))
|
||||
instance.metatags = metatags
|
||||
|
||||
|
||||
def register():
|
||||
|
|
Reference in a new issue