check_links: ignore code samples in <pre> blocks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YDRjtmAiNpo5mK5N3d7QwZ
This commit is contained in:
2026-10-14 18:45:09 +02:00
co-authored by Claude Opus 5.5
parent 0663e96a46
commit bfb34266c8
+3 -1
View File
@@ -11,13 +11,15 @@ from urllib.parse import unquote, urldefrag, urlparse
# minified pages drop the quotes around simple attribute values
ATTR = re.compile(r'\b(?:href|src)=(?:"([^"]*)"|\'([^\']*)\'|([^\s>"\']+))', re.I)
# code samples show markup, they do not link (escaped HTML in a <pre> would look like attributes)
PRE = re.compile(r'<pre\b.*?</pre>', re.I | re.S)
def main():
root = Path(sys.argv[1] if len(sys.argv) > 1 else "public").resolve()
broken, checked = {}, 0
for page in sorted(root.rglob("*.html")):
text = page.read_text(encoding="utf-8", errors="replace")
text = PRE.sub("", page.read_text(encoding="utf-8", errors="replace"))
for url in {a or b or c for a, b, c in ATTR.findall(text)}:
url = urldefrag(html.unescape(url))[0]
parsed = urlparse(url)