Skip to content

Commit e11461f

Browse files
committed
feat: add checker for role with extra backtick
1 parent 688bef9 commit e11461f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

sphinxlint/checkers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ def check_role_with_double_backticks(file, lines, options=None):
301301
)
302302

303303

304+
@checker(".rst", ".po")
305+
def check_role_with_extra_backtick(filename, lines, options):
306+
"""Check for extra backtick in roles.
307+
308+
Bad: :func:`foo``
309+
Bad: :func:``foo`
310+
Good: :func:`foo`
311+
"""
312+
for lno, line in enumerate(lines, start=1):
313+
for match in rst.ROLE_WITH_EXTRA_BACKTICK_RE.finditer(line):
314+
yield lno, f"Extra backtick in role: {match.group(0).strip()!r}"
315+
316+
304317
@checker(".rst", ".po")
305318
def check_missing_space_before_role(file, lines, options=None):
306319
"""Search for missing spaces before roles.

sphinxlint/rst.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
249249
# :const:`None`
250250
DOUBLE_BACKTICK_ROLE_RE = re.compile(rf"(?<!``){ROLE_HEAD}``")
251251

252+
# Find roles with extra backtick like:
253+
# :mod:`!cgi`` (extra backtick at the end)
254+
# :mod:``!cgi` (extra backtick at the beginning)
255+
ROLE_WITH_EXTRA_BACKTICK_RE = re.compile(
256+
rf"({ROLE_HEAD}(?:``[^`\s]+`(?!\S)|`[^`\s]+``))(?!`)"
257+
)
258+
252259
START_STRING_PREFIX = f"(^|(?<=\\s|[{OPENERS}{DELIMITERS}|]))"
253260
END_STRING_SUFFIX = f"($|(?=\\s|[\x00{CLOSING_DELIMITERS}{DELIMITERS}{CLOSERS}|]))"
254261

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.. expect: Extra backtick in role: ':mod:`!cgi``' (role-with-extra-backtick)
2+
.. expect: Extra backtick in role: ':mod:``!cgitb`' (role-with-extra-backtick)
3+
.. expect: Extra backtick in role: ':func:`foo``' (role-with-extra-backtick)
4+
.. expect: Extra backtick in role: ':class:`MyClass``' (role-with-extra-backtick)
5+
.. expect: Extra backtick in role: ':meth:``method`' (role-with-extra-backtick)
6+
7+
This file contains roles with extra backtick that should be detected.
8+
9+
* :pep:`594`: Remove the :mod:`!cgi`` and :mod:``!cgitb` modules
10+
11+
* :func:`foo`` should be :func:`foo`
12+
13+
* :class:`MyClass`` is wrong
14+
15+
* :meth:``method` should be fixed

0 commit comments

Comments
 (0)