Skip to content

Commit d5473d7

Browse files
committed
Get extra known directives from config file
1 parent 145d4d9 commit d5473d7

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ requires-python = ">= 3.7"
2323
dependencies = [
2424
"regex",
2525
"polib",
26+
"tomli>=2; python_version < '3.11'",
2627
]
2728
dynamic = ["version"]
2829

sphinxlint/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from itertools import chain, starmap
88

99
from sphinxlint import check_file
10+
from sphinxlint import rst
11+
from sphinxlint.config import get_config
1012
from sphinxlint.checkers import all_checkers
1113
from sphinxlint.sphinxlint import CheckersOptions
1214

@@ -113,6 +115,11 @@ def walk(path, ignore_list):
113115

114116

115117
def main(argv=None):
118+
config = get_config()
119+
120+
# Append extra directives
121+
rst.DIRECTIVES_CONTAINING_ARBITRARY_CONTENT.extend(config.get("known_directives", []))
122+
116123
enabled_checkers, args = parse_args(argv)
117124
options = CheckersOptions.from_argparse(args)
118125
if args.list:

sphinxlint/checkers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ def check_directive_with_three_dots(file, lines, options=None):
136136
Bad: ... versionchanged:: 3.6
137137
Good: .. versionchanged:: 3.6
138138
"""
139+
three_dot_directive_re = rst.three_dot_directive_re()
139140
for lno, line in enumerate(lines, start=1):
140-
if rst.THREE_DOT_DIRECTIVE_RE.search(line):
141+
if three_dot_directive_re.search(line):
141142
yield lno, "directive should start with two dots, not three."
142143

143144

@@ -148,8 +149,9 @@ def check_directive_missing_colons(file, lines, options=None):
148149
Bad: .. versionchanged 3.6.
149150
Good: .. versionchanged:: 3.6
150151
"""
152+
seems_directive_re = rst.seems_directive_re()
151153
for lno, line in enumerate(lines, start=1):
152-
if rst.SEEMS_DIRECTIVE_RE.search(line):
154+
if seems_directive_re.search(line):
153155
yield lno, "comment seems to be intended as a directive"
154156

155157

sphinxlint/config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
from os.path import isfile
3+
from typing import Any
4+
5+
if sys.version_info[:2] >= (3, 11):
6+
import tomllib
7+
else:
8+
try:
9+
import tomli as tomllib
10+
except ImportError:
11+
tomllib = None
12+
13+
14+
def _read_toml(filename: str) -> dict[str, Any]:
15+
if tomllib is None:
16+
return {}
17+
with open(filename, "rb") as f:
18+
return tomllib.load(f)
19+
20+
21+
def get_config() -> dict[str, Any]:
22+
if isfile("sphinx.toml"):
23+
table = _read_toml("sphinx.toml")
24+
elif isfile("pyproject.toml"):
25+
table = _read_toml("pyproject.toml")
26+
else:
27+
table = {}
28+
return table.get("tool", {}).get("sphinx-lint", {})

sphinxlint/rst.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,6 @@
132132
r"^\s*\.\. (" + "|".join(DIRECTIVES_CONTAINING_RST) + ")::"
133133
)
134134

135-
ALL_DIRECTIVES = (
136-
"("
137-
+ "|".join(DIRECTIVES_CONTAINING_RST + DIRECTIVES_CONTAINING_ARBITRARY_CONTENT)
138-
+ ")"
139-
)
140-
141135
QUOTE_PAIRS = [
142136
"»»", # Swedish
143137
"‘‚", # Albanian/Greek/Turkish
@@ -184,6 +178,14 @@
184178
UNICODE_ALLOWED_AFTER_INLINE_MARKUP = r"[\p{Pe}\p{Pi}\p{Pf}\p{Pd}\p{Po}]"
185179

186180

181+
def get_all_directives() -> str:
182+
return (
183+
"("
184+
+ "|".join(DIRECTIVES_CONTAINING_RST + DIRECTIVES_CONTAINING_ARBITRARY_CONTENT)
185+
+ ")"
186+
)
187+
188+
187189
def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
188190
"""Generate a regex matching an inline markup.
189191
@@ -259,19 +261,24 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
259261
rf"(^|\s)`:{SIMPLENAME}:{INTERPRETED_TEXT_RE.pattern}", flags=re.VERBOSE | re.DOTALL
260262
)
261263

264+
262265
# Find comments that look like a directive, like:
263266
# .. versionchanged 3.6
264267
# or
265268
# .. versionchanged: 3.6
266269
# as it should be:
267270
# .. versionchanged:: 3.6
268-
SEEMS_DIRECTIVE_RE = re.compile(rf"^\s*(?<!\.)\.\. {ALL_DIRECTIVES}([^a-z:]|:(?!:))")
271+
def seems_directive_re() -> re.Pattern[str]:
272+
return re.compile(rf"^\s*(?<!\.)\.\. {get_all_directives()}([^a-z:]|:(?!:))")
273+
269274

270275
# Find directive prefixed with three dots instead of two, like:
271276
# ... versionchanged:: 3.6
272277
# instead of:
273278
# .. versionchanged:: 3.6
274-
THREE_DOT_DIRECTIVE_RE = re.compile(rf"\.\.\. {ALL_DIRECTIVES}::")
279+
def three_dot_directive_re() -> re.Pattern[str]:
280+
return re.compile(rf"\.\.\. {get_all_directives()}::")
281+
275282

276283
# Find role used with double backticks instead of simple backticks like:
277284
# :const:``None``

sphinxlint/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def hide_non_rst_blocks(lines, hidden_block_cb=None):
177177
def type_of_explicit_markup(line):
178178
"""Tell apart various explicit markup blocks."""
179179
line = line.lstrip()
180-
if re.match(rf"\.\. {rst.ALL_DIRECTIVES}::", line):
180+
if re.match(rf"\.\. {rst.get_all_directives()}::", line):
181181
return "directive"
182182
if re.match(r"\.\. \[[0-9]+\] ", line):
183183
return "footnote"

0 commit comments

Comments
 (0)