Skip to content

Exclude underscores in download directive URLs #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion sphinxlint/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,28 @@ def check_missing_underscore_after_hyperlink(file, lines, options=None):

Bad: `Link text <https://example.com>`
Good: `Link text <https://example.com>`_

Note:
URLs within download directives don't need trailing underscores.
https://www.sphinx-doc.org/en/master/usage/referencing.html#role-download
"""
for lno, line in enumerate(lines, start=1):
if "`" not in line:
continue
for match in rst.SEEMS_HYPERLINK_RE.finditer(line):
if not match.group(2):
yield lno, "missing underscore after closing backtick in hyperlink"
# Check if this is within any download directive on the line
# Include optional underscore in pattern to handle both cases
is_in_download = False
for download_match in rst.HYPERLINK_WITHIN_DOWNLOAD_RE.finditer(line):
if (
match.start() >= download_match.start()
and match.end() <= download_match.end()
):
is_in_download = True
break
if not is_in_download:
yield lno, "missing underscore after closing backtick in hyperlink"


@checker(".rst", ".po")
Expand Down
3 changes: 2 additions & 1 deletion sphinxlint/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,10 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
# The :issue`123` is ...
ROLE_MISSING_RIGHT_COLON_RE = re.compile(rf"(^|\s):{SIMPLENAME}`(?!`)")


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

HYPERLINK_WITHIN_DOWNLOAD_RE = re.compile(r":download:`[^`]*`_?")

LEAKED_MARKUP_RE = re.compile(r"[a-z]::\s|`|\.\.\s*\w+:")

TRIPLE_BACKTICKS_RE = re.compile(
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/xpass/download-directive-with-url.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Download directives should not require underscores after URLs.

A basic download directive with https:
:download:`Download file <https://example.com/file.pdf>`

One with http:
:download:`Get the archive <http://downloads.example.com/archive.zip>`

An inline download:
This line contains a :download:`link to a file <https://example.com/file.txt>`.

Multiple download directives in a row:
First :download:`Download this file <https://example.com/first-file.txt>` and
then :download:`this one <https://example.com/second-file.txt>` something else

These should not trigger missing-underscore-after-hyperlink errors.