| 1 | """Generate the code reference pages and navigation.""" |
| 2 | |
| 3 | import re |
| 4 | from pathlib import Path |
| 5 | |
| 6 | import mkdocs_gen_files |
| 7 | |
| 8 | nav = mkdocs_gen_files.Nav() |
| 9 | |
| 10 | EXCLUDE = [ |
| 11 | re.compile(pat) |
| 12 | for pat in [ |
| 13 | "style_maps", |
| 14 | "color_info", |
| 15 | ] |
| 16 | ] |
| 17 | |
| 18 | |
| 19 | for path in sorted(Path("pytermgui").rglob("*.py")): |
| 20 | module_path = path.relative_to(".").with_suffix("") |
| 21 | |
| 22 | if any(pat.match(path.parts[-1]) is not None for pat in EXCLUDE): |
| 23 | continue |
| 24 | |
| 25 | doc_path = path.relative_to(".").with_suffix(".md") |
| 26 | full_doc_path = Path("reference", doc_path) |
| 27 | |
| 28 | parts = tuple(module_path.parts) |
| 29 | |
| 30 | if parts[-1] == "__init__": |
| 31 | parts = parts[:-1] |
| 32 | doc_path = doc_path.with_name("index.md") |
| 33 | full_doc_path = full_doc_path.with_name("index.md") |
| 34 | elif parts[-1] == "__main__": |
| 35 | continue |
| 36 | |
| 37 | nav[parts] = doc_path.as_posix() |
| 38 | |
| 39 | with mkdocs_gen_files.open(full_doc_path, "w") as fd: |
| 40 | ident = ".".join(parts) |
| 41 | fd.write(f"::: {ident}") |
| 42 | |
| 43 | mkdocs_gen_files.set_edit_path(full_doc_path, path) |
| 44 | |
| 45 | with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file: |
| 46 | content = list(nav.build_literate_nav()) |
| 47 | nav_file.writelines(content) |