Coverage for .nox/test/lib/python3.12/site-packages/mkdocs_licenseinfo/plugin.py: 97%

32 statements  

« prev     ^ index     » next       coverage.py v7.3.4, created at 2023-12-27 09:44 +0000

1"""This module contains the MkDocs plugin. 

2 

3It creates a Markdown extension ([`LicenseInfoExtension`][mkdocs_licenseinfo.extension.LicenseInfoExtension]), 

4and adds it to `mkdocs` during the [`on_config` event hook](https://www.mkdocs.org/user-guide/plugins/#on_config). 

5""" 

6 

7from __future__ import annotations 

8 

9from typing import TYPE_CHECKING 

10 

11from mkdocs.config import Config 

12from mkdocs.config import config_options as opt 

13from mkdocs.plugins import BasePlugin 

14 

15from mkdocs_licenseinfo.extension import LicenseInfoExtension 

16 

17if TYPE_CHECKING: 

18 from mkdocs.config.defaults import MkDocsConfig 

19 

20 

21class PluginConfig(Config): 

22 """Configuration options for `mkdocs_github_changelog` in `mkdocs.yml`.""" 

23 ignore_packages = opt.Optional(opt.ListOfItems(opt.Type(str))) 

24 """Packages to ignore.""" 

25 fail_packages = opt.Optional(opt.ListOfItems(opt.Type(str))) 

26 """Packages to fail on.""" 

27 skip_packages = opt.Optional(opt.ListOfItems(opt.Type(str))) 

28 """Packages to skip.""" 

29 ignore_licenses = opt.Optional(opt.ListOfItems(opt.Type(str))) 

30 """Licenses to ignore.""" 

31 fail_licenses = opt.Optional(opt.ListOfItems(opt.Type(str))) 

32 """Licenses to fail on.""" 

33 requirements_path = opt.Optional(opt.Type(str)) 

34 """Path to the requirements/pyproject.toml dir relative to docs dir (otherwise uses the invocation directory).""" 

35 package_template = opt.Optional(opt.Type(str)) 

36 """Jinja2 template string to override the default.""" 

37 enabled = opt.Type(bool, default=True) 

38 """Enable or disable the plugin.""" 

39 

40 

41class MkdocsLicenseInfoPlugin(BasePlugin[PluginConfig]): 

42 """`mkdocs` plugin to provide the licenseinfo.""" 

43 

44 def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None: 

45 """Initialises the extension if the plugin is enabled.""" 

46 self.config.docs_dir = config.docs_dir 

47 if self.config.enabled: 

48 licenseinfo_extension = LicenseInfoExtension(self.config) 

49 config.markdown_extensions.append(licenseinfo_extension) # type: ignore[arg-type] 

50 return config