|
5 | 5 |
|
6 | 6 | import mkdocs_gen_files
|
7 | 7 |
|
| 8 | +OUTPUT_ROOT = Path("reference") |
| 9 | +SUMMARY_FILE = Path("SUMMARY.md") |
| 10 | +INDEX_FILE = "index.md" |
| 11 | + |
| 12 | +SKIP_DIR = "graph_objects" |
| 13 | + |
| 14 | +CONTENT = """ |
| 15 | +# {title} |
| 16 | + |
| 17 | +::: {ident} |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + """Main driver.""" |
| 23 | + |
| 24 | + # Setup. |
| 25 | + temp_dir = _get_temp_dir() |
| 26 | + nav = mkdocs_gen_files.Nav() |
| 27 | + |
| 28 | + # Process each Python file. |
| 29 | + for path in sorted(Path("plotly").rglob("*.py")): |
| 30 | + if _skip_file(path): |
| 31 | + continue |
| 32 | + |
| 33 | + # Paths. |
| 34 | + module_path = path.relative_to(".").with_suffix("") |
| 35 | + doc_path = path.relative_to(".").with_suffix(".md") |
| 36 | + full_doc_path = OUTPUT_ROOT / doc_path |
| 37 | + |
| 38 | + # Dunder special cases. |
| 39 | + parts = tuple(module_path.parts) |
| 40 | + if parts[-1] == "__main__": |
| 41 | + continue |
| 42 | + if parts[-1] == "__init__": |
| 43 | + parts = parts[:-1] |
| 44 | + doc_path = doc_path.with_name(INDEX_FILE) |
| 45 | + full_doc_path = full_doc_path.with_name(INDEX_FILE) |
| 46 | + |
| 47 | + # Save constructed data. |
| 48 | + nav[parts] = doc_path.as_posix() |
| 49 | + mkdocs_gen_files.set_edit_path(full_doc_path, path) |
| 50 | + content = _make_content(parts) |
| 51 | + _save_in_memory(full_doc_path, content) |
| 52 | + _save_in_temp_dir(temp_dir, doc_path, content) |
| 53 | + |
| 54 | + # Generate navigation summary. |
| 55 | + _generate_nav_summary(temp_dir, nav) |
| 56 | + |
| 57 | + |
| 58 | +def _generate_nav_summary(temp_dir, nav): |
| 59 | + """Create navigation summary (saving if requested).""" |
| 60 | + lines = nav.build_literate_nav() |
| 61 | + |
| 62 | + with mkdocs_gen_files.open(OUTPUT_ROOT / SUMMARY_FILE, "w") as writer: |
| 63 | + writer.writelines(lines) |
8 | 64 |
|
9 | | -# Saving Markdown files? |
10 | | -temp_dir = os.getenv("MKDOCS_TEMP_DIR", None) |
11 | | -if temp_dir is not None: |
12 | | - temp_dir = Path(temp_dir) |
13 | | - |
14 | | -# Set up the generation engine. |
15 | | -nav = mkdocs_gen_files.Nav() |
16 | | - |
17 | | -# Match each Python file. |
18 | | -for path in sorted(Path("plotly").rglob("*.py")): |
19 | | - # Documentation path. |
20 | | - module_path = path.relative_to(".").with_suffix("") |
21 | | - doc_path = path.relative_to(".").with_suffix(".md") |
22 | | - full_doc_path = Path("reference", doc_path) |
23 | | - |
24 | | - # Handle dunder special cases. |
25 | | - parts = tuple(module_path.parts) |
26 | | - if parts[-1] == "__init__": |
27 | | - parts = parts[:-1] |
28 | | - doc_path = doc_path.with_name("index.md") |
29 | | - full_doc_path = full_doc_path.with_name("index.md") |
30 | | - elif parts[-1] == "__main__": |
31 | | - continue |
32 | | - |
33 | | - # Save constructed data. |
34 | | - nav[parts] = doc_path.as_posix() |
35 | | - mkdocs_gen_files.set_edit_path(full_doc_path, path) |
36 | | - |
37 | | - # Save in-memory file. |
38 | | - with mkdocs_gen_files.open(full_doc_path, "w") as writer: |
39 | | - ident = ".".join(parts) |
40 | | - writer.write(f"# {ident}\n\n") |
41 | | - writer.write(f"::: {ident}") |
42 | | - |
43 | | - # Save to disk if requested. |
44 | 65 | if temp_dir is not None:
|
45 | | - temp_path = temp_dir / doc_path |
46 | | - temp_path.parent.mkdir(exist_ok=True, parents=True) |
47 | | - with open(temp_path, "w") as writer: |
48 | | - ident = ".".join(parts) |
49 | | - writer.write(f"# {ident}\n\n") |
50 | | - writer.write(f"::: {ident}") |
51 | | - |
52 | | -# Generate navigation summary. |
53 | | -with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as writer: |
54 | | - writer.writelines(nav.build_literate_nav()) |
55 | | -if temp_dir is not None: |
56 | | - temp_path = temp_dir / "SUMMARY.md" |
| 66 | + with open(temp_dir / SUMMARY_FILE, "w") as writer: |
| 67 | + writer.writelines(lines) |
| 68 | + |
| 69 | + |
| 70 | +def _get_temp_dir(): |
| 71 | + """Get temporary directory for on-disk files if requested.""" |
| 72 | + temp_dir = os.getenv("MKDOCS_TEMP_DIR", None) |
| 73 | + if temp_dir is not None: |
| 74 | + temp_dir = Path(temp_dir) |
| 75 | + return temp_dir |
| 76 | + |
| 77 | + |
| 78 | +def _make_content(parts): |
| 79 | + """Generate text to put in files.""" |
| 80 | + ident = parts |
| 81 | + if (len(parts) == 3) and (parts[1] == SKIP_DIR) and (parts[2] != SKIP_DIR): |
| 82 | + ident = [*parts[:-1], f"_{parts[2]}"] |
| 83 | + return CONTENT.format(title=".".join(parts), ident=".".join(ident)) |
| 84 | + |
| 85 | + |
| 86 | +def _save_in_memory(output_path, content): |
| 87 | + """Save in-memory file.""" |
| 88 | + with mkdocs_gen_files.open(output_path, "w") as writer: |
| 89 | + writer.write(content) |
| 90 | + |
| 91 | + |
| 92 | +def _save_in_temp_dir(temp_dir, doc_path, content): |
| 93 | + """Save on-disk file.""" |
| 94 | + if temp_dir is None: |
| 95 | + return |
| 96 | + |
| 97 | + temp_path = temp_dir / doc_path |
| 98 | + temp_path.parent.mkdir(exist_ok=True, parents=True) |
57 | 99 | with open(temp_path, "w") as writer:
|
58 | | - writer.writelines(nav.build_literate_nav()) |
| 100 | + writer.write(content) |
| 101 | + |
| 102 | + |
| 103 | +def _skip_file(path): |
| 104 | + """Don't include files like 'graph_objects/_bar.py'.""" |
| 105 | + return ( |
| 106 | + path.is_file() and (path.parent.name == SKIP_DIR) and str(path.name).startswith("_") |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +# Do NOT protect this with `if __name__ == "__main__"` because this is |
| 111 | +# run as a plugin by MkDocs rather than as a top-level script. |
| 112 | +main() |
0 commit comments