Skip to content

GeneratorSettings

🛈 DocStrings

Settings for generating a website from a MkNodes build function.

Parameters:

Name Type Description Default
project_dir Path

Return a new path pointing to the current working directory.

PosixPath('/home/runner/work/mkdocs-mknodes/mkdocs-mknodes')
config_file Path | None

The site config the generated config inherits from (mkdocs.yml).

None
build_fn str

Build function (module:callable, path/file.py:callable, module:Class.method).

'mkdocs_mknodes:parse'
kwargs dict[str, Any]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>
repo_path str

Repository to build docs for (local path or remote URL).

'.'
clone_depth int

Clone depth for remote repositories.

100
show_page_info bool

Append a collapsible box with build info to every page.

False
render_by_default bool

Render generated pages with the MkNodes Jinja environment.

True
global_resources bool

Put CSS / JS into the site config instead of per-page template blocks.

True
docs_dir str | None

Docs folder (default: docs_dir of the site config, else docs).

None
generated_dir str | None

Folder to write generated pages into (default: docs_dir).

If it differs from docs_dir, hand-written pages get mirrored into it and it becomes the docs dir of the generated config.

None
generated_config str

Filename of the generated site config (relative to the project dir).

'mkdocs.generated.yml'
site_dir str | None

Output folder for the HTML build (default: the site config's site_dir).

None
jinja_loaders list[dict[str, Any]] | None
None
jinja_extensions list[str] | None
None
jinja_block_start_string str | None
None
jinja_block_end_string str | None
None
jinja_variable_start_string str | None
None
jinja_variable_end_string str | None
None
site_config dict[str, Any]

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

<class 'dict'>
Source code in mkdocs_mknodes/generator/settings.py
@dataclasses.dataclass
class GeneratorSettings:
    """Settings for generating a website from a MkNodes build function."""

    project_dir: pathlib.Path = dataclasses.field(default_factory=pathlib.Path.cwd)
    """Root folder of the project (contains `mkdocs.yml` / `pyproject.toml`)."""
    config_file: pathlib.Path | None = None
    """The site config the generated config inherits from (`mkdocs.yml`)."""
    build_fn: str = paths.DEFAULT_BUILD_FN
    """Build function (`module:callable`, `path/file.py:callable`, `module:Class.method`)."""
    kwargs: dict[str, Any] = dataclasses.field(default_factory=dict)
    """Keyword arguments for the build function (`pages` for the default build function)."""
    repo_path: str = "."
    """Repository to build docs for (local path or remote URL)."""
    clone_depth: int = 100
    """Clone depth for remote repositories."""
    show_page_info: bool = False
    """Append a collapsible box with build info to every page."""
    render_by_default: bool = True
    """Render generated pages with the MkNodes Jinja environment."""
    global_resources: bool = True
    """Put CSS / JS into the site config instead of per-page template blocks."""
    docs_dir: str | None = None
    """Docs folder (default: `docs_dir` of the site config, else `docs`)."""
    generated_dir: str | None = None
    """Folder to write generated pages into (default: `docs_dir`).

    If it differs from `docs_dir`, hand-written pages get mirrored into it and it becomes
    the docs dir of the generated config.
    """
    generated_config: str = GENERATED_CONFIG
    """Filename of the generated site config (relative to the project dir)."""
    site_dir: str | None = None
    """Output folder for the HTML build (default: the site config's `site_dir`)."""
    jinja_loaders: list[dict[str, Any]] | None = None
    jinja_extensions: list[str] | None = None
    jinja_block_start_string: str | None = None
    jinja_block_end_string: str | None = None
    jinja_variable_start_string: str | None = None
    jinja_variable_end_string: str | None = None
    site_config: dict[str, Any] = dataclasses.field(default_factory=dict, repr=False)
    """The loaded site config (`INHERIT` resolved, custom tags preserved)."""

    @property
    def resolved_docs_dir(self) -> pathlib.Path:
        name = self.docs_dir or self.site_config.get("docs_dir") or "docs"
        return self.project_dir / name

    @property
    def resolved_generated_dir(self) -> pathlib.Path:
        if self.generated_dir:
            return self.project_dir / self.generated_dir
        return self.resolved_docs_dir

    @property
    def generated_config_path(self) -> pathlib.Path:
        return self.project_dir / self.generated_config

    @property
    def theme_config(self) -> dict[str, Any]:
        theme = self.site_config.get("theme") or {}
        if isinstance(theme, str):
            theme = {"name": theme}
        return yamlconfig.resolve(theme)

    def get_builder(self) -> Callable[..., Any]:
        """Return the build function with the configured kwargs bound."""
        from mknodes.utils import classhelpers

        build_fn_path = self.build_fn
        module, sep, qualname = build_fn_path.rpartition(":")
        if module.endswith(".py") and not pathlib.Path(module).is_absolute():
            build_fn_path = f"{(self.project_dir / module).as_posix()}{sep}{qualname}"
        build_fn = classhelpers.to_callable(build_fn_path)
        return functools.partial(build_fn, **self.kwargs)

    def get_jinja_config(self) -> jinjarope.EnvConfig:
        import jinjarope

        cfg = jinjarope.EnvConfig(
            block_start_string=self.jinja_block_start_string or "{%",
            block_end_string=self.jinja_block_end_string or "%}",
            variable_start_string=self.jinja_variable_start_string or r"{{",
            variable_end_string=self.jinja_variable_end_string or r"}}",
            loader=jinjarope.loaders.from_json(self.jinja_loaders),
        )
        if self.jinja_extensions:
            cfg.extensions = list(self.jinja_extensions)
        docs_loader = jinjarope.FileSystemLoader(str(self.resolved_docs_dir))
        loader = cfg.loader
        cfg.loader = jinjarope.ChoiceLoader([loader, docs_loader]) if loader else docs_loader
        return cfg

    @classmethod
    def load(
        cls,
        project_dir: str | os.PathLike[str] | None = None,
        config_file: str | os.PathLike[str] | None = None,
        **overrides: Any,
    ) -> GeneratorSettings:
        """Load settings for given project.

        Args:
            project_dir: Project root (default: cwd, or the folder of `config_file`)
            config_file: Site config to read (default: `mkdocs.yml` in the project dir)
            overrides: Explicit setting overrides (`None` values are ignored)
        """
        if project_dir is None:
            project_dir = pathlib.Path(config_file).parent if config_file else pathlib.Path()
        root = pathlib.Path(project_dir).resolve()
        cfg_path = pathlib.Path(config_file) if config_file else None
        if cfg_path and not cfg_path.is_absolute():
            cfg_path = (pathlib.Path.cwd() / cfg_path).resolve()
        if cfg_path is None:
            cfg_path = next((root / n for n in CONFIG_NAMES if (root / n).is_file()), None)
        site_config = yamlconfig.load_file(cfg_path) if cfg_path and cfg_path.is_file() else {}
        values: dict[str, Any] = {}
        values |= _normalize(get_plugin_config(site_config))
        values |= _normalize(_read_pyproject(root))
        values |= {k: v for k, v in _normalize(overrides).items() if v is not None}
        if "pages" in values:  # shortcut for the default build function
            values.setdefault("kwargs", {})["pages"] = values.pop("pages")
        fields = {f.name for f in dataclasses.fields(cls)}
        known = {k: v for k, v in values.items() if k in fields}
        known["kwargs"] = known.get("kwargs") or {}
        return cls(
            project_dir=root,
            config_file=cfg_path if cfg_path and cfg_path.is_file() else None,
            site_config=site_config,
            **{k: v for k, v in known.items() if k not in {"project_dir", "config_file"}},
        )

build_fn class-attribute instance-attribute

build_fn: str = paths.DEFAULT_BUILD_FN

Build function (module:callable, path/file.py:callable, module:Class.method).

clone_depth class-attribute instance-attribute

clone_depth: int = 100

Clone depth for remote repositories.

config_file class-attribute instance-attribute

config_file: Path | None = None

The site config the generated config inherits from (mkdocs.yml).

docs_dir class-attribute instance-attribute

docs_dir: str | None = None

Docs folder (default: docs_dir of the site config, else docs).

generated_config class-attribute instance-attribute

generated_config: str = GENERATED_CONFIG

Filename of the generated site config (relative to the project dir).

generated_dir class-attribute instance-attribute

generated_dir: str | None = None

Folder to write generated pages into (default: docs_dir).

If it differs from docs_dir, hand-written pages get mirrored into it and it becomes the docs dir of the generated config.

global_resources class-attribute instance-attribute

global_resources: bool = True

Put CSS / JS into the site config instead of per-page template blocks.

kwargs class-attribute instance-attribute

kwargs: dict[str, Any] = dataclasses.field(default_factory=dict)

Keyword arguments for the build function (pages for the default build function).

project_dir class-attribute instance-attribute

project_dir: Path = dataclasses.field(default_factory=pathlib.Path.cwd)

Root folder of the project (contains mkdocs.yml / pyproject.toml).

render_by_default class-attribute instance-attribute

render_by_default: bool = True

Render generated pages with the MkNodes Jinja environment.

repo_path class-attribute instance-attribute

repo_path: str = '.'

Repository to build docs for (local path or remote URL).

show_page_info class-attribute instance-attribute

show_page_info: bool = False

Append a collapsible box with build info to every page.

site_config class-attribute instance-attribute

site_config: dict[str, Any] = dataclasses.field(default_factory=dict, repr=False)

The loaded site config (INHERIT resolved, custom tags preserved).

site_dir class-attribute instance-attribute

site_dir: str | None = None

Output folder for the HTML build (default: the site config's site_dir).

get_builder

get_builder() -> Callable[..., Any]

Return the build function with the configured kwargs bound.

Source code in mkdocs_mknodes/generator/settings.py
def get_builder(self) -> Callable[..., Any]:
    """Return the build function with the configured kwargs bound."""
    from mknodes.utils import classhelpers

    build_fn_path = self.build_fn
    module, sep, qualname = build_fn_path.rpartition(":")
    if module.endswith(".py") and not pathlib.Path(module).is_absolute():
        build_fn_path = f"{(self.project_dir / module).as_posix()}{sep}{qualname}"
    build_fn = classhelpers.to_callable(build_fn_path)
    return functools.partial(build_fn, **self.kwargs)

load classmethod

load(
    project_dir: str | PathLike[str] | None = None,
    config_file: str | PathLike[str] | None = None,
    **overrides: Any
) -> GeneratorSettings

Load settings for given project.

Parameters:

Name Type Description Default
project_dir str | PathLike[str] | None

Project root (default: cwd, or the folder of config_file)

None
config_file str | PathLike[str] | None

Site config to read (default: mkdocs.yml in the project dir)

None
overrides Any

Explicit setting overrides (None values are ignored)

{}
Source code in mkdocs_mknodes/generator/settings.py
@classmethod
def load(
    cls,
    project_dir: str | os.PathLike[str] | None = None,
    config_file: str | os.PathLike[str] | None = None,
    **overrides: Any,
) -> GeneratorSettings:
    """Load settings for given project.

    Args:
        project_dir: Project root (default: cwd, or the folder of `config_file`)
        config_file: Site config to read (default: `mkdocs.yml` in the project dir)
        overrides: Explicit setting overrides (`None` values are ignored)
    """
    if project_dir is None:
        project_dir = pathlib.Path(config_file).parent if config_file else pathlib.Path()
    root = pathlib.Path(project_dir).resolve()
    cfg_path = pathlib.Path(config_file) if config_file else None
    if cfg_path and not cfg_path.is_absolute():
        cfg_path = (pathlib.Path.cwd() / cfg_path).resolve()
    if cfg_path is None:
        cfg_path = next((root / n for n in CONFIG_NAMES if (root / n).is_file()), None)
    site_config = yamlconfig.load_file(cfg_path) if cfg_path and cfg_path.is_file() else {}
    values: dict[str, Any] = {}
    values |= _normalize(get_plugin_config(site_config))
    values |= _normalize(_read_pyproject(root))
    values |= {k: v for k, v in _normalize(overrides).items() if v is not None}
    if "pages" in values:  # shortcut for the default build function
        values.setdefault("kwargs", {})["pages"] = values.pop("pages")
    fields = {f.name for f in dataclasses.fields(cls)}
    known = {k: v for k, v in values.items() if k in fields}
    known["kwargs"] = known.get("kwargs") or {}
    return cls(
        project_dir=root,
        config_file=cfg_path if cfg_path and cfg_path.is_file() else None,
        site_config=site_config,
        **{k: v for k, v in known.items() if k not in {"project_dir", "config_file"}},
    )

:fa-brands-github: Show source on GitHub

Page info
Resources
Resources(css=[],
          markdown_extensions={},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mkdocs-mknodes/mkdocs-mknodes/.venv/lib/python3.13/site-packages/mknodes/pages/mkclasspage/__init__.py
  source_function: MkClassPage.__init__
  source_line_no: 46
title: GeneratorSettings