"""Shared helpers for the trpgphysics.org build scripts (loaded by path, because `python -I` does not put the script folder on sys.path). Header/footer text is copied exactly from the existing pages.""" import html, pathlib, re NAV_ITEMS = [ ("index.html", "Home"), ("theory.html", "The theory"), ("equation.html", "Equation"), ("results.html", "Results"), ("paper.html", "Paper"), ("record.html", "Record"), ("data.html", "Data & code"), ("credits.html", "Credits"), ("publications.html", "Publications"), ("math.html", "The math"), ("code.html", "The code"), ("originals.html", "Who did the math first"), ("about.html", "About"), ] COPY = "© 2026 Matthew Gelfman and Melissa Gelfman" FOOTER = ('\n') def nav_html(current, available): parts = [] for href, label in NAV_ITEMS: if href not in available: continue cur = ' aria-current="page"' if href == current else "" parts.append(f'{label}') return '" def page(fname, title, desc, body, available): d = html.escape(desc, quote=True) t = html.escape(title) return f''' {t} | TRPG Physics
TRPG Physics {nav_html(fname, available)}
{body}
{FOOTER} ''' def rewrite_navs(build): """Replace the on every page with the current nav (only links whose page exists).""" build = pathlib.Path(build) available = {p.name for p in build.glob("*.html")} for p in sorted(build.glob("*.html")): t = p.read_text(encoding="utf-8") new = re.sub(r'', lambda m: nav_html(p.name, available), t, count=1, flags=re.S) if new != t: p.write_text(new, encoding="utf-8") return available def update_sitemap(build, pages): sm = pathlib.Path(build) / "sitemap.xml" t = sm.read_text(encoding="utf-8") for pg in pages: loc = f"https://trpgphysics.org/{pg}" if loc not in t: t = t.replace("", f" {loc}2026-11-14\n") sm.write_text(t, encoding="utf-8")