From 770cfe7156f5da284b8249d70939c9609a5601fa Mon Sep 17 00:00:00 2001 From: Declan Gaylo Date: Thu, 4 Jan 2024 00:16:24 -0500 Subject: [PATCH 1/3] * Set priority and changefreq using metadata * Added tests for metadata * Update README.md to reflect new feature --- README.md | 19 +++++++++++++++++++ RELEASE.md | 3 +++ pelican/plugins/sitemap/sitemap.py | 19 +++++++++++++++++-- pelican/plugins/sitemap/test_data/article2.md | 9 +++++++++ pelican/plugins/sitemap/test_sitemap.py | 11 +++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 RELEASE.md create mode 100644 pelican/plugins/sitemap/test_data/article2.md diff --git a/README.md b/README.md index ed709b9..37907b9 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,25 @@ SITEMAP = { } ``` +Using Metadata +-------------- + +In addition to applying a configuration to all articles/pages using the `SITEMAP`, `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** + +Here is an example of using metadata in a Markdown file: + +``` +Title: Frequently Changed Article +ChangeFreq: daily +Priority: 0.3 + +This is the article content. +``` + Contributing ------------ diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..496521c --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,3 @@ +Release type: minor + +Allows changefreq and priority to be set by page/article metadata diff --git a/pelican/plugins/sitemap/sitemap.py b/pelican/plugins/sitemap/sitemap.py index 4ea2b0b..b260124 100644 --- a/pelican/plugins/sitemap/sitemap.py +++ b/pelican/plugins/sitemap/sitemap.py @@ -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, fail 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, fail 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, diff --git a/pelican/plugins/sitemap/test_data/article2.md b/pelican/plugins/sitemap/test_data/article2.md new file mode 100644 index 0000000..a4bdf7a --- /dev/null +++ b/pelican/plugins/sitemap/test_data/article2.md @@ -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. diff --git a/pelican/plugins/sitemap/test_sitemap.py b/pelican/plugins/sitemap/test_sitemap.py index 2a69990..bf27b23 100644 --- a/pelican/plugins/sitemap/test_sitemap.py +++ b/pelican/plugins/sitemap/test_sitemap.py @@ -51,6 +51,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) @@ -66,5 +67,15 @@ def test_xml(self): monthly 0.5 +""" + self.assertIn(needle, contents) + + needle = """\ + +http://localhost/test-post-daily.html +2023-07-12T13:00:00+00:00 +daily +0.3 + """ self.assertIn(needle, contents) From 137e9063ce92235f9d2b0cc7b06729df5b59bd9d Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Tue, 5 Nov 2024 09:16:50 +0100 Subject: [PATCH 2/3] Delete RELEASE.md --- RELEASE.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md deleted file mode 100644 index 496521c..0000000 --- a/RELEASE.md +++ /dev/null @@ -1,3 +0,0 @@ -Release type: minor - -Allows changefreq and priority to be set by page/article metadata From b3d1211d5f70b2ee36c3fee2920585ba8fd68750 Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Tue, 5 Nov 2024 09:23:12 +0100 Subject: [PATCH 3/3] Minor tweaks --- README.md | 5 +++-- pelican/plugins/sitemap/sitemap.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e8cd24b..b65733f 100644 --- a/README.md +++ b/README.md @@ -87,13 +87,14 @@ SITEMAP = { Using Metadata -------------- -In addition to applying a configuration to all articles/pages using the `SITEMAP`, `ChangeFreq` and `Priority` can also be specified as metadata for individual articles/pages. The same restrictions on the values apply: +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** -Here is an example of using metadata in a Markdown file: +Following is an example of using sitemap-related metadata in a Markdown file: ``` Title: Frequently Changed Article diff --git a/pelican/plugins/sitemap/sitemap.py b/pelican/plugins/sitemap/sitemap.py index 5a68e55..036193e 100644 --- a/pelican/plugins/sitemap/sitemap.py +++ b/pelican/plugins/sitemap/sitemap.py @@ -157,13 +157,13 @@ def is_excluded(item): else "indexes" ) - # see if changefreq specified in metadata headers, fail back to config + # 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, fail back to config + # see if priority specified in metadata headers; fall back to config priority_raw = getattr(obj, "priority", priorities[content_type]) try: priority = float(priority_raw)