Skip to content

feat: add checker for role with extra backtick #139

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 1 commit 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
13 changes: 13 additions & 0 deletions sphinxlint/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ def check_role_with_double_backticks(file, lines, options=None):
)


@checker(".rst", ".po")
def check_role_with_extra_backtick(filename, lines, options):
"""Check for extra backtick in roles.

Bad: :func:`foo``
Bad: :func:``foo`
Good: :func:`foo`
"""
for lno, line in enumerate(lines, start=1):
for match in rst.ROLE_WITH_EXTRA_BACKTICK_RE.finditer(line):
yield lno, f"Extra backtick in role: {match.group(0).strip()!r}"


@checker(".rst", ".po")
def check_missing_space_before_role(file, lines, options=None):
"""Search for missing spaces before roles.
Expand Down
7 changes: 7 additions & 0 deletions sphinxlint/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
# :const:`None`
DOUBLE_BACKTICK_ROLE_RE = re.compile(rf"(?<!``){ROLE_HEAD}``")

# Find roles with extra backtick like:
# :mod:`!cgi`` (extra backtick at the end)
# :mod:``!cgi` (extra backtick at the beginning)
ROLE_WITH_EXTRA_BACKTICK_RE = re.compile(
rf"({ROLE_HEAD}(?:``[^`\s]+`(?!\S)|`[^`\s]+``))(?!`)"
)

START_STRING_PREFIX = f"(^|(?<=\\s|[{OPENERS}{DELIMITERS}|]))"
END_STRING_SUFFIX = f"($|(?=\\s|[\x00{CLOSING_DELIMITERS}{DELIMITERS}{CLOSERS}|]))"

Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/xfail/role-with-extra-backtick.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.. expect: Extra backtick in role: ':mod:`!cgi``' (role-with-extra-backtick)
.. expect: Extra backtick in role: ':mod:``!cgitb`' (role-with-extra-backtick)
.. expect: Extra backtick in role: ':func:`foo``' (role-with-extra-backtick)
.. expect: Extra backtick in role: ':class:`MyClass``' (role-with-extra-backtick)
.. expect: Extra backtick in role: ':meth:``method`' (role-with-extra-backtick)

This file contains roles with extra backtick that should be detected.

* :pep:`594`: Remove the :mod:`!cgi`` and :mod:``!cgitb` modules

* :func:`foo`` should be :func:`foo`

* :class:`MyClass`` is wrong

* :meth:``method` should be fixed
Loading