33from typing import List
44
55from sphinx .application import Sphinx
6+ from sphinx .config import Config as _SphinxConfig
7+ from sphinx .util import logging
68from sphinx .util .docutils import SphinxDirective
79from sphinx .util .nodes import nodes
810
911from 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
1255class SpellingNoOpDirective (SphinxDirective ):
1356 """Definition of the stub spelling directive."""
@@ -21,7 +64,10 @@ def run(self) -> List[nodes.Node]:
2164
2265def 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