| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
from typing import IO, Dict, Mapping |
| 5 |
|
| 6 |
from mkdocs.config import base |
| 7 |
from mkdocs.config import config_options as c |
| 8 |
from mkdocs.structure.pages import Page, _AbsoluteLinksValidationValue |
| 9 |
from mkdocs.utils.yaml import get_yaml_loader, yaml_load |
| 10 |
|
| 11 |
|
| 12 |
class _LogLevel(c.OptionallyRequired[int]): |
| 13 |
levels: Mapping[str, int] = { |
| 14 |
"warn": logging.WARNING, |
| 15 |
"info": logging.INFO, |
| 16 |
"ignore": logging.DEBUG, |
| 17 |
} |
| 18 |
|
| 19 |
def run_validation(self, value: object) -> int: |
| 20 |
if not isinstance(value, str): |
| 21 |
raise base.ValidationError(f"Expected a string, but a {type(value)} was given.") |
| 22 |
try: |
| 23 |
return self.levels[value] |
| 24 |
except KeyError: |
| 25 |
raise base.ValidationError(f"Expected one of {list(self.levels)}, got {value!r}") |
| 26 |
|
| 27 |
|
| 28 |
class _AbsoluteLinksValidation(_LogLevel): |
| 29 |
levels: Mapping[str, int] = { |
| 30 |
**_LogLevel.levels, |
| 31 |
"relative_to_docs": _AbsoluteLinksValidationValue.RELATIVE_TO_DOCS, |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
# NOTE: The order here is important. During validation some config options |
| 36 |
# depend on others. So, if config option A depends on B, then A should be |
| 37 |
# listed higher in the schema. |
| 38 |
class MkDocsConfig(base.Config): |
| 39 |
"""The configuration of MkDocs itself (the root object of mkdocs.yml).""" |
| 40 |
|
| 41 |
config_file_path: str = c.Type(str) # type: ignore[assignment] |
| 42 |
"""The path to the mkdocs.yml config file. Can't be populated from the config.""" |
| 43 |
|
| 44 |
site_name = c.Type(str) |
| 45 |
"""The title to use for the documentation.""" |
| 46 |
|
| 47 |
nav = c.Optional(c.Nav()) |
| 48 |
"""Defines the structure of the navigation.""" |
| 49 |
pages = c.Deprecated(removed=True, moved_to='nav') |
| 50 |
|
| 51 |
exclude_docs = c.Optional(c.PathSpec()) |
| 52 |
"""Gitignore-like patterns of files (relative to docs dir) to exclude from the site.""" |
| 53 |
|
| 54 |
draft_docs = c.Optional(c.PathSpec()) |
| 55 |
"""Gitignore-like patterns of files (relative to docs dir) to mark as draft.""" |
| 56 |
|
| 57 |
not_in_nav = c.Optional(c.PathSpec()) |
| 58 |
"""Gitignore-like patterns of files (relative to docs dir) that are not intended to be in the nav. |
| 59 |
|
| 60 |
This marks doc files that are expected not to be in the nav, otherwise they will cause a log message |
| 61 |
(see also `validation.nav.omitted_files`). |
| 62 |
""" |
| 63 |
|
| 64 |
site_url = c.Optional(c.URL(is_dir=True)) |
| 65 |
"""The full URL to where the documentation will be hosted.""" |
| 66 |
|
| 67 |
site_description = c.Optional(c.Type(str)) |
| 68 |
"""A description for the documentation project that will be added to the |
| 69 |
HTML meta tags.""" |
| 70 |
site_author = c.Optional(c.Type(str)) |
| 71 |
"""The name of the author to add to the HTML meta tags.""" |
| 72 |
|
| 73 |
theme = c.Theme(default='mkdocs') |
| 74 |
"""The MkDocs theme for the documentation.""" |
| 75 |
|
| 76 |
docs_dir = c.DocsDir(default='docs', exists=True) |
| 77 |
"""The directory containing the documentation markdown.""" |
| 78 |
|
| 79 |
site_dir = c.SiteDir(default='site') |
| 80 |
"""The directory where the site will be built to""" |
| 81 |
|
| 82 |
copyright = c.Optional(c.Type(str)) |
| 83 |
"""A copyright notice to add to the footer of documentation.""" |
| 84 |
|
| 85 |
google_analytics = c.Deprecated( |
| 86 |
message=( |
| 87 |
'The configuration option {} has been deprecated and ' |
| 88 |
'will be removed in a future release of MkDocs. See the ' |
| 89 |
'options available on your theme for an alternative.' |
| 90 |
), |
| 91 |
option_type=c.Type(list, length=2), |
| 92 |
) |
| 93 |
"""set of values for Google analytics containing the account IO and domain |
| 94 |
this should look like, ['UA-27795084-5', 'mkdocs.org']""" |
| 95 |
|
| 96 |
dev_addr = c.IpAddress(default='127.0.0.1:8000') |
| 97 |
"""The address on which to serve the live reloading docs server.""" |
| 98 |
|
| 99 |
use_directory_urls = c.Type(bool, default=True) |
| 100 |
"""If `True`, use `<page_name>/index.html` style files with hyperlinks to |
| 101 |
the directory. If `False`, use `<page_name>.html style file with |
| 102 |
hyperlinks to the file. |
| 103 |
True generates nicer URLs, but False is useful if browsing the output on |
| 104 |
a filesystem.""" |
| 105 |
|
| 106 |
repo_url = c.Optional(c.URL()) |
| 107 |
"""Specify a link to the project source repo to be included |
| 108 |
in the documentation pages.""" |
| 109 |
|
| 110 |
repo_name = c.Optional(c.RepoName('repo_url')) |
| 111 |
"""A name to use for the link to the project source repo. |
| 112 |
Default, If repo_url is unset then None, otherwise |
| 113 |
"GitHub", "Bitbucket" or "GitLab" for known url or Hostname |
| 114 |
for unknown urls.""" |
| 115 |
|
| 116 |
edit_uri_template = c.Optional(c.EditURITemplate('edit_uri')) |
| 117 |
edit_uri = c.Optional(c.EditURI('repo_url')) |
| 118 |
"""Specify a URI to the docs dir in the project source repo, relative to the |
| 119 |
repo_url. When set, a link directly to the page in the source repo will |
| 120 |
be added to the generated HTML. If repo_url is not set also, this option |
| 121 |
is ignored.""" |
| 122 |
|
| 123 |
extra_css = c.Type(list, default=[]) |
| 124 |
extra_javascript = c.ListOfItems(c.ExtraScript(), default=[]) |
| 125 |
"""Specify which css or javascript files from the docs directory should be |
| 126 |
additionally included in the site.""" |
| 127 |
|
| 128 |
extra_templates = c.Type(list, default=[]) |
| 129 |
"""Similar to the above, but each template (HTML or XML) will be build with |
| 130 |
Jinja2 and the global context.""" |
| 131 |
|
| 132 |
markdown_extensions = c.MarkdownExtensions( |
| 133 |
builtins=['toc', 'tables', 'fenced_code'], configkey='mdx_configs' |
| 134 |
) |
| 135 |
"""PyMarkdown extension names.""" |
| 136 |
|
| 137 |
mdx_configs = c.Private[Dict[str, dict]]() |
| 138 |
"""PyMarkdown extension configs. Populated from `markdown_extensions`.""" |
| 139 |
|
| 140 |
strict = c.Type(bool, default=False) |
| 141 |
"""Enabling strict mode causes MkDocs to stop the build when a problem is |
| 142 |
encountered rather than display an error.""" |
| 143 |
|
| 144 |
remote_branch = c.Type(str, default='gh-pages') |
| 145 |
"""The remote branch to commit to when using gh-deploy.""" |
| 146 |
|
| 147 |
remote_name = c.Type(str, default='origin') |
| 148 |
"""The remote name to push to when using gh-deploy.""" |
| 149 |
|
| 150 |
extra = c.SubConfig() |
| 151 |
"""extra is a mapping/dictionary of data that is passed to the template. |
| 152 |
This allows template authors to require extra configuration that not |
| 153 |
relevant to all themes and doesn't need to be explicitly supported by |
| 154 |
MkDocs itself. A good example here would be including the current |
| 155 |
project version.""" |
| 156 |
|
| 157 |
plugins = c.Plugins(theme_key='theme', default=['search']) |
| 158 |
"""A list of plugins. Each item may contain a string name or a key value pair. |
| 159 |
A key value pair should be the string name (as the key) and a dict of config |
| 160 |
options (as the value).""" |
| 161 |
|
| 162 |
hooks = c.Hooks('plugins') |
| 163 |
"""A list of filenames that will be imported as Python modules and used as |
| 164 |
an instance of a plugin each.""" |
| 165 |
|
| 166 |
watch = c.ListOfPaths(default=[]) |
| 167 |
"""A list of extra paths to watch while running `mkdocs serve`.""" |
| 168 |
|
| 169 |
class Validation(base.Config): |
| 170 |
class NavValidation(base.Config): |
| 171 |
omitted_files = _LogLevel(default='info') |
| 172 |
"""Warning level for when a doc file is never mentioned in the navigation. |
| 173 |
For granular configuration, see `not_in_nav`.""" |
| 174 |
|
| 175 |
not_found = _LogLevel(default='warn') |
| 176 |
"""Warning level for when the navigation links to a relative path that isn't an existing page on the site.""" |
| 177 |
|
| 178 |
absolute_links = _AbsoluteLinksValidation(default='info') |
| 179 |
"""Warning level for when the navigation links to an absolute path (starting with `/`).""" |
| 180 |
|
| 181 |
nav = c.SubConfig(NavValidation) |
| 182 |
|
| 183 |
class LinksValidation(base.Config): |
| 184 |
not_found = _LogLevel(default='warn') |
| 185 |
"""Warning level for when a Markdown doc links to a relative path that isn't an existing document on the site.""" |
| 186 |
|
| 187 |
absolute_links = _AbsoluteLinksValidation(default='info') |
| 188 |
"""Warning level for when a Markdown doc links to an absolute path (starting with `/`).""" |
| 189 |
|
| 190 |
unrecognized_links = _LogLevel(default='info') |
| 191 |
"""Warning level for when a Markdown doc links to a relative path that doesn't look like |
| 192 |
it could be a valid internal link. For example, if the link ends with `/`.""" |
| 193 |
|
| 194 |
anchors = _LogLevel(default='info') |
| 195 |
"""Warning level for when a Markdown doc links to an anchor that's not present on the target page.""" |
| 196 |
|
| 197 |
links = c.SubConfig(LinksValidation) |
| 198 |
|
| 199 |
validation = c.PropagatingSubConfig[Validation]() |
| 200 |
|
| 201 |
_current_page: Page | None = None |
| 202 |
"""The currently rendered page. Please do not access this and instead |
| 203 |
rely on the `page` argument to event handlers.""" |
| 204 |
|
| 205 |
def load_dict(self, patch: dict) -> None: |
| 206 |
super().load_dict(patch) |
| 207 |
if 'config_file_path' in patch: |
| 208 |
raise base.ValidationError("Can't set config_file_path in config") |
| 209 |
|
| 210 |
def load_file(self, config_file: IO) -> None: |
| 211 |
"""Load config options from the open file descriptor of a YAML file.""" |
| 212 |
loader = get_yaml_loader(config=self) |
| 213 |
self.load_dict(yaml_load(config_file, loader)) |
| 214 |
|
| 215 |
|
| 216 |
def get_schema() -> base.PlainConfigSchema: |
| 217 |
"""Soft-deprecated, do not use.""" |
| 218 |
return MkDocsConfig._schema |