diff --git a/scripts/verify-website.py b/scripts/verify-website.py index c32d92ccc6f..708d74550fc 100644 --- a/scripts/verify-website.py +++ b/scripts/verify-website.py @@ -56,6 +56,20 @@ def markdown_ids(path): return identifiers | parser.ids +def checked_references(references, label, errors, *, obsolete_hint=""): + """Yield parsed references while recording rejected URLs in encounter order.""" + for reference in references: + try: + url = split_reference(reference) + except ValueError as error: + errors.append(f"{label}: invalid URL {reference!r}: {error}") + continue + if url.hostname == LEGACY_ORIGIN and (url.path == "/rea" or url.path.startswith("/rea/")): + errors.append(f"{label}: obsolete website URL: {reference}{obsolete_hint}") + continue + yield reference, url + + def check_repository_links(root): """Check local links in website Markdown and current-repository source links.""" root = root.resolve() @@ -68,15 +82,8 @@ def check_repository_links(root): parser.feed(path.read_text(encoding="utf-8")) sources[path] = parser.references for path, references in sources.items(): - for reference in references: - try: - url = split_reference(reference) - except ValueError as error: - errors.append(f"{path.relative_to(root)}: invalid URL {reference!r}: {error}") - continue - if url.hostname == LEGACY_ORIGIN and (url.path == "/rea" or url.path.startswith("/rea/")): - errors.append(f"{path.relative_to(root)}: obsolete website URL: {reference}") - continue + label = path.relative_to(root) + for reference, url in checked_references(references, label, errors): if url.hostname == "github.com": match = re.match(r"^/morluto/rea/(?:blob|tree)/main/(.+)$", url.path) if not match: @@ -87,9 +94,9 @@ def check_repository_links(root): else: target = (path.parent / unquote(url.path)).resolve() if url.path else path if not target.is_relative_to(root) or not target.exists(): - errors.append(f"{path.relative_to(root)}: missing repository target: {reference}") + errors.append(f"{label}: missing repository target: {reference}") elif target.suffix == ".md" and url.fragment and unquote(url.fragment) not in markdown_ids(target): - errors.append(f"{path.relative_to(root)}: missing Markdown fragment: {reference}") + errors.append(f"{label}: missing Markdown fragment: {reference}") return errors @@ -357,15 +364,7 @@ def check_site(site): if identifier not in page.ids: errors.append(f"{label}: missing copy target: {identifier}") text_urls = [match.group().rstrip(".,;)") for match in SITE_TEXT_URL.finditer(" ".join(page.text))] - for reference in page.references + text_urls: - try: - url = split_reference(reference) - except ValueError as error: - errors.append(f"{label}: invalid URL {reference!r}: {error}") - continue - if url.hostname == LEGACY_ORIGIN and (url.path == "/rea" or url.path.startswith("/rea/")): - errors.append(f"{label}: obsolete website URL: {reference}; use rea.tools") - continue + for reference, url in checked_references(page.references + text_urls, label, errors, obsolete_hint="; use rea.tools"): if url.hostname == "rea.tools": if url.scheme != "https" or url.netloc != "rea.tools": errors.append(f"{label}: website URL must use {SITE_ORIGIN}: {reference}")