Skip to content

Commit 7abb813

Browse files
Merge pull request #78 from patrickfournier/sitename-and-summary
Enhance OpenGraph tags
2 parents 31c56b9 + 1fb2620 commit 7abb813

File tree

7 files changed

+104
-3
lines changed

7 files changed

+104
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ Image: https://www.example.com/article-image.jpg
269269

270270
Based on [Open Graph protocol](https://ogp.me), the SEO plugin implements required properties and some aditionnals ones:
271271

272+
```
273+
<meta property="og:site_name" content=":sitename:" />
274+
```
275+
`:sitename:`: The `SITENAME` from the Pelican settings.
276+
272277
```
273278
<meta property="og:url" content=":fileurl:" />
274279
```
@@ -293,6 +298,7 @@ og_image: https://www.example.com/og-image.jpg
293298
```
294299

295300
If these metadata are not declared, `:title:`, `:description:`, `:image:` will be filled by the default `Title`, `Description` (Pelican metadata) and `Image` (plugin metadata) if they exist.
301+
If `Description` is not defined, a plain text version of `Summary` will be used instead.
296302

297303
```
298304
<meta property="og:locale" content=":language:">

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""HTML Enhancer : get instances of HTML enhancements."""
22

3+
from bs4 import BeautifulSoup
4+
35
from pelican.contents import Article, Page
46

57
from .article_schema_creator import ArticleSchemaCreator
@@ -9,6 +11,17 @@
911
from .twitter_cards import TwitterCards
1012

1113

14+
def _get_plain_text_summary(metadata):
15+
"""Get content from summary, without HTML tags."""
16+
17+
soup = BeautifulSoup(
18+
metadata.get("summary", ""),
19+
"html.parser",
20+
)
21+
text_summary = soup.get_text().strip()
22+
return text_summary
23+
24+
1225
class HTMLEnhancer:
1326
"""HTML Enhancer : get instances of HTML enhancements."""
1427

@@ -63,12 +76,14 @@ def __init__(self, file, output_path, path, open_graph=False, twitter_cards=Fals
6376

6477
if open_graph:
6578
self.open_graph = OpenGraph(
79+
sitename=_settings.get("SITENAME"),
6680
siteurl=_settings.get("SITEURL"),
6781
fileurl=_fileurl,
6882
file_type=_file_type,
6983
title=_metadata.get("og_title") or _title,
7084
description=_metadata.get("og_description")
71-
or _metadata.get("description"),
85+
or _metadata.get("description")
86+
or _get_plain_text_summary(_metadata),
7287
image=_metadata.get("og_image") or _image,
7388
locale=_settings.get("LOCALE"),
7489
)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class OpenGraph:
99
"""
1010

1111
def __init__(
12-
self, siteurl, fileurl, file_type, title, description, image, locale
12+
self, sitename, siteurl, fileurl, file_type, title, description, image, locale
1313
) -> None:
14+
self.sitename = sitename
1415
self.siteurl = siteurl
1516
self.fileurl = fileurl
1617
self.type = file_type
@@ -57,6 +58,9 @@ def create_tags(self) -> dict:
5758
"""
5859
open_graph_tags = {}
5960

61+
if self.sitename:
62+
open_graph_tags["site_name"] = self.sitename
63+
6064
open_graph_tags["url"] = self._create_absolute_fileurl()
6165
open_graph_tags["type"] = self.type
6266

pelican/plugins/seo/seo_report/seo_analyzer/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class SEOAnalyzer:
1111

1212
def __init__(self, article):
1313
self._title = getattr(article, "title", None)
14-
self._description = getattr(article, "description", None)
14+
self._description = getattr(article, "description", None) or getattr(
15+
article, "summary", None
16+
)
1517
self._content = getattr(article, "content", None)
1618
self._settings = getattr(article, "settings", None)
1719

pelican/plugins/seo/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def fake_article():
7575
"og_description": "OG Description",
7676
"og_image": "https://www.fakesite.com/og-image.jpg",
7777
"tw_account": "@TestTWCards",
78+
"summary": "Fake summary",
7879
}
7980
title = "Fake Title"
8081
description = "Fake description"

pelican/plugins/seo/tests/test_open_graph.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_create_absolute_fileurl(self, fake_article):
1717
"""
1818

1919
og = OpenGraph(
20+
sitename=None,
2021
siteurl=fake_article.settings["SITEURL"],
2122
fileurl=fake_article.url,
2223
file_type=None,
@@ -39,6 +40,7 @@ def test_create_absolute_fileurl_with_site_url_with_path(self, site_url, file_ur
3940
slash and a file URL with or without a leading slash properly.
4041
"""
4142
og = OpenGraph(
43+
sitename=None,
4244
siteurl=site_url,
4345
fileurl=file_url,
4446
file_type=None,
@@ -67,6 +69,7 @@ def test_get_locale(self, locale, expected_result):
6769
"""
6870

6971
og = OpenGraph(
72+
sitename=None,
7073
siteurl=None,
7174
fileurl=None,
7275
file_type=None,
@@ -86,6 +89,7 @@ def test_create_tags(self, fake_article):
8689
"""
8790

8891
og = OpenGraph(
92+
sitename=fake_article.settings["SITENAME"],
8993
siteurl=fake_article.settings["SITEURL"],
9094
fileurl=fake_article.url,
9195
file_type="article",
@@ -97,6 +101,7 @@ def test_create_tags(self, fake_article):
97101

98102
og_tags = og.create_tags()
99103

104+
assert og_tags["site_name"] == "Fake Site Name"
100105
assert og_tags["url"] == "https://www.fakesite.com/fake-title.html"
101106
assert og_tags["type"] == "article"
102107
assert og_tags["title"] == "OG Title"
@@ -111,6 +116,7 @@ def test_create_tags_missing_elements(self, fake_article_missing_elements):
111116
"""
112117

113118
og = OpenGraph(
119+
sitename=fake_article_missing_elements.settings["SITENAME"],
114120
siteurl=fake_article_missing_elements.settings["SITEURL"],
115121
fileurl=fake_article_missing_elements.url,
116122
file_type="article",
@@ -122,6 +128,7 @@ def test_create_tags_missing_elements(self, fake_article_missing_elements):
122128

123129
og_tags = og.create_tags()
124130

131+
assert "site_name" not in og_tags
125132
assert "title" not in og_tags
126133
assert "description" not in og_tags
127134
assert "image" not in og_tags

pelican/plugins/seo/tests/test_seo_enhancer.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,77 @@ def test_add_html_enhancements_to_file_with_open_graph(
233233
<link href="https://www.fakesite.com/fake-title.html" rel="canonical"/>
234234
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "Fake Site Name", "item": "https://www.fakesite.com"}, {"@type": "ListItem", "position": 2, "name": "Fake_file", "item": "https://www.fakesite.com/fake_file.html"}]}</script>
235235
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "Article", "author": {"@type": "Person", "name": "Fake author"}, "publisher": {"@type": "Organization", "name": "Fake Site Name", "logo": {"@type": "ImageObject", "url": "https://www.fakesite.com/fake-logo.jpg"}}, "headline": "Fake Title", "about": "Fake category", "datePublished": "2019-04-03 23:49"}</script>
236+
<meta content="Fake Site Name" property="og:site_name"/>
236237
<meta content="https://www.fakesite.com/fake-title.html" property="og:url"/>
237238
<meta content="website" property="og:type"/>
238239
<meta content="OG Title" property="og:title"/>
239240
<meta content="OG Description" property="og:description"/>
240241
<meta content="https://www.fakesite.com/og-image.jpg" property="og:image"/>
241242
<meta content="fr_FR" property="og:locale"/>
243+
</head>
244+
<body>
245+
<h1>Fake content title</h1>
246+
<p>Fake content 🙃</p>
247+
<a href="https://www.fakesite.com">Fake internal link</a>
248+
<p>Fake content with <code>inline code</code></p>
249+
<p>Fake content with "<a href="https://www.fakesite.com">Fake inline internal link</a>"</p>
250+
</body>
251+
</html>"""
252+
)
253+
254+
def test_add_html_enhancements_to_file_with_open_graph_using_summary_for_description(
255+
self, fake_article, fake_seo_enhancer
256+
):
257+
"""
258+
Test if add_html_to_file with open_graph setting
259+
adds Open Graph tags to HTML files.
260+
"""
261+
262+
# Remove higher priority values for the og:description tag to force the use of
263+
# the summary from article description.
264+
del fake_article.metadata["og_description"]
265+
del fake_article.description
266+
267+
path = "fake_output/fake_file.html"
268+
fake_html_enhancements = fake_seo_enhancer.launch_html_enhancer(
269+
file=fake_article,
270+
output_path="fake_output",
271+
path=path,
272+
open_graph=True,
273+
)
274+
275+
with patch(
276+
"seo.seo_enhancer.open", mock_open(read_data=fake_article.content)
277+
) as mocked_open:
278+
mocked_file_handle = mocked_open.return_value
279+
280+
fake_seo_enhancer.add_html_to_file(
281+
enhancements=fake_html_enhancements, path=path
282+
)
283+
assert len(mocked_open.call_args_list) == 2
284+
mocked_file_handle.read.assert_called_once()
285+
mocked_file_handle.write.assert_called_once()
286+
287+
write_args, _ = mocked_file_handle.write.call_args_list[0]
288+
fake_html_content = write_args[0]
289+
290+
# The og:description tag should now contain "Fake summary".
291+
assert (
292+
fake_html_content
293+
== """<html>
294+
<head>
295+
<title>Fake Title</title>
296+
<meta content="Fake description" name="description"/>
297+
<link href="https://www.fakesite.com/fake-title.html" rel="canonical"/>
298+
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "Fake Site Name", "item": "https://www.fakesite.com"}, {"@type": "ListItem", "position": 2, "name": "Fake_file", "item": "https://www.fakesite.com/fake_file.html"}]}</script>
299+
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "Article", "author": {"@type": "Person", "name": "Fake author"}, "publisher": {"@type": "Organization", "name": "Fake Site Name", "logo": {"@type": "ImageObject", "url": "https://www.fakesite.com/fake-logo.jpg"}}, "headline": "Fake Title", "about": "Fake category", "datePublished": "2019-04-03 23:49"}</script>
300+
<meta content="Fake Site Name" property="og:site_name"/>
301+
<meta content="https://www.fakesite.com/fake-title.html" property="og:url"/>
302+
<meta content="website" property="og:type"/>
303+
<meta content="OG Title" property="og:title"/>
304+
<meta content="Fake summary" property="og:description"/>
305+
<meta content="https://www.fakesite.com/og-image.jpg" property="og:image"/>
306+
<meta content="fr_FR" property="og:locale"/>
242307
</head>
243308
<body>
244309
<h1>Fake content title</h1>
@@ -295,6 +360,7 @@ def test_add_html_enhancements_to_file_with_twitter_cards(
295360
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "Article", "author": {"@type": "Person", "name": "Fake author"}, "publisher": {"@type": "Organization", "name": "Fake Site Name", "logo": {"@type": "ImageObject", "url": "https://www.fakesite.com/fake-logo.jpg"}}, "headline": "Fake Title", "about": "Fake category", "datePublished": "2019-04-03 23:49"}</script>
296361
<meta content="summary" name="twitter:card"/>
297362
<meta content="@TestTWCards" name="twitter:site"/>
363+
<meta content="Fake Site Name" property="og:site_name"/>
298364
<meta content="https://www.fakesite.com/fake-title.html" property="og:url"/>
299365
<meta content="website" property="og:type"/>
300366
<meta content="OG Title" property="og:title"/>

0 commit comments

Comments
 (0)