Skip to content

docs_site

Builds the documentation site from the source that ships in the package (DOCS_SOURCE_DIR), keeps it under ~/.mujoco-mojo/docs/v<version>/, and serves it.

Building needs the docs extra (Zensical and the API reference plugins). Serving does not: it is a static folder behind the web server that Mojo already has, so a version that was built once can be served without those packages.

The bundled source has no images (see scripts/gen_docs_source.py), so the build points every link to a file that is not there at the same file on GitHub, at the tag of the version being built.

DocsBuildError

Bases: RuntimeError

The docs site could not be built. The message says why, including the end of the builder's output when it failed.

docs_site_dir

Python
docs_site_dir(version: str) -> Path

The folder the built site of version is kept in.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def docs_site_dir(version: str) -> Path:
    """The folder the built site of `version` is kept in."""
    return MUJOCO_MOJO_DIR / "docs" / f"v{version}"

is_built

Python
is_built(site_dir: Path) -> bool

Whether site_dir holds a built site.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def is_built(site_dir: Path) -> bool:
    """Whether `site_dir` holds a built site."""
    return (site_dir / "index.html").is_file()

built_versions

Python
built_versions() -> list[str]

The versions that have a built site, in version order, oldest first.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def built_versions() -> list[str]:
    """The versions that have a built site, in version order, oldest first."""
    root = MUJOCO_MOJO_DIR / "docs"
    if not root.is_dir():
        return []
    return sorted(
        (
            folder.name.removeprefix("v")
            for folder in root.iterdir()
            if folder.name.startswith("v") and is_built(folder)
        ),
        key=_version_key,
    )

github_ref

Python
github_ref(version: str) -> str

The git ref the files of version are at: its release tag, or master for a version that has no tag (a development build, or the fallback version of a package that is not installed).

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def github_ref(version: str) -> str:
    """The git ref the files of `version` are at: its release tag, or `master` for a version that has no tag (a development build, or the fallback version of a package that is not installed)."""
    if ".dev" in version or "+" in version or version.startswith("0.0.0"):
        return "master"
    return f"v{version}"

require_docs_extra

Python
require_docs_extra() -> None

Raises ModuleNotFoundError, with how to install them, if a package the build needs is missing.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def require_docs_extra() -> None:
    """Raises `ModuleNotFoundError`, with how to install them, if a package the build needs is missing."""
    missing = [
        package
        for module, package in DOCS_EXTRA_MODULES.items()
        if importlib.util.find_spec(module) is None
    ]
    if missing:
        raise ModuleNotFoundError(
            f"Building the docs needs the `docs` extra (missing: {', '.join(missing)}). "
            "Install with `uv add mujoco-mojo[docs]` or `pip install mujoco-mojo[docs]`"
        )
Python
link_missing_assets(docs_dir: Path, ref: str) -> int

Points each image, video, video poster and video source in the pages of docs_dir that is not there at the same file on GitHub at ref, keeping the part after the file name (#only-dark). Anything inside a code block is left alone. A tag's attributes have to be on the line it opens on. Returns how many links it changed.

This rule, a link to a file that is missing, is the whole of it: a build from a checkout that has the files changes nothing, and nothing has to list what the bundle leaves out.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def link_missing_assets(docs_dir: Path, ref: str) -> int:
    """
    Points each image, video, video poster and video source in the pages of `docs_dir` that is not there at the same file on GitHub at `ref`, keeping the part after the file name (`#only-dark`). Anything inside a code block is left alone. A tag's attributes have to be on the line it opens on. Returns how many links it changed.

    This rule, a link to a file that is missing, is the whole of it: a build from a checkout that has the files changes nothing, and nothing has to list what the bundle leaves out.
    """
    changed = 0

    def rewriter(page: Path) -> Callable[[re.Match[str]], str]:
        def swap(match: re.Match[str]) -> str:
            nonlocal changed
            head: str = match["head"]
            url = _github_url(page, match["url"], docs_dir, ref)
            if url is None:
                return match.group(0)
            changed += 1
            return head + url

        return swap

    for page in sorted(docs_dir.rglob("*.md")):
        swap = rewriter(page)
        original = page.read_text(encoding="utf-8")
        in_fence = False
        lines: list[str] = []
        for line in original.splitlines(keepends=True):
            if _FENCE.match(line):
                in_fence = not in_fence
            elif not in_fence:
                line = _HTML_MEDIA_TAG.sub(
                    lambda tag: _HTML_MEDIA_URL.sub(swap, tag.group(0)),
                    _MD_IMAGE.sub(swap, line),
                )
            lines.append(line)
        text = "".join(lines)
        if text != original:
            page.write_text(text, encoding="utf-8", newline="")
    return changed

patch_config

Python
patch_config(config: Path, package_dir: Path) -> None

Points the two paths in config (mkdocs.yml) that name the package's source in the repo, the one the API reference is written from and the one the docstrings are read from, at the installed package at package_dir, removes the watch: list, makes the site's address a local one, and makes the site work offline (_make_offline). A text edit and not a YAML round trip, since the file has !!python/name: tags. Only the copy that is built here is edited: the online docs are built from the repo's own config.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def patch_config(config: Path, package_dir: Path) -> None:
    """Points the two paths in `config` (`mkdocs.yml`) that name the package's source in the repo, the one the API reference is written from and the one the docstrings are read from, at the installed package at `package_dir`, removes the `watch:` list, makes the site's address a local one, and makes the site work offline (`_make_offline`). A text edit and not a YAML round trip, since the file has `!!python/name:` tags. Only the copy that is built here is edited: the online docs are built from the repo's own config."""
    text = config.read_text(encoding="utf-8")
    for setting in (_MODULES_SETTING, _PATHS_SETTING):
        if setting not in text:
            raise DocsBuildError(
                f"The bundled mkdocs.yml has no `{setting}` line, so the build cannot find the package's source. Update `patch_config` in mujoco_mojo/utils/docs_site.py to match it."
            )
    text = text.replace(
        _MODULES_SETTING, f"modules: [{json.dumps(package_dir.as_posix())}]"
    )
    text = text.replace(
        _PATHS_SETTING, f"paths: [{json.dumps(package_dir.parent.as_posix())}]"
    )
    text = _WATCH_SETTING.sub("", text)
    text = _SITE_URL_SETTING.sub(f"site_url: {_LOCAL_SITE_URL}", text)
    config.write_text(_make_offline(text), encoding="utf-8", newline="")

build_docs

Python
build_docs(
    version: str,
    *,
    rebuild: bool = False,
    source_dir: Path = DOCS_SOURCE_DIR,
    package_dir: Path | None = None,
) -> Path

Builds the docs site of version and returns its folder (docs_site_dir). A site that is already built is returned as it is unless rebuild is set.

The build runs in a scratch copy of the bundled source beside the result, since the builder writes next to its config and wants its output folder inside the config's folder, and the bundle is in the install, which may be read-only. The scratch copy is removed whether the build works or not, and the site only appears in its place once it is whole, so a failed build leaves nothing behind. package_dir is the package the API reference is written from (the installed one).

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def build_docs(
    version: str,
    *,
    rebuild: bool = False,
    source_dir: Path = DOCS_SOURCE_DIR,
    package_dir: Path | None = None,
) -> Path:
    """
    Builds the docs site of `version` and returns its folder (`docs_site_dir`). A site that is already built is returned as it is unless `rebuild` is set.

    The build runs in a scratch copy of the bundled source beside the result, since the builder writes next to its config and wants its output folder inside the config's folder, and the bundle is in the install, which may be read-only. The scratch copy is removed whether the build works or not, and the site only appears in its place once it is whole, so a failed build leaves nothing behind. `package_dir` is the package the API reference is written from (the installed one).
    """
    require_docs_extra()
    target = docs_site_dir(version)
    if is_built(target) and not rebuild:
        return target
    if package_dir is None:
        package_dir = Path(__file__).parents[1]

    target.parent.mkdir(parents=True, exist_ok=True)
    stage = Path(tempfile.mkdtemp(prefix=f".build-v{version}-", dir=target.parent))
    try:
        _copy_source(source_dir, stage)
        link_missing_assets(stage / "docs", github_ref(version))
        patch_config(stage / "mkdocs.yml", package_dir)
        result = subprocess.run(
            [sys.executable, "-m", "zensical", "build", "--clean", "-f", "mkdocs.yml"],
            cwd=stage,
            capture_output=True,
            text=True,
            check=False,
            env={**os.environ, "PYTHONUTF8": "1", "NO_COLOR": "1"},
        )
        if result.returncode != 0:
            output = (result.stdout + result.stderr).strip().splitlines()
            raise DocsBuildError(
                "The docs build failed:\n" + "\n".join(output[-ERROR_TAIL_LINES:])
            )
        if not is_built(stage / "site"):
            raise DocsBuildError("The docs build finished but made no site.")
        if target.exists():
            shutil.rmtree(target)
        os.replace(stage / "site", target)
    finally:
        shutil.rmtree(stage, ignore_errors=True)
    return target

docs_app_for

Python
docs_app_for(site_dir: Path) -> Starlette

The web app that serves the built site in site_dir: index.html for a folder, a redirect to the folder's trailing slash, and the site's own 404.html.

Source code in src/mujoco_mojo/utils/docs_site.py
Python
def docs_app_for(site_dir: Path) -> Starlette:
    """The web app that serves the built site in `site_dir`: `index.html` for a folder, a redirect to the folder's trailing slash, and the site's own `404.html`."""
    return Starlette(
        routes=[Mount("/", app=StaticFiles(directory=site_dir, html=True), name="docs")]
    )