Skip to content

Commit 9bcf931

Browse files
Generate OpenGraph tags for Article content type.
Additionally, adds a setting for mapping author names to X (Twitter) accounts, to be consistent with Facebook profiles.
1 parent be79bd4 commit 9bcf931

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

pelican/plugins/seo/seo_enhancer/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def launch_html_enhancer(
4343
if open_graph:
4444
html_enhancements["open_graph"] = html_enhancer.open_graph.create_tags()
4545

46+
if open_graph and hasattr(html_enhancer, "open_graph_article"):
47+
html_enhancements["open_graph_article"] = (
48+
html_enhancer.open_graph_article.create_tags()
49+
)
50+
4651
if twitter_cards:
4752
html_enhancements["twitter_cards"] = (
4853
html_enhancer.twitter_cards.create_tags()
@@ -125,6 +130,18 @@ def add_html_to_file(self, enhancements, path):
125130
for og_property, og_content in enhancements["open_graph"].items():
126131
self._add_meta_tag(soup, "property", "og", og_property, og_content)
127132

133+
if "open_graph_article" in enhancements:
134+
for og_property, og_content in enhancements["open_graph_article"].items():
135+
if isinstance(og_content, str):
136+
self._add_meta_tag(
137+
soup, "property", "article", og_property, og_content
138+
)
139+
elif isinstance(og_content, list):
140+
for element in og_content:
141+
self._add_meta_tag(
142+
soup, "property", "article", og_property, element
143+
)
144+
128145
with open(path, "w", encoding="utf8") as html_file:
129146
html_file.write(str(soup))
130147

pelican/plugins/seo/seo_enhancer/html_enhancer/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .article_schema_creator import ArticleSchemaCreator
88
from .breadcrumb_schema_creator import BreadcrumbSchemaCreator
99
from .canonical_url_creator import CanonicalURLCreator
10-
from .open_graph import OpenGraph
10+
from .open_graph import OpenGraph, OpenGraphArticle
1111
from .twitter_cards import TwitterCards
1212

1313

@@ -88,7 +88,25 @@ def __init__(self, file, output_path, path, open_graph=False, twitter_cards=Fals
8888
locale=_settings.get("LOCALE"),
8989
)
9090

91+
if isinstance(file, Article):
92+
_modified = getattr(file, "modified", None)
93+
_author_profiles = _settings.get(
94+
"SEO_ENHANCER_AUTHOR_FACEBOOK_PROFILE", {}
95+
)
96+
_author_profile = _author_profiles.get(_author.name, None)
97+
self.open_graph_article = OpenGraphArticle(
98+
date=_date,
99+
modified=_modified,
100+
category=_category.name,
101+
tags=getattr(file, "tags", []),
102+
author=_metadata.get("fb_profile") or _author_profile,
103+
)
104+
91105
if twitter_cards:
106+
_author_profiles = _settings.get(
107+
"SEO_ENHANCER_AUTHOR_TWITTER_PROFILE", {}
108+
)
109+
_author_profile = _author_profiles.get(_author.name, None)
92110
self.twitter_cards = TwitterCards(
93-
tw_account=_metadata.get("tw_account"),
111+
tw_account=_metadata.get("tw_account") or _author_profile,
94112
)

pelican/plugins/seo/seo_enhancer/html_enhancer/open_graph.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import locale
22
from urllib import parse
33

4+
from pelican.utils import strftime
5+
46

57
class OpenGraph:
68
"""
@@ -78,3 +80,34 @@ def create_tags(self) -> dict:
7880
open_graph_tags["locale"] = locale
7981

8082
return open_graph_tags
83+
84+
85+
class OpenGraphArticle:
86+
"""
87+
Get all Open Graph elements for an Article according to
88+
https://ogp.me/?#no_vertical.
89+
"""
90+
91+
def __init__(self, date, modified, category, tags, author) -> None:
92+
self.date = date
93+
self.modified = modified
94+
self.category = category
95+
self.tags = tags
96+
self.author = author
97+
98+
def create_tags(self) -> dict:
99+
"""
100+
Compute all Open Graph elements for Article and return them in a ready-to-use
101+
dict.
102+
"""
103+
tags = {}
104+
tags["published_time"] = strftime(self.date, "%Y-%m-%d")
105+
if self.modified is not None:
106+
tags["modified_time"] = strftime(self.modified, "%Y-%m-%d")
107+
if self.category is not None:
108+
tags["section"] = self.category
109+
if self.tags is not None and self.tags:
110+
tags["tags"] = self.tags
111+
if self.author is not None:
112+
tags["author"] = self.author
113+
return tags

0 commit comments

Comments
 (0)