1
0
mirror of https://github.com/django/django.git synced 2025-09-10 11:09:12 +00:00

Added sphinxlint checker to flag relative targets in :doc: roles.

Co-authored-by: Adam Johnson <me@adamj.eu>
This commit is contained in:
David Smith 2025-08-28 10:22:21 -03:00 committed by nessita
parent 56955636e6
commit c93dddf659

View File

@ -14,7 +14,7 @@ from sphinxlint.checkers import (
from sphinxlint.checkers import checker as sphinxlint_checker from sphinxlint.checkers import checker as sphinxlint_checker
from sphinxlint.rst import SIMPLENAME from sphinxlint.rst import SIMPLENAME
from sphinxlint.sphinxlint import check_text from sphinxlint.sphinxlint import check_text
from sphinxlint.utils import PER_FILE_CACHES, hide_non_rst_blocks from sphinxlint.utils import PER_FILE_CACHES, hide_non_rst_blocks, paragraphs
def django_check_file(filename, checkers, options=None): def django_check_file(filename, checkers, options=None):
@ -136,6 +136,22 @@ def check_python_domain_in_roles(file, lines, options=None):
yield lno, f":py domain is the default and can be omitted {role.group(0)!r}" yield lno, f":py domain is the default and can be omitted {role.group(0)!r}"
_DOC_CAPTURE_TARGET_RE = re.compile(r":doc:`(?:[^<`]+<)?([^>`]+)>?`")
@sphinxlint_checker(".rst", rst_only=True)
def check_absolute_targets_doc_role(file, lines, options=None):
for paragraph_lno, paragraph in paragraphs(lines):
for error in _DOC_CAPTURE_TARGET_RE.finditer(paragraph):
target = error.group(1)
# Skip absolute or intersphinx refs like "python:using/windows".
if target.startswith("/") or ":" in target.split("/", 1)[0]:
continue
# Relative target, report as a violation.
error_offset = paragraph[: error.start()].count("\n")
yield (paragraph_lno + error_offset, target)
import sphinxlint # noqa: E402 import sphinxlint # noqa: E402
sphinxlint.check_file = django_check_file sphinxlint.check_file = django_check_file