1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#!/usr/bin/env python3
"""
Generate the code reference pages.
Everything under /develop/api-references is actually generated by this script.
"""
import os
import shutil
from pathlib import Path
import mkdocs_gen_files
CODE_PATHS = ["PyFunceble"]
DOCPATH = "develop/api-references"
EXCLUDE_MODULES = ["PyFunceble.data"]
try:
# Ensure that we cleanup the directory before we start generating the files.
shutil.rmtree(DOCPATH)
except FileNotFoundError:
shutil.rmtree(os.path.join("docs", DOCPATH), ignore_errors=True)
nav = mkdocs_gen_files.Nav()
for code_path in CODE_PATHS:
for path in sorted(Path(code_path).rglob("*.py")):
module_path = path.with_suffix("")
doc_path = path.relative_to(code_path).with_suffix(".md")
full_doc_path = Path(DOCPATH, doc_path)
parts = list(module_path.parts)
if parts[-1] == "__init__":
parts: list[str] = parts[:-1]
doc_path = doc_path.with_name("index.md")
full_doc_path = full_doc_path.with_name("index.md")
elif parts[-1] == "__main__":
continue
identifier = ".".join(parts)
if any(f"{x}." in identifier for x in EXCLUDE_MODULES):
if os.path.exists(full_doc_path.absolute()):
os.remove(full_doc_path.absolute())
continue
nav[parts] = doc_path.as_posix()
with mkdocs_gen_files.open(full_doc_path, "w") as file_stream:
print("::: " + identifier, file=file_stream)
mkdocs_gen_files.set_edit_path(full_doc_path, path)
with mkdocs_gen_files.open(Path(DOCPATH, "SUMMARY.md"), "w") as nav_file_stream:
nav_file_stream.writelines(nav.build_literate_nav())
|