/ scripts / gen_ref_nav.py
gen_ref_nav.py
 1  """Generate the code 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      elif parts[-1].startswith("_"):
25          continue
26  
27      nav_parts = [f"{mod_symbol} {part}" for part in parts]
28      nav[tuple(nav_parts)] = doc_path.as_posix()
29  
30      with mkdocs_gen_files.open(full_doc_path, "w") as fd:
31          ident = ".".join(parts)
32          fd.write(f"---\ntitle: {ident}\n---\n\n::: {ident}")
33  
34      mkdocs_gen_files.set_edit_path(full_doc_path, ".." / path.relative_to(root))
35  
36  with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
37      nav_file.writelines(nav.build_literate_nav())