diff --git a/.github/scripts/build_index.py b/.github/scripts/build_index.py new file mode 100644 index 0000000..83569e7 --- /dev/null +++ b/.github/scripts/build_index.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +"""Build the index page listing the generated PDFs. + +Each entry carries the time its PDF was last updated, so a reader can tell at +a glance whether what they are looking at reflects the latest source. That +time comes from git, not the file's mtime: the PDFs are restored from a cache +or checked out fresh, so their mtimes are the time of the run rather than the +time the document last changed. + +Usage: build_index.py --public public --template site/index.html \ + --output public/index.html [--tracked-dir pdfs] +""" + +import argparse +import datetime +import os +import subprocess +import sys + +PDF_LIST_MARKER = "" +GENERATED_MARKER = "" + + +def git_last_updated(repo_relative_path): + """Commit time of a path, or None when git knows nothing about it.""" + try: + out = subprocess.run( + ["git", "log", "-1", "--format=%cI", "--", repo_relative_path], + capture_output=True, text=True, check=False, + ) + except OSError: + return None + stamp = out.stdout.strip() + if not stamp: + return None + try: + return datetime.datetime.fromisoformat(stamp) + except ValueError: + return None + + +def as_utc_text(moment): + return moment.astimezone(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M UTC") + + +def as_iso(moment): + return moment.astimezone(datetime.timezone.utc).isoformat() + + +def time_tag(moment, css_class, prefix=""): + """A