#!/usr/bin/env python3 """Repair the two artifacts the destruction passes introduced. 1. The link stripper could eat the space after a `- ` marker (`-`X``) and leave a dangling `see .` where a wikilink used to be. 2. The Eras collapse could swallow a table's HEADER row (it looked like just another non-current row), leaving a separator with no header above it. Usage: repair-damage.py [--apply] """ import pathlib import re import sys VAULT = pathlib.Path("serverAppConfigs-docs") ERAS_HEADER = "| Window | Payload | Runtime | Shape | Replaced because | Rollback |" def main(argv): apply = "--apply" in argv marker = dangling = header = 0 for p in sorted(VAULT.rglob("*.md")): s = str(p) if ".AGENTTRASH" in s or ".obsidian" in s: continue lines = p.read_text(errors="replace").split("\n") out = [] # Only a pointer whose target was deleted reads as `… see .` — never touch # a line merely because it wraps on a comma. (An earlier version of this # rule stripped the trailing comma off 660 wrapped prose lines, which # `scripts/restore-punctuation.py` then had to undo from HEAD.) for line in lines: new = line if re.match(r"^(\s*)[-*](?![-* ])[$`[(*_]", new): new = re.sub(r"^(\s*)[-*]", r"\1- ", new) marker += 1 if re.search(r"\s(?:see|also|details in|via|per)\s*\.?\s*$", new): new = re.sub(r"\s(?:see|also|details in|via|per)\s*\.?\s*$", "", new) dangling += 1 out.append(new) # A separator row whose line above is not a header gets its header back. fixed = [] for i, line in enumerate(out): if re.match(r"^\|-+", line.strip()) and (not fixed or not fixed[-1].strip().startswith("|")): fixed.append(ERAS_HEADER) header += 1 fixed.append(line) if apply and "".join(fixed) != "".join(lines): p.write_text("\n".join(fixed)) print(f"{'APPLIED' if apply else 'dry run'}: marker spacing {marker}, " f"dangling pointers {dangling}, restored table headers {header}") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))