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:
@@ -11,13 +11,15 @@ from urllib.parse import unquote, urldefrag, urlparse
|
|||||||
|
|
||||||
# minified pages drop the quotes around simple attribute values
|
# minified pages drop the quotes around simple attribute values
|
||||||
ATTR = re.compile(r'\b(?:href|src)=(?:"([^"]*)"|\'([^\']*)\'|([^\s>"\']+))', re.I)
|
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():
|
def main():
|
||||||
root = Path(sys.argv[1] if len(sys.argv) > 1 else "public").resolve()
|
root = Path(sys.argv[1] if len(sys.argv) > 1 else "public").resolve()
|
||||||
broken, checked = {}, 0
|
broken, checked = {}, 0
|
||||||
for page in sorted(root.rglob("*.html")):
|
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)}:
|
for url in {a or b or c for a, b, c in ATTR.findall(text)}:
|
||||||
url = urldefrag(html.unescape(url))[0]
|
url = urldefrag(html.unescape(url))[0]
|
||||||
parsed = urlparse(url)
|
parsed = urlparse(url)
|
||||||
|
|||||||
Reference in New Issue
Block a user