Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

md_links: balanced parentheses are part of a link target (Fixes #1464) (#1756)

Eyal Golan8c108882e7b6
diff --git a/scripts/oss/md_links.py b/scripts/oss/md_links.py
index a5ec9e0fe..4ca495872 100644
--- a/scripts/oss/md_links.py
+++ b/scripts/oss/md_links.py
@@ -3,7 +3,8 @@
`uv run scripts/oss/md_links.py [--root DIR]`. Ignores code (inline spans, and fences of three or more backticks or tildes), absolute URLs (any `scheme:`),
`#anchor` and `mailto:` links, and targets with brackets (reference-style leftovers). Anchors after `#` are not checked, only the file.
-Handles a link title (`[a](b "t")`), `<angle>` targets, `%20` escapes and the outer link of a badge (`[![x](img)](page)`).
+Handles a link title (`[a](b "t")`), `<angle>` targets, `%20` escapes, balanced parentheses in a target
+(`[a](setup_(mac).md)`) and the outer link of a badge (`[![x](img)](page)`).
A target must match a tracked file or directory exactly, so a wrong letter case fails on macOS too (CI's Linux would).
One line per dead link (file: link); exit 1 if any."""
import argparse
@@ -15,7 +16,9 @@ from urllib.parse import unquote
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
-LINK = re.compile(r"\]\(<?([^)\s>]+)>?(?:\s+(?:\"[^\"]*\"|'[^']*'))?\)")
+# A target is a run of characters with no space, ")" or ">", and may hold balanced parentheses one level deep
+# (`docs/setup_(mac).md`, #1464); the first unbalanced ")" ends the link.
+LINK = re.compile(r"\]\(<?((?:[^()\s>]|\([^()\s>]*\))+)>?(?:\s+(?:\"[^\"]*\"|'[^']*'))?\)")
# A fence opens with 3+ backticks or tildes and closes with the same run; an inline span is a backtick run closed by an
# equal run and may not cross a blank line, so one stray backtick cannot swallow the rest of the file.
FENCE = re.compile(r"(`{3,}|~{3,}).*?\1", re.S)
diff --git a/scripts/oss/tests/test_md_links.py b/scripts/oss/tests/test_md_links.py
index 854bcc891..3117e27ca 100644
--- a/scripts/oss/tests/test_md_links.py
+++ b/scripts/oss/tests/test_md_links.py
@@ -61,3 +61,15 @@ def test_stray_backtick_and_shorter_fence_run_do_not_hide_real_links(make_repo):
def test_an_inline_span_does_not_cross_a_blank_line(make_repo):
root = make_repo({'README.md': 'a ` b\n\n[one](gone1.md)\n\nc ` d\n'})
assert m.dead_links(root) == [('README.md', 'gone1.md')]
+
+
+def test_balanced_parentheses_are_part_of_the_target(make_repo, capsys):
+ # #1464: the target used to stop at the first ")", so a file with parentheses in its name was reported as dead
+ root = make_repo({
+ 'README.md': '[guide](docs/setup_(mac).md) [gone](docs/missing_(x).md) [t](docs/setup_(mac).md "title")\n'
+ '[angle](<docs/setup_(mac).md>) [hash](docs/setup_(mac).md#x)\n',
+ 'docs/setup_(mac).md': 'x\n',
+ })
+ assert m.dead_links(root) == [('README.md', 'docs/missing_(x).md')]
+ assert m.main(['--root', str(root)]) == 1
+ assert capsys.readouterr().out == 'README.md: docs/missing_(x).md\n'