1+ #!/usr/bin/env python3
2+ """Mirror docs/ and docs_dev/ into a unified virtual FS for MkDocs.
3+
4+ Virtual output structure:
5+ readme.md ← from docs_dev/readme_toplevel.md
6+ user/ ← from docs/
7+ dev/ ← from docs_dev/ (excluding utils/, requirements.txt, readme_toplevel.md)
8+ """
9+
10+ from __future__ import annotations
11+
12+ from pathlib import Path
13+
14+ DOCS_DEV_EXCLUDE = {
15+
16+ }
17+
18+
19+ def _repo_root_from_script (script_path : Path ) -> Path :
20+ """Infer repository root from script location."""
21+ return script_path .parents [2 ]
22+
23+
24+ def _mirror_into_virtual_fs (
25+ source_dir : Path ,
26+ virtual_prefix : str ,
27+ exclude : set [str ] | None = None ,
28+ ) -> None :
29+ """Read all files from source_dir and write them into the mkdocs virtual FS.
30+
31+ Args:
32+ source_dir: Real directory to mirror.
33+ virtual_prefix: Virtual FS prefix to write files under.
34+ exclude: Top-level names within source_dir to skip.
35+ """
36+ import mkdocs_gen_files
37+
38+ exclude = exclude or set ()
39+
40+ for source_file in source_dir .rglob ("*" ):
41+ if not source_file .is_file ():
42+ continue
43+
44+ relative = source_file .relative_to (source_dir )
45+
46+ if relative .parts [0 ] in exclude :
47+ continue
48+
49+ virtual_path = f"{ virtual_prefix } /{ relative .as_posix ()} "
50+ with mkdocs_gen_files .open (virtual_path , "wb" ) as fd :
51+ fd .write (source_file .read_bytes ())
52+
53+
54+ def _run_from_mkdocs_gen_files () -> None :
55+ """Run when this module is loaded by mkdocs-gen-files."""
56+ import mkdocs_gen_files
57+
58+ script_path = Path (__file__ ).resolve ()
59+ repo_root = _repo_root_from_script (script_path )
60+
61+ # 1. readme_toplevel.md → virtual readme.md
62+ readme_source = repo_root / "docs_dev" / "readme_toplevel.md"
63+ if readme_source .exists ():
64+ with mkdocs_gen_files .open ("README.md" , "wb" ) as fd :
65+ fd .write (readme_source .read_bytes ())
66+ else :
67+ print (f"Warning: '{ readme_source } ' does not exist, skipping." )
68+
69+ # 2. docs/ → virtual user/
70+ docs_dir = repo_root / "docs"
71+ if docs_dir .exists ():
72+ _mirror_into_virtual_fs (docs_dir , "user" )
73+ else :
74+ print (f"Warning: '{ docs_dir } ' does not exist, skipping." )
75+
76+ # 3. docs_dev/ → virtual dev/ (with exclusions)
77+ docs_dev_dir = repo_root / "docs_dev"
78+ if docs_dev_dir .exists ():
79+ _mirror_into_virtual_fs (docs_dev_dir , "dev" , exclude = DOCS_DEV_EXCLUDE )
80+ else :
81+ print (f"Warning: '{ docs_dev_dir } ' does not exist, skipping." )
82+
83+
84+ if __name__ != "__main__" :
85+ _run_from_mkdocs_gen_files ()
0 commit comments