File size: 3,826 Bytes
b855ced | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/usr/bin/env python3
"""Documentation checks (docs as code). Standard library only.
1. Every relative link and image in the project's Markdown points at a file
that exists, and every #anchor at a heading that exists.
2. Every TAPER_* setting the server, desktop app or scripts read is
documented somewhere in the docs (deploy/taper.env.example counts).
3. Every document in docs/ is listed in docs/README.md.
scripts/ci/check-docs.py
"""
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(subprocess.check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip())
# The project's own docs. content/ is the specification, checked by Hugo.
DOC_GLOBS = ["*.md", "docs/**/*.md", "deploy/*.md", "server/*.md", "client/*.md", "desktop/*.md"]
SKIP = {"ANTI_SCRAPING_STRATEGY.md", "RESEARCH_SUMMARY.md", "DEPENDENCIES.md"}
LINK = re.compile(r"!?\[[^\]]*\]\(([^)\s]+)(?:\s+\"[^\"]*\")?\)|<img[^>]+src=\"([^\"]+)\"")
FENCE = re.compile(r"^(```|~~~).*?^\1", re.M | re.S)
def tracked(pattern):
out = subprocess.check_output(["git", "ls-files", "--cached", "--others", "--exclude-standard", "--", f":(glob){pattern}"], cwd=ROOT, text=True)
return [ROOT / p for p in out.split()]
def slug(heading):
s = re.sub(r"[`*_]|\[([^\]]*)\]\([^)]*\)", r"\1", heading.strip().lower())
s = re.sub(r"[^\w\- ]", "", s)
return s.replace(" ", "-")
def anchors(path, cache={}):
if path not in cache:
text = FENCE.sub("", path.read_text(encoding="utf-8"))
cache[path] = {slug(m.group(1)) for m in re.finditer(r"^#{1,6}\s+(.+?)\s*#*$", text, re.M)}
return cache[path]
def check_links(docs):
errors = []
for doc in docs:
text = FENCE.sub("", doc.read_text(encoding="utf-8"))
text = re.sub(r"`[^`\n]*`", "", text)
for m in LINK.finditer(text):
target = m.group(1) or m.group(2)
if re.match(r"^[a-z]+:", target) or target.startswith("//"):
continue # external: not checked offline
path_part, _, anchor = target.partition("#")
dest = doc if not path_part else (doc.parent / path_part).resolve()
rel = doc.relative_to(ROOT)
if not dest.exists():
errors.append(f"{rel}: broken link {target}")
elif anchor and dest.suffix == ".md" and anchor not in anchors(dest):
errors.append(f"{rel}: no heading for #{anchor} in {dest.relative_to(ROOT)}")
return errors
def check_settings(docs):
used = set()
for pattern in ["server/*.js", "desktop/taper_desktop/*.py", "scripts/*.sh", "deploy/*"]:
for f in tracked(pattern):
if f.name.startswith("test") or not f.is_file():
continue
used |= set(re.findall(r"\b(TAPER_[A-Z0-9_]+)\b", f.read_text(encoding="utf-8", errors="replace")))
documented = set()
for f in docs + tracked("deploy/taper.env.example"):
documented |= set(re.findall(r"\b(TAPER_[A-Z0-9_]+)\b", f.read_text(encoding="utf-8")))
return [f"setting {s} is read by the code but not documented" for s in sorted(used - documented)]
def check_index():
index = (ROOT / "docs/README.md").read_text(encoding="utf-8")
return [f"docs/README.md doesn't list {p.relative_to(ROOT)}"
for p in tracked("docs/*.md") + tracked("docs/design/*.md")
if p.name != "README.md" and p.relative_to(ROOT / "docs").as_posix() not in index]
def main():
docs = sorted({p for g in DOC_GLOBS for p in tracked(g) if p.name not in SKIP and p.exists()})
errors = check_links(docs) + check_settings(docs) + check_index()
for e in errors:
print(e)
print(f"{len(docs)} documents checked, {len(errors)} problems")
return 1 if errors else 0
if __name__ == "__main__":
sys.exit(main())
|