openai/chatkit-python
Publicmirrored from https://github.com/openai/chatkit-pythonAvailable
docs/gen_ref_pages.py
43lines · modeblame
f688d870victor-openai8 months ago | 1 | from pathlib import Path |
| 2 | | |
| 3 | import mkdocs_gen_files | |
| 4 | | |
| 5 | SRC_ROOT = Path("chatkit") | |
| 6 | URL_ROOT = Path("api") / "chatkit" | |
| 7 | DOCS_ROOT = Path("docs") | |
| 8 | MANUAL_DOCS_ROOT = DOCS_ROOT / URL_ROOT | |
| 9 | | |
| 10 | if MANUAL_DOCS_ROOT.exists(): | |
| 11 | MANUAL_DOCS = { | |
| 12 | path.relative_to(DOCS_ROOT) for path in MANUAL_DOCS_ROOT.rglob("*.md") | |
| 13 | } | |
| 14 | else: | |
| 15 | MANUAL_DOCS = set() | |
| 16 | | |
| 17 | # Root index page for the package | |
| 18 | root_doc = URL_ROOT / "index.md" | |
| 19 | if root_doc not in MANUAL_DOCS: | |
| 20 | with mkdocs_gen_files.open(root_doc, "w") as f: | |
| 21 | f.write("# chatkit\n\n") | |
| 22 | f.write("::: chatkit\n") | |
| 23 | else: | |
| 24 | mkdocs_gen_files.set_edit_path(root_doc, DOCS_ROOT / root_doc) | |
| 25 | | |
| 26 | for path in SRC_ROOT.rglob("*.py"): | |
| 27 | if path.name.startswith("_"): | |
| 28 | continue | |
| 29 | if path.name in {"version.py", "logger.py"}: | |
| 30 | continue | |
| 31 | | |
| 32 | module_path = path.with_suffix("").relative_to(SRC_ROOT) | |
| 33 | identifier = ".".join(("chatkit", *module_path.parts)) | |
| 34 | doc_path = (URL_ROOT / module_path).with_suffix(".md") | |
| 35 | | |
| 36 | if doc_path in MANUAL_DOCS: | |
| 37 | mkdocs_gen_files.set_edit_path(doc_path, DOCS_ROOT / doc_path) | |
| 38 | continue | |
| 39 | | |
| 40 | mkdocs_gen_files.set_edit_path(doc_path, path) | |
| 41 | with mkdocs_gen_files.open(doc_path, "w") as f: | |
| 42 | f.write(f"# {path.stem}\n\n") | |
| 43 | f.write(f"::: {identifier}\n") |