Skip to content

Commit e24bb0e

Browse files
authored
feat: add checker for role with extra backtick (#139)
feat: add checker for detecting role with extra backtick
1 parent ee39033 commit e24bb0e

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
@@ -322,6 +322,19 @@ def check_role_with_double_backticks(file, lines, options=None):
322322
)
323323

324324

325+
@checker(".rst", ".po")
326+
def check_role_with_extra_backtick(filename, lines, options):
327+
"""Check for extra backtick in roles.
328+
329+
Bad: :func:`foo``
330+
Bad: :func:``foo`
331+
Good: :func:`foo`
332+
"""
333+
for lno, line in enumerate(lines, start=1):
334+
for match in rst.ROLE_WITH_EXTRA_BACKTICK_RE.finditer(line):
335+
yield lno, f"Extra backtick in role: {match.group(0).strip()!r}"
336+
337+
325338
@checker(".rst", ".po")
326339
def check_missing_space_before_role(file, lines, options=None):
327340
"""Search for missing spaces before roles.

sphinxlint/rst.py

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

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

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)