#!/usr/bin/env python3 import os, re root = "." mapping = {} # Build a mapping of "what the HTML asks for" -> "what's actually on disk" for dirpath, _, files in os.walk(root): for fn in files: if "@" in fn: relpath = os.path.relpath(os.path.join(dirpath, fn), root) m = re.match(r"^(.+)@(.+)$", fn) if not m: continue base, tail = m.groups() ext = os.path.splitext(base)[1] # e.g. .css query = tail[:-len(ext)] if ext and tail.endswith(ext) else tail orig_fn = f"{base}?{query}" orig_path = os.path.join(os.path.dirname(relpath), orig_fn).replace(os.sep, "/") new_path = relpath.replace(os.sep, "/") mapping["/" + orig_path] = "/" + new_path print(f"Found {len(mapping)} mismatched asset(s)") # Rewrite every reference to the correct on-disk filename for dirpath, _, files in os.walk(root): for fn in files: if fn.endswith((".html", ".css")): full = os.path.join(dirpath, fn) with open(full, "r", errors="ignore") as f: content = f.read() changed = False for orig, actual in mapping.items(): if orig in content: content = content.replace(orig, actual) changed = True if changed: with open(full, "w") as f: f.write(content) print("Fixed:", full)