Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions scripts/check_external_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,27 +336,52 @@ def report_needs_issue(report: dict) -> bool:


def fix_moved(report: dict, root: Path = ROOT) -> tuple[int, list[Path]]:
"""Rewrite each permanently moved link to the address its redirect ended on, in the files that cite it (#1255).
Only `[text](URL)` and `<URL>` forms are rewritten, a target that looks like a login or error page is left alone,
and ignored URLs never reach the report. Returns (links rewritten, files changed); run `make build-explainers`
after changing an explainer."""
"""Rewrite moved links in Markdown and HTML formats."""
changed: dict[Path, str] = {}
count = 0

for item in report.get("moved", []):
old, new = item["url"], item["detail"]

if LOGIN_OR_ERROR.search(urllib.parse.urlsplit(new).path):
continue

escaped_old = re.escape(old)

for name in item["files"]:
path = Path(name) if Path(name).is_absolute() else root / name
text = changed.get(path) or path.read_text(encoding="utf-8")
fixed = text.replace(f"]({old})", f"]({new})").replace(f"<{old}>", f"<{new}>")
text = changed.get(path)
if text is None:
text = path.read_text(encoding="utf-8")

fixed = re.sub(
rf"(\]\(\s*){escaped_old}(?=\s*(?:\"[^\"]*\"|'[^']*'|\([^)]*\))?\s*\))",
lambda m: m.group(1) + new,
text,
)

fixed = re.sub(
rf"(?m)^(\s*\[[^\]]+\]:\s*<?){escaped_old}(?=>?(?:\s+.*)?$)",
lambda m: m.group(1) + new,
fixed,
)

fixed = re.sub(
rf"""(?i)(\bhref\s*=\s*)(["']){escaped_old}\2""",
lambda m: m.group(1) + m.group(2) + new + m.group(2),
fixed,
)

fixed = fixed.replace(f"<{old}>", f"<{new}>")

if fixed != text:
changed[path] = fixed
count += 1

for path, text in changed.items():
path.write_text(text, encoding="utf-8")
return count, sorted(changed)

return count, sorted(changed)

def _positive_int(value: str) -> int:
number = int(value)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_check_external_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,30 @@ def test_main_fix_moved_rewrites_after_reporting(tmp_path, monkeypatch, capsys):
out = capsys.readouterr().out
assert "1 moved" in out and "Rewrote 1 moved link(s) in 1 file(s)" in out
assert doc.read_text(encoding="utf-8") == "[a](https://new.example.org/a)\n"

def test_fix_moved_rewrites_additional_link_formats(tmp_path):
doc = tmp_path / "doc.md"
doc.write_text(
'[Google](https://old.example.org/a "Search")\n'
'[google]: https://old.example.org/a\n'
'<a href="https://old.example.org/a">Google</a>\n',
encoding="utf-8",
)

report = {
"moved": [{
"url": "https://old.example.org/a",
"detail": "https://new.example.org/a",
"files": ["doc.md"],
}]
}

count, files = script.fix_moved(report, root=tmp_path)

assert count == 1
assert files == [doc]
assert doc.read_text(encoding="utf-8") == (
'[Google](https://new.example.org/a "Search")\n'
'[google]: https://new.example.org/a\n'
'<a href="https://new.example.org/a">Google</a>\n'
)
Loading