Skip to content

Commit c0f1e41

Browse files
committed
📝🎨 Mark versions as known word spellings
Otherwise, `sphinxcontrib.spelling` would complain about `dev` and two/three-letter local version identifier segments as misspelled[[1]]. [1]: sphinx-contrib/spelling#224
1 parent ffba30a commit c0f1e41

2 files changed

Lines changed: 50 additions & 10 deletions

File tree

docs/conf.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,10 @@
3737
'sphinx_tabs.tabs',
3838
'sphinxcontrib.apidoc',
3939
'sphinxcontrib.towncrier.ext', # provides `.. towncrier-draft-entries::`
40-
]
4140

42-
# Conditional third-party extensions:
43-
try:
44-
import sphinxcontrib.spelling as _sphinxcontrib_spelling
45-
except ImportError:
46-
extensions.append('spelling_stub_ext')
47-
else:
48-
del _sphinxcontrib_spelling # noqa: WPS100
49-
extensions.append('sphinxcontrib.spelling')
41+
# In-tree extensions:
42+
'spelling_stub_ext', # auto-loads `sphinxcontrib.spelling` if installed
43+
]
5044

5145
# Add any paths that contain templates here, relative to this directory.
5246
templates_path = ['_templates']

docs/spelling_stub_ext.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,54 @@
33
from typing import List
44

55
from sphinx.application import Sphinx
6+
from sphinx.config import Config as _SphinxConfig
7+
from sphinx.util import logging
68
from sphinx.util.docutils import SphinxDirective
79
from sphinx.util.nodes import nodes
810

911
from cheroot import __version__
1012

13+
try:
14+
from enchant.tokenize import ( # noqa: WPS433
15+
Filter as _EnchantTokenizeFilterBase,
16+
)
17+
except ImportError:
18+
_EnchantTokenizeFilterBase = object # noqa: WPS440
19+
20+
21+
logger = logging.getLogger(__name__)
22+
23+
24+
def _configure_spelling_ext(app: Sphinx, config: _SphinxConfig) -> None:
25+
class VersionFilter(_EnchantTokenizeFilterBase): # noqa: WPS431
26+
# NOTE: It's nested because we need to reference the config by closure.
27+
"""Filter for treating version words as known."""
28+
29+
def _skip(self, word: str) -> bool:
30+
# NOTE: Only accessing the config values in the method since they
31+
# NOTE: aren't yet populated when the config-inited event happens.
32+
known_version_words = {
33+
config.release,
34+
config.version,
35+
__version__,
36+
}
37+
if word not in known_version_words:
38+
return False
39+
40+
logger.debug(
41+
'Known version words: %r', # noqa: WPS323
42+
known_version_words,
43+
)
44+
logger.debug(
45+
'Ignoring %r because it is a known version', # noqa: WPS323
46+
word,
47+
)
48+
49+
return True
50+
51+
app.config.spelling_filters = [VersionFilter]
52+
app.setup_extension('sphinxcontrib.spelling')
53+
1154

1255
class SpellingNoOpDirective(SphinxDirective):
1356
"""Definition of the stub spelling directive."""
@@ -21,7 +64,10 @@ def run(self) -> List[nodes.Node]:
2164

2265
def setup(app: Sphinx) -> None:
2366
"""Initialize the extension."""
24-
app.add_directive('spelling', SpellingNoOpDirective)
67+
if _EnchantTokenizeFilterBase is object:
68+
app.add_directive('spelling', SpellingNoOpDirective)
69+
else:
70+
app.connect('config-inited', _configure_spelling_ext)
2571

2672
return {
2773
'parallel_read_safe': True,

0 commit comments

Comments
 (0)