Skip to content

Commit 3aee36b

Browse files
committed
New detector: Missing backtick in front of an hyperlink.
1 parent 67faaf8 commit 3aee36b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

sphinxlint.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
"""Sphinx rst linter."""
1212

13-
__version__ = "0.6.5"
13+
__version__ = "0.6.6"
1414

1515
import argparse
1616
import multiprocessing
@@ -239,6 +239,8 @@ def clean_paragraph(paragraph):
239239
paragraph = escape2null(paragraph)
240240
paragraph = _clean_heuristic(paragraph, inline_literal_re)
241241
paragraph = _clean_heuristic(paragraph, inline_internal_target_re)
242+
paragraph = _clean_heuristic(paragraph, hyperlink_references_re)
243+
paragraph = _clean_heuristic(paragraph, anonymous_hyperlink_references_re)
242244
paragraph = normal_role_re.sub("", paragraph)
243245
return paragraph.replace("\x00", "\\")
244246

@@ -418,6 +420,8 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
418420
# https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
419421
interpreted_text_re = inline_markup_gen("`", "`")
420422
inline_internal_target_re = inline_markup_gen("_`", "`")
423+
hyperlink_references_re = inline_markup_gen("`", "`_")
424+
anonymous_hyperlink_references_re = inline_markup_gen("`", "`__")
421425
inline_literal_re = inline_markup_gen("``", "``")
422426
normal_role_re = re.compile(
423427
f":{SIMPLENAME}:{interpreted_text_re.pattern}", flags=re.VERBOSE | re.DOTALL
@@ -627,6 +631,27 @@ def check_missing_space_before_default_role(file, lines, options=None):
627631
)
628632

629633

634+
@checker(".rst")
635+
def check_hyperlink_reference_missing_backtick(file, lines, options=None):
636+
"""Search for missing backticks in front of hyperlink references.
637+
638+
Bad: Misc/NEWS <https://github.com/python/cpython/blob/v3.2.6/Misc/NEWS>`_
639+
Good: `Misc/NEWS <https://github.com/python/cpython/blob/v3.2.6/Misc/NEWS>`_
640+
"""
641+
for paragraph_lno, paragraph in paragraphs(lines):
642+
if paragraph.count("|") > 4:
643+
return # we don't handle tables yet.
644+
paragraph = clean_paragraph(paragraph)
645+
paragraph = interpreted_text_re.sub("", paragraph)
646+
for hyperlink_reference in re.finditer(r"\S* <https?://[^ ]+>`_", paragraph):
647+
error_offset = paragraph[: hyperlink_reference.start()].count("\n")
648+
context = hyperlink_reference.group(0)
649+
yield (
650+
paragraph_lno + error_offset,
651+
f"missing backtick before hyperlink reference: {context!r}.",
652+
)
653+
654+
630655
@checker(".rst")
631656
def check_missing_colon_in_role(file, lines, options=None):
632657
"""Search for missing colons in roles.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In the following line, the hyperlink reference misses its opening backtick
2+
Misc/NEWS <https://github.com/python/cpython/blob/v3.2.6/Misc/NEWS>`_
3+
that's bad.

0 commit comments

Comments
 (0)