# case 4757: the revision stamp and copyright year in the footer are set when a manual is built, never by hand. # Used by both manuals (hooks: in mkdocs.yml). In the copyright line REV-AUTO becomes REV- and YEAR-AUTO # becomes that date's year. The date is the last commit that changed this manual (its folder, mkdocs.yml included), # or today if it has uncommitted changes or git is not available (e.g. a build outside the repo). # Both manuals build with strict: true, so a warning here fails the build: only a missing placeholder warns. import datetime import logging import os import subprocess log = logging.getLogger("mkdocs.hooks.rev_date") def _git(folder, *args): return subprocess.run(["git", *args, "--", "."], cwd=folder, capture_output=True, text=True, check=True).stdout.strip() def on_config(config): folder = os.path.dirname(os.path.abspath(config.config_file_path)) today = datetime.date.today().isoformat() try: rev = today if _git(folder, "status", "--porcelain") else _git(folder, "log", "-1", "--format=%cs") except (OSError, subprocess.CalledProcessError): log.info("git not available: revision date set to today") rev = today rev = rev or today copyright = config.copyright or "" for placeholder in ("REV-AUTO", "YEAR-AUTO"): if placeholder not in copyright: log.warning(f"{placeholder} not found in the copyright line of {config.config_file_path}") config.copyright = copyright.replace("REV-AUTO", "REV-" + rev).replace("YEAR-AUTO", rev[:4]) log.info(f"revision stamp: {config.copyright}") return config