Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set ChangeFreq and Priority via metadata #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ SITEMAP = {
}
```

Using Metadata
--------------

In addition to applying a configuration to all articles/pages using the `SITEMAP` setting, `ChangeFreq` and `Priority` can also be specified as metadata for individual articles/pages. The same restrictions on the values apply:

* Valid options for `ChangeFreq` are `always`, `hourly`, `daily`, `weekly`, `monthly`, `yearly` and `never`.
* Valid options for `Priority` must be a decimal number between `0` and `1`.

**Example**

Following is an example of using sitemap-related metadata in a Markdown file:

```
Title: Frequently Changed Article
ChangeFreq: daily
Priority: 0.3

This is the article content.
```

Contributing
------------

Expand Down
19 changes: 17 additions & 2 deletions pelican/plugins/sitemap/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,23 @@ def is_excluded(item):
if isinstance(obj, contents.Page)
else "indexes"
)
changefreq = changefreqs[content_type]
priority = float(priorities[content_type])

# see if changefreq specified in metadata headers; fall back to config
changefreq = getattr(obj, "changefreq", changefreqs[content_type])
if changefreq not in CHANGEFREQ_VALUES:
log.error(f"sitemap: Invalid 'changefreqs' value: {changefreq!r}")
changefreq = changefreqs[content_type]

# see if priority specified in metadata headers; fall back to config
priority_raw = getattr(obj, "priority", priorities[content_type])
try:
priority = float(priority_raw)
except ValueError:
log.exception(
f"sitemap: Require numeric priority. Got: {priority_raw!r}"
)
priority = priorities[content_type]

translations = "".join(
XML_TRANSLATION.format(
trans.lang,
Expand Down
9 changes: 9 additions & 0 deletions pelican/plugins/sitemap/test_data/article2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Title: Test post daily
Date: 2023-07-12 13:00:00
Category: test
Tags: foo, bar, foobar
Summary: Daily testing is my main function in life.
ChangeFreq: daily
Priority: 0.3

This is the article content.
11 changes: 11 additions & 0 deletions pelican/plugins/sitemap/test_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_txt(self):
http://localhost/tag/foo.html
http://localhost/tag/foobar.html
http://localhost/tags.html
http://localhost/test-post-daily.html
http://localhost/test-post.html
"""
self.assertEqual(expected, contents)
Expand All @@ -68,5 +69,15 @@ def test_xml(self):
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
"""
self.assertIn(needle, contents)

needle = """\
<url>
<loc>http://localhost/test-post-daily.html</loc>
<lastmod>2023-07-12T13:00:00+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.3</priority>
</url>
"""
self.assertIn(needle, contents)