#!/usr/bin/env python3 """migrate_to_v2_assets.py — point the content at the v2 artwork filenames. The v2 renderer (render_from_art.mjs) writes JPEG/WebP, not PNG: public/banners/.jpg (hero fallback; derives .webp) public/og/.jpg (EN share card) public/og/zh-.jpg (ZH share card) Until this migration runs, every page still points at the v1 PNG paths, so it has to happen in the SAME commit as the images or the site serves 404 heroes. Idempotent: it only rewrites values that still look like the v1 paths, keeps each file's quoting style, and prints what it changed. Run with --write to apply. python scripts/og-gen/migrate_to_v2_assets.py # report only python scripts/og-gen/migrate_to_v2_assets.py --write """ from __future__ import annotations import argparse import re from pathlib import Path ROOT = Path(__file__).resolve().parents[2] POSTS = ROOT / "src" / "content" / "posts" ZH = POSTS / "zh" # ogImage: "/og/.png" or ogImage: /og/.png (quotes optional) OG_RE = re.compile(r'^(ogImage:[ \t]*)(?P"?)/og/(?P[a-z0-9-]+)\.png(?P=q)[ \t]*$', re.M) BANNER_RE = re.compile(r'^(banner:[ \t]*)(?P"?)/banners/(?P[a-z0-9-]+)\.png(?P=q)[ \t]*$', re.M) def migrate(path: Path, zh: bool, write: bool) -> list[str]: text = path.read_text(encoding="utf-8") slug = path.stem changes: list[str] = [] def og_sub(m: re.Match) -> str: target = f"/og/zh-{slug}.jpg" if zh else f"/og/{slug}.jpg" changes.append(f"ogImage {m.group(3)}.png -> {target}") return f"{m.group(1)}{m.group('q')}{target}{m.group('q')}" def banner_sub(m: re.Match) -> str: target = f"/banners/{slug}.jpg" changes.append(f"banner {m.group(3)}.png -> {target}") return f"{m.group(1)}{m.group('q')}{target}{m.group('q')}" new = OG_RE.sub(og_sub, text) new = BANNER_RE.sub(banner_sub, new) if new != text and write: path.write_text(new, encoding="utf-8") return changes def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--write", action="store_true") args = ap.parse_args() total = 0 files_changed = 0 for folder, zh in ((POSTS, False), (ZH, True)): for p in sorted(folder.glob("*.md")): ch = migrate(p, zh, args.write) if ch: files_changed += 1 total += len(ch) print(f"{'WROTE' if args.write else 'would'} {p.relative_to(ROOT)}: " + "; ".join(ch)) print(f"\n{files_changed} files, {total} field(s) {'' if args.write else '(report only — pass --write)'}") if __name__ == "__main__": main()