|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | + |
| 6 | +ROOT = Path(__file__).resolve().parents[1] |
| 7 | +PACKAGE_DIR = ROOT / "zzupy" |
| 8 | +OUTPUT = ROOT / "docs" / "reference" / "api.md" |
| 9 | + |
| 10 | + |
| 11 | +def iter_modules() -> list[str]: |
| 12 | + modules: list[str] = [] |
| 13 | + |
| 14 | + for path in sorted(PACKAGE_DIR.rglob("*.py")): |
| 15 | + relative = path.relative_to(ROOT).with_suffix("") |
| 16 | + parts = list(relative.parts) |
| 17 | + |
| 18 | + if parts[-1] == "__main__": |
| 19 | + continue |
| 20 | + if parts[-1] == "__init__": |
| 21 | + parts = parts[:-1] |
| 22 | + |
| 23 | + if not parts: |
| 24 | + continue |
| 25 | + |
| 26 | + modules.append(".".join(parts)) |
| 27 | + |
| 28 | + return modules |
| 29 | + |
| 30 | + |
| 31 | +def heading_level(module: str) -> str: |
| 32 | + depth = module.count(".") |
| 33 | + return "#" * min(depth + 2, 6) |
| 34 | + |
| 35 | + |
| 36 | +def build_content(modules: list[str]) -> str: |
| 37 | + lines = [ |
| 38 | + "---", |
| 39 | + "title: API", |
| 40 | + "---", |
| 41 | + "", |
| 42 | + "<!-- 由 scripts/generate_api_reference.py 自动生成,请勿手动维护模块列表。 -->", |
| 43 | + "", |
| 44 | + "此页由 `Zensical` 配合 `mkdocstrings` 根据源码自动生成,适合在以下场景使用:", |
| 45 | + "", |
| 46 | + "- 查看公开模块和导出符号", |
| 47 | + "- 确认方法签名、参数名和返回值", |
| 48 | + "- 查询 Pydantic 模型字段", |
| 49 | + "", |
| 50 | + "如果你更关注接入步骤和调用示例,优先阅读 `Usage`。", |
| 51 | + "", |
| 52 | + ] |
| 53 | + |
| 54 | + for module in modules: |
| 55 | + lines.extend( |
| 56 | + [ |
| 57 | + f"{heading_level(module)} `{module}`", |
| 58 | + "", |
| 59 | + f"::: {module}", |
| 60 | + "", |
| 61 | + ] |
| 62 | + ) |
| 63 | + |
| 64 | + return "\n".join(lines) |
| 65 | + |
| 66 | + |
| 67 | +def main() -> None: |
| 68 | + modules = iter_modules() |
| 69 | + OUTPUT.write_text(build_content(modules), encoding="utf-8") |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments