/ 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  src = Path(__file__).parent.parent / "src"
11  
12  for path in sorted(src.rglob("*.py")):
13      module_path = path.relative_to(src).with_suffix("")
14      doc_path = path.relative_to(src).with_suffix(".md")
15      full_doc_path = Path("reference", doc_path)
16  
17      parts = tuple(module_path.parts)
18  
19      if parts[-1] == "__init__":
20          parts = parts[:-1]
21          doc_path = doc_path.with_name("index.md")
22          full_doc_path = full_doc_path.with_name("index.md")
23      elif parts[-1].startswith("_"):
24          continue
25  
26      nav_parts = [f"{mod_symbol} {part}" for part in parts]
27      nav[tuple(nav_parts)] = doc_path.as_posix()
28  
29      with mkdocs_gen_files.open(full_doc_path, "w") as fd:
30          ident = ".".join(parts)
31          fd.write(f"::: {ident}")
32  
33      mkdocs_gen_files.set_edit_path(full_doc_path, ".." / path)
34  
35  with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
36      nav_file.writelines(nav.build_literate_nav())