From 4f8bec4de75cf9421ed7cf679cb04ce9f903fdee Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 18 Feb 2026 11:36:33 +0000 Subject: [PATCH] Cleanup config --- src/mkdocs/__init__.py | 4 +-- src/mkdocs/cli.py | 62 ++++++++++++++++++++++++++++++++-- src/mkdocs/mkdocs.py | 76 ++---------------------------------------- 3 files changed, 63 insertions(+), 79 deletions(-) diff --git a/src/mkdocs/__init__.py b/src/mkdocs/__init__.py index 114738f..c0ca190 100644 --- a/src/mkdocs/__init__.py +++ b/src/mkdocs/__init__.py @@ -1,4 +1,4 @@ from .mkdocs import MkDocs -from .cli import cli +from .cli import cli, build, serve -__all__ = [MkDocs, cli] +__all__ = [MkDocs, cli, build, serve] diff --git a/src/mkdocs/cli.py b/src/mkdocs/cli.py index 27caae5..bfe8df5 100644 --- a/src/mkdocs/cli.py +++ b/src/mkdocs/cli.py @@ -1,7 +1,8 @@ +import pathlib +import tomllib + import click import mkdocs - -import pathlib import flask @@ -18,6 +19,55 @@ } +def default_config() -> dict: + return { + 'mkdocs': { + 'nav': [], + }, + 'loaders': { + 'theme': 'pkg://mkdocs/default', + 'docs': 'dir://', + }, + 'context': { + }, + 'markdown': { + 'extensions': [ + 'fenced_code', + 'footnotes', + 'tables', + 'toc', + # 'pymdownx.tasklist', + # 'gfm_admonition', + 'mkdocs.extensions.rewrite_urls', + 'mkdocs.extensions.short_codes', + 'mkdocs.extensions.strike_thru', + ], + 'configs': { + 'footnotes': {'BACKLINK_TITLE': ''}, + 'toc': {'anchorlink': True, 'marker': ''} + }, + }, + # 'commands': { + # 'build': 'mkdocs:build', + # 'serve': 'mkdocs:serve', + # } + } + + +def load_config(path: pathlib.Path) -> dict: + # Read the mkdocs.toml + text = path.read_text() + config = tomllib.loads(text) + + # Merge the default config. + default = default_config() + for key, value in default.items(): + if key not in config: + config[key] = value + + return config + + def build(mk: mkdocs.MkDocs) -> None: buildpath = pathlib.Path('site') for resource in mk.resources: @@ -53,7 +103,13 @@ def _(path=''): def cli(): - mk = mkdocs.MkDocs() + path = pathlib.Path('mkdocs.toml') + if path.exists(): + config = load_config(path) + else: + config = default_config() + + mk = mkdocs.MkDocs(config) group = click.Group(commands=[ click.Command(name='build', callback=lambda: build(mk)), click.Command(name='serve', callback=lambda: serve(mk)), diff --git a/src/mkdocs/mkdocs.py b/src/mkdocs/mkdocs.py index 31b495f..2939c69 100644 --- a/src/mkdocs/mkdocs.py +++ b/src/mkdocs/mkdocs.py @@ -14,32 +14,6 @@ from .extensions.rewrite_urls import PageContext -# Config... - -class Config: - def __init__(self, config: dict, filename: str) -> None: - self._config = config - self._filename = filename - - def get(self, *args: str): - value = self._config - for arg in args: - if not isinstance(value, dict) or arg not in value: - return None - value = value[arg] - return value - - def __getitem__(self, key: str) -> typing.Any: - return self._config[key] - - def __repr__(self): - return f'' - - -class ConfigError(Exception): - pass - - class Page: def __init__(self, text: str, html: str, toc: str, title: str, url: str, path: str): self.text = text # The markdown text @@ -244,13 +218,13 @@ def get_source(self, environment: jinja2.Environment, template: str): # Here we go... class MkDocs: - def __init__(self): + def __init__(self, config: dict) -> None: self.loaders = { 'https': ZipURL, 'pkg': Package, 'dir': Directory, } - self.config = self.load_config('mkdocs.toml') + self.config = config self.handlers = self.load_handlers(self.config) self.resources, self.templates = self.load_resources(self.handlers) self.env = self.load_env(self.templates) @@ -272,52 +246,6 @@ def path_to_url(self, path: pathlib.Path) -> str: # 'css/styles.css' -> '/css/styles.css' return pathlib.Path('/').joinpath(path).as_posix() - def load_config(self, filename: str) -> dict: - path = pathlib.Path(filename) - if not path.exists(): - print("* No 'config.toml' file, using defaults.") - config = {} - # raise ConfigError(f"Missing config '{filename}'") - else: - text = path.read_text() - try: - config = tomllib.loads(text) - except tomllib.TOMLDecodeError as exc: - raise ConfigError(f"Invalid TOML in config '{filename}'\n{exc}") - - default = { - 'mkdocs': { - 'nav': [], - }, - 'loaders': { - 'theme': 'pkg://mkdocs/default', - 'docs': 'dir://', - }, - 'context': { - }, - 'markdown': { - 'extensions': [ - 'fenced_code', - 'footnotes', - 'tables', - 'toc', - # 'pymdownx.tasklist', - # 'gfm_admonition', - 'mkdocs.extensions.rewrite_urls', - 'mkdocs.extensions.short_codes', - 'mkdocs.extensions.strike_thru', - ], - 'configs': { - 'footnotes': {'BACKLINK_TITLE': ''}, - 'toc': {'anchorlink': True, 'marker': ''} - }, - }, - } - for key, value in default.items(): - if key not in config: - config[key] = value - return Config(config, filename=filename) - def load_handlers(self, config: dict) -> list[Handler]: loaders_config = config['loaders']