|
| 1 | +#!/usr/bin/env -S uv run --script |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.14" |
| 4 | +# dependencies = [ |
| 5 | +# "tomlkit>=0.14.0", |
| 6 | +# ] |
| 7 | +# /// |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import dataclasses |
| 13 | +import os |
| 14 | +from collections.abc import Sequence |
| 15 | +from pathlib import Path |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +import tomlkit |
| 19 | + |
| 20 | +MARKDOWN_TEMPLATE: str = """\ |
| 21 | +::: {module_path} |
| 22 | + options: |
| 23 | + toc_label: {module_path} |
| 24 | +""" |
| 25 | +MODULE_SYMBOL: str = '<code class="doc-symbol doc-symbol-toc doc-symbol-module"></code>' |
| 26 | + |
| 27 | + |
| 28 | +class Args(argparse.Namespace): |
| 29 | + api_root: Path |
| 30 | + docs_dir: Path |
| 31 | + print_nav: bool |
| 32 | + src: Path |
| 33 | + |
| 34 | + |
| 35 | +@dataclasses.dataclass |
| 36 | +class Nav: |
| 37 | + module_path: str = "" |
| 38 | + index: str | None = None |
| 39 | + children: dict[str, Nav] = dataclasses.field(default_factory=dict) |
| 40 | + |
| 41 | + def add(self, parts: Sequence[str], full_doc_path: Path) -> None: |
| 42 | + if not parts: |
| 43 | + self.index = os.fspath(full_doc_path) |
| 44 | + return |
| 45 | + if parts[0] in self.children: |
| 46 | + child = self.children[parts[0]] |
| 47 | + else: |
| 48 | + child = type(self)( |
| 49 | + module_path=f"{self.module_path}.{parts[0]}" |
| 50 | + if self.module_path |
| 51 | + else parts[0] |
| 52 | + ) |
| 53 | + self.children[parts[0]] = child |
| 54 | + child.add(parts[1:], full_doc_path) |
| 55 | + |
| 56 | + def dump(self) -> Any: |
| 57 | + children: list[Any] = [] |
| 58 | + if self.index is not None: |
| 59 | + children.append(self.index) |
| 60 | + for _, child in sorted(self.children.items()): |
| 61 | + children.append(child.dump()) |
| 62 | + if len(children) == 1: |
| 63 | + return children[0] |
| 64 | + return {f"{MODULE_SYMBOL} {self.module_path}": children} |
| 65 | + |
| 66 | + |
| 67 | +def is_public(part: str) -> bool: |
| 68 | + return not part.startswith("_") or (part.startswith("__") and part.endswith("__")) |
| 69 | + |
| 70 | + |
| 71 | +def parse_args() -> Args: |
| 72 | + parser: argparse.ArgumentParser = argparse.ArgumentParser() |
| 73 | + parser.add_argument("src", nargs="?", default="src", type=Path) |
| 74 | + parser.add_argument("--api-root", default="reference/", type=Path) |
| 75 | + parser.add_argument("--docs-dir", default="docs/", type=Path) |
| 76 | + parser.add_argument("--print-nav", action="store_true") |
| 77 | + return parser.parse_args(namespace=Args()) |
| 78 | + |
| 79 | + |
| 80 | +def main() -> None: |
| 81 | + args: Args = parse_args() |
| 82 | + nav = Nav() |
| 83 | + for path in args.src.rglob("*.py"): |
| 84 | + relative: Path = path.relative_to(args.src) |
| 85 | + module_path: Path = relative.with_suffix("") |
| 86 | + parts: tuple[str, ...] = tuple(module_path.parts) |
| 87 | + if not all(is_public(part) for part in parts): |
| 88 | + continue |
| 89 | + doc_path: Path = relative.with_suffix(".md") |
| 90 | + if parts[-1] == "__init__": |
| 91 | + parts: tuple[str, ...] = parts[:-1] |
| 92 | + doc_path: Path = doc_path.with_name("README.md") |
| 93 | + elif parts[-1] == "__main__": |
| 94 | + continue |
| 95 | + doc_path: Path = args.api_root / doc_path |
| 96 | + full_doc_path: Path = args.docs_dir / doc_path |
| 97 | + full_doc_path.parent.mkdir(parents=True, exist_ok=True) |
| 98 | + full_doc_path.write_text(MARKDOWN_TEMPLATE.format(module_path=".".join(parts))) |
| 99 | + nav.add(parts, doc_path) |
| 100 | + print(tomlkit.dumps(nav.dump())) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments