Coverage for src/mkdocs_spellcheck/backends/codespell.py: 0.00%
23 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-05-05 19:28 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2024-05-05 19:28 +0200
1"""Backend for the `codespell` tool."""
3from __future__ import annotations
5import os
6from typing import TYPE_CHECKING, Any
8from codespell_lib._codespell import (
9 Misspelling,
10 _builtin_default,
11 _builtin_dictionaries,
12 _data_root,
13 build_dict,
14 fix_case,
15)
17from mkdocs_spellcheck.backends import Backend
18from mkdocs_spellcheck.loggers import get_plugin_logger
20if TYPE_CHECKING:
21 from mkdocs.structure.pages import Page
23logger = get_plugin_logger(__name__)
25DEFAULT_DICTS = _builtin_default.split(",")
28class CodespellBackend(Backend):
29 """Backend for the `codespell` tool."""
31 def __init__(self, config: dict[str, Any], known_words: set[str] | None = None) -> None:
32 """Initialize the `codespell` backend.
34 This backend needs to build a list of misspellings based
35 on dictionaries provided by `codespell` itself.
37 Parameters:
38 config: User configuration from `mkdocs.yml`.
39 known_words: Globally known words.
40 """
41 known_words = known_words or set()
42 use_dictionaries = []
43 for dictionary in config.get("dictionaries", DEFAULT_DICTS):
44 for builtin in _builtin_dictionaries:
45 if builtin[0] == dictionary:
46 use_dictionaries.append(os.path.join(_data_root, f"dictionary{builtin[2]}.txt"))
47 self.misspellings: dict[str, Misspelling] = {}
48 for dictionary in use_dictionaries:
49 build_dict(dictionary, self.misspellings, known_words)
51 def check(self, page: Page, word: str) -> None: # noqa: D102
52 if word in self.misspellings:
53 # reason = self.misspellings[word].reason
54 fixword = fix_case(word, self.misspellings[word].data)
55 logger.warning(f"(codespell) {page.file.src_path}: Misspelled '{word}', did you mean '{fixword}'?")