Skip to content

Commit e3987be

Browse files
authored
Exclude underscores in download directive URLs (#135)
1 parent a2753eb commit e3987be

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

sphinxlint/checkers.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,28 @@ def check_missing_underscore_after_hyperlink(file, lines, options=None):
256256
257257
Bad: `Link text <https://example.com>`
258258
Good: `Link text <https://example.com>`_
259+
260+
Note:
261+
URLs within download directives don't need trailing underscores.
262+
https://www.sphinx-doc.org/en/master/usage/referencing.html#role-download
259263
"""
260264
for lno, line in enumerate(lines, start=1):
261265
if "`" not in line:
262266
continue
263267
for match in rst.SEEMS_HYPERLINK_RE.finditer(line):
264268
if not match.group(2):
265-
yield lno, "missing underscore after closing backtick in hyperlink"
269+
# Check if this is within any download directive on the line
270+
# Include optional underscore in pattern to handle both cases
271+
is_in_download = False
272+
for download_match in rst.HYPERLINK_WITHIN_DOWNLOAD_RE.finditer(line):
273+
if (
274+
match.start() >= download_match.start()
275+
and match.end() <= download_match.end()
276+
):
277+
is_in_download = True
278+
break
279+
if not is_in_download:
280+
yield lno, "missing underscore after closing backtick in hyperlink"
266281

267282

268283
@checker(".rst", ".po")

sphinxlint/rst.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,10 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
270270
# The :issue`123` is ...
271271
ROLE_MISSING_RIGHT_COLON_RE = re.compile(rf"(^|\s):{SIMPLENAME}`(?!`)")
272272

273-
274273
SEEMS_HYPERLINK_RE = re.compile(r"`[^`]+?(\s?)<https?://[^`]+>`(_?)")
275274

275+
HYPERLINK_WITHIN_DOWNLOAD_RE = re.compile(r":download:`[^`]*`_?")
276+
276277
LEAKED_MARKUP_RE = re.compile(r"[a-z]::\s|`|\.\.\s*\w+:")
277278

278279
TRIPLE_BACKTICKS_RE = re.compile(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Download directives should not require underscores after URLs.
2+
3+
A basic download directive with https:
4+
:download:`Download file <https://example.com/file.pdf>`
5+
6+
One with http:
7+
:download:`Get the archive <http://downloads.example.com/archive.zip>`
8+
9+
An inline download:
10+
This line contains a :download:`link to a file <https://example.com/file.txt>`.
11+
12+
Multiple download directives in a row:
13+
First :download:`Download this file <https://example.com/first-file.txt>` and
14+
then :download:`this one <https://example.com/second-file.txt>` something else
15+
16+
These should not trigger missing-underscore-after-hyperlink errors.

0 commit comments

Comments
 (0)