|
| 1 | +# -----------------------------------------------------# |
| 2 | +# Library imports # |
| 3 | +# -----------------------------------------------------# |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import mkdocs_gen_files |
| 7 | + |
| 8 | +# -----------------------------------------------------# |
| 9 | +# Configuration # |
| 10 | +# -----------------------------------------------------# |
| 11 | +# Package source code relative path |
| 12 | +src_dir = "bootstrap_python_package" |
| 13 | +# Generated pages will be grouped in this nav folder |
| 14 | +nav_pages_path = "API-Reference" |
| 15 | + |
| 16 | + |
| 17 | +# -----------------------------------------------------# |
| 18 | +# Runner # |
| 19 | +# -----------------------------------------------------# |
| 20 | +""" Generate code reference pages and navigation |
| 21 | +
|
| 22 | + Based on the recipe of mkdocstrings: |
| 23 | + https://github.com/mkdocstrings/mkdocstrings |
| 24 | + https://github.com/mkdocstrings/mkdocstrings/issues/389#issuecomment-1100735216 |
| 25 | +
|
| 26 | + Credits: |
| 27 | + Timothée Mazzucotelli |
| 28 | + https://github.com/pawamoy |
| 29 | +""" |
| 30 | +# Iterate over each Python file |
| 31 | +for path in sorted(Path(src_dir).rglob("*.py")): |
| 32 | + # Get path in module, documentation and absolute |
| 33 | + module_path = path.relative_to(src_dir).with_suffix("") |
| 34 | + doc_path = path.relative_to(src_dir).with_suffix(".md") |
| 35 | + full_doc_path = Path(nav_pages_path, doc_path) |
| 36 | + |
| 37 | + # Handle edge cases |
| 38 | + parts = (src_dir,) + tuple(module_path.parts) |
| 39 | + if parts[-1] == "__init__": |
| 40 | + parts = parts[:-1] |
| 41 | + doc_path = doc_path.with_name("index.md") |
| 42 | + full_doc_path = full_doc_path.with_name("index.md") |
| 43 | + elif parts[-1] == "__main__": |
| 44 | + continue |
| 45 | + |
| 46 | + # Write docstring documentation to disk via parser |
| 47 | + with mkdocs_gen_files.open(full_doc_path, "w") as fd: |
| 48 | + ident = ".".join(parts) |
| 49 | + fd.write(f"::: {ident}") |
| 50 | + # Update parser |
| 51 | + mkdocs_gen_files.set_edit_path(full_doc_path, path) |
0 commit comments