#!/usr/bin/env python3 import os, re, time, sys from datetime import datetime, timezone # IN = "./src/feed.html" # OUT = "./src/feed.rss" IN = sys.argv[1] OUT = sys.argv[2] def fmt_rfc2822(ts: float) -> str: return datetime.fromtimestamp(ts, timezone.utc).strftime("%a, %d %b %Y %H:%M:%S %z") # Read Typst-generated HTML with open(IN, encoding="utf-8") as f: html = f.read() # Extract blocks blocks = re.findall(r"(.*?)", html, re.DOTALL) posts = [] for block in blocks: url_match = re.search(r"

(.*?)

", block) title_match = re.search(r"(.*?)", block) if not (url_match and title_match): continue url = url_match.group(1).strip() title = title_match.group(1).strip() # Determine file mod time (fallback = now) filename = "./src/" + os.path.basename(url.replace("html", "typ")) print(f"adding {filename}") ts = os.path.getmtime(filename) if os.path.exists(filename) else time.time() posts.append({ "title": title, "url": url, "ts": ts, "pubDate": fmt_rfc2822(ts) }) # Sort newest first posts.sort(key=lambda p: p["ts"], reverse=True) rss_items = "\n".join( f""" {p["title"]} {p["url"]} {p["pubDate"]} """ for p in posts ) # Build RSS rss = f""" 0x4200.cafe https://0x4200.cafe/ Latest posts from 0x4200.cafe {fmt_rfc2822(time.time())} {rss_items} """ # Write out with open(OUT, "w", encoding="utf-8") as f: f.write(rss) print(f"✅ Built {OUT} with {len(posts)} items (sorted newest first)")