Lukasz Boryn 38eae57e6f refactor(lints): walk only what git does not ignore
link-check and render-check each held a byte-identical pages() that
walked the filesystem, so gitignored HANDOFF.md could redden a repo-wide
gate — 024 had to carry "clear HANDOFF.md first" as a run condition, and
a handoff had to be written link-free.

Both now import a shared pages() from _pages.py, whose census comes from
git ls-files --cached --others --exclude-standard: tracked plus
untracked-but-not-ignored is exactly not-ignored, so a page is walked
before it is ever added. Where git cannot answer — not a repo, no git on
PATH — the walk falls back to the old rglob rather than shrinking to
nothing, and the catch is narrow so an unnamed git failure still
surfaces.

023 and 019 each state the old walk in their Contract, so both take
superseded-by-026; their Contract and Verify are untouched, per 022.
023 also cited the docstring the walk moved out of, so that cite follows
it to _pages.py — a cite is a working pointer at every status, per 022 —
and _pages.py words that docstring on one line, because a cite symbol
that wraps never resolves.

026 passed its muster gate (matches: yes) and flips to implemented. Its
Justification loses three lines to fit the tally: block; its Contract and
Verify are byte-identical.

blueprint 026
2026-07-31 16:53:11 +02:00

69 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Validate relative markdown links. Usage: link-check <repo-root>
Errors (exit 1): an inline link whose target does not resolve against the
containing file's directory. Nothing warns: an unresolvable link is always
a defect, never a judgement call. The tally keeps the siblings' shape.
The links are load-bearing and silent when broken. A skill installs as a
symlink into `~/.claude/skills/<name>/` and `..` traverses it physically,
so a link written for the repo layout resolves from the installed base
directory too — but nothing follows a link automatically. A dangling one
surfaces as a failed read, not an error, so the signal has to be static.
Fenced blocks are skipped: there a link is illustration, not navigation.
`OKF/blueprints/` is skipped for its siblings' reason — a locked
blueprint is append-only, so a rename cannot repair a link inside one.
A page git ignores is skipped too: the walk is `_pages.py`'s.
"""
import re
import sys
from pathlib import Path
from _pages import pages
LINK = re.compile(r"\]\(([^)]*)\)")
EXTERNAL = ("http://", "https://", "mailto:", "#")
def targets(text):
"""Link targets outside fenced blocks, as (line number, target).
The line number is 1-based, so an error reads as a cite. A line
opening or closing a fence toggles the skip and is not itself read.
"""
fenced = False
for number, line in enumerate(text.splitlines(), 1):
if line.startswith("```"):
fenced = not fenced
elif not fenced:
for target in LINK.findall(line):
if not target.startswith(EXTERNAL):
yield number, target
def main(argv):
if len(argv) != 2:
print("usage: link-check <repo-root>", file=sys.stderr)
return 2
root = Path(argv[1]).resolve()
if not root.is_dir():
print(f"not a directory: {root}", file=sys.stderr)
return 2
errors = []
for page, rel in pages(root):
for number, target in targets(page.read_text()):
path = target.split("#", 1)[0] # a fragment names no file
if not (page.parent / path).exists():
errors.append(f"{rel}:{number}: unresolvable link: {target}")
for line in errors:
print(f"ERROR {line}")
print(f"{len(errors)} error(s), 0 warning(s)")
return 1 if errors else 0
if __name__ == "__main__":
sys.exit(main(sys.argv))