From f0974806e66894d1865d076d01b7023d3a129b5c Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 22 Nov 2026 14:18:53 +0100 Subject: [PATCH] fix format of atom feeds (#301) * fix format of atom feeds * Update post_build.py --- scripts/post_build.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/post_build.py b/scripts/post_build.py index 7b5e2fe0e..d2915a125 100644 --- a/scripts/post_build.py +++ b/scripts/post_build.py @@ -10,6 +10,8 @@ import json import re import shutil import sys +from datetime import timezone +from email.utils import parsedate_to_datetime from pathlib import Path import markdown @@ -27,6 +29,24 @@ def plain_text(text: str) -> str: return re.sub(r'<[^>]+>', '', markdown.markdown(text)) +def reformat_feed_dates(feed: Path) -> int: + """Rewrite dates form RSS to Atom (ISO 8601).""" + pattern = re.compile( + r'(<(?:pubDate|lastBuildDate)\b[^>]*>)([^<]*)()' + ) + content = feed.read_text(encoding='utf-8') + + def replace(match: re.Match[str]) -> str: + date = parsedate_to_datetime(match.group(2).strip()) + if date.tzinfo is None: + date = date.replace(tzinfo=timezone.utc) + return f'{match.group(1)}{date.isoformat(sep=" ", timespec="seconds")}{match.group(3)}' + + content, reformatted = pattern.subn(replace, content) + feed.write_text(content, encoding='utf-8') + return reformatted + + def build_plugins_json(): """Write the plugin and publisher list to plugins.json.""" plugins = {} @@ -81,7 +101,8 @@ def copy_legacy_feeds(): src = site_dir.joinpath(blog, 'feed_rss_created.xml') dst = site_dir.joinpath(blog, 'feed.atom') shutil.copyfile(src, dst) - print(f'Copied {src} -> {dst}') + count = reformat_feed_dates(dst) + print(f'Copied {src} -> {dst}; Reformatted {count} dates in target') if __name__ == '__main__':