/ scripts / gen_api_ref.py
gen_api_ref.py
 1  # Generate the API reference pages and navigation.
 2  
 3  from pathlib import Path
 4  
 5  import mkdocs_gen_files
 6  
 7  nav = mkdocs_gen_files.Nav()
 8  mod_symbol = '<code class="doc-symbol doc-symbol-nav doc-symbol-module"></code>'
 9  
10  root = Path(__file__).parent.parent
11  src = root / "src"
12  
13  for path in sorted(src.rglob("*.py")):
14      module_path = path.relative_to(src).with_suffix("")
15      doc_path = path.relative_to(src).with_suffix(".md")
16      full_doc_path = Path("reference", doc_path)
17  
18      parts = tuple(module_path.parts)
19  
20      if parts[-1] == "__init__":
21          parts = parts[:-1]
22          doc_path = doc_path.with_name("index.md")
23          full_doc_path = full_doc_path.with_name("index.md")
24  
25      if any(part.startswith("_") for part in parts):
26          continue
27  
28      nav_parts = [f"{mod_symbol} {part}" for part in parts]
29      nav[tuple(nav_parts)] = doc_path.as_posix()
30  
31      with mkdocs_gen_files.open(full_doc_path, "w") as fd:
32          ident = ".".join(parts)
33          fd.write(f"---\ntitle: {ident}\n---\n\n::: {ident}")
34  
35      mkdocs_gen_files.set_edit_path(full_doc_path, ".." / path.relative_to(root))
36  
37  with mkdocs_gen_files.open("reference/SUMMARY.txt", "w") as nav_file:
38      nav_file.writelines(nav.build_literate_nav())