46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Prueft relative Markdown-Links aller versionierten .md-Dateien.
|
|
|
|
Analog zum lokalen Linkvalidator (GNAI-013 Abschnitt 4: broken_local_links=0).
|
|
Absolute externe URLs (http/https/mailto) und reine Anker werden nicht geprüft.
|
|
"""
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
LINK_RE = re.compile(r"\[[^\]]*\]\(([^)#\s]+)(#[^)]*)?\)")
|
|
|
|
|
|
def md_files() -> list[str]:
|
|
out = subprocess.run(["git", "ls-files", "-z", "*.md"], capture_output=True, check=True)
|
|
return [f for f in out.stdout.decode("utf-8", "surrogateescape").split("\0") if f and not f.startswith(".venv/")]
|
|
|
|
|
|
def main() -> int:
|
|
broken = []
|
|
checked = 0
|
|
for path in md_files():
|
|
base = os.path.dirname(path)
|
|
with open(path, encoding="utf-8", errors="replace") as fh:
|
|
for lineno, line in enumerate(fh, 1):
|
|
for m in LINK_RE.finditer(line):
|
|
target = m.group(1)
|
|
if re.match(r"^[a-zA-Z][a-zA-Z0-9+.-]*:", target): # http:, mailto:, ...
|
|
continue
|
|
checked += 1
|
|
resolved = os.path.normpath(os.path.join(base, target))
|
|
if not os.path.exists(resolved):
|
|
broken.append(f"{path}:{lineno}: {target}")
|
|
if broken:
|
|
print(f"LINK-CHECK: {len(broken)} gebrochene Links (geprueft: {checked}):")
|
|
for b in broken:
|
|
print(" ", b)
|
|
return 1
|
|
print(f"LINK-CHECK: OK ({checked} relative Links in {len(md_files())} Dateien)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|