diff --git a/src/mkdocs_spellcheck/plugin.py b/src/mkdocs_spellcheck/plugin.py index ad7a488..7a69e03 100644 --- a/src/mkdocs_spellcheck/plugin.py +++ b/src/mkdocs_spellcheck/plugin.py @@ -13,19 +13,37 @@ from mkdocs.config.config_options import Type as MkType from mkdocs.plugins import BasePlugin from mkdocs.structure.pages import Page -from symspellpy import SymSpell -from mkdocs_spellcheck.backends import codespell, symspellpy +from mkdocs_spellcheck.backends import Backend from mkdocs_spellcheck.loggers import get_logger from mkdocs_spellcheck.words import get_words logger = get_logger(__name__) -backends_map: dict[str, Any] = { - "symspellpy": symspellpy.SymspellpyBackend, - "codespell": codespell.CodespellBackend, -} +def load_backend(name: str) -> Backend: + """Load the specified backend. + + This function imports the specified backend and returns its class. + It is important not to import the backends at the top level, as + they may not be installed. + + Arguments: + name: The name of the backend to load. + + Returns: + The backend class. + """ + if name == "symspellpy": + from mkdocs_spellcheck.backends import symspellpy + + return symspellpy.SymspellpyBackend + elif name == "codespell": + from mkdocs_spellcheck.backends import codespell + + return codespell.CodespellBackend + else: + raise ValueError(f"Unknown backend: {name}") class SpellCheckPlugin(BasePlugin): @@ -53,7 +71,6 @@ class SpellCheckPlugin(BasePlugin): def __init__(self) -> None: # noqa: D107 self.known_words: set[str] = set() - self.spell: SymSpell = None super().__init__() def on_config(self, config: Config, **kwargs: Any) -> Config: @@ -93,7 +110,7 @@ def on_config(self, config: Config, **kwargs: Any) -> Config: backend_config = {} else: backend_name, backend_config = next(iter(backend_conf.items())) - self.backends[backend_name] = backends_map[backend_name]( + self.backends[backend_name] = load_backend(backend_name)( known_words=self.known_words, config=backend_config, )