|
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 | from datetime import datetime |
| 4 | +from pathlib import Path |
| 5 | +import shutil |
4 | 6 |
|
5 | 7 | sys.path.insert(0, os.path.abspath("../..")) |
6 | 8 |
|
|
130 | 132 | html_css_files = ["css/custom_log.css"] |
131 | 133 |
|
132 | 134 |
|
| 135 | +def _sync_examples(app): |
| 136 | + """Sync top-level examples into language-specific doc trees. |
| 137 | +
|
| 138 | + Policy: |
| 139 | + - README.md -> English docs/en/_examples_synced/<example>/README.md |
| 140 | + - README_zh.md -> Chinese docs/zh/_examples_synced/<example>/README_zh.md |
| 141 | + - If a language-specific README missing, that example is simply skipped for that language. |
| 142 | + """ |
| 143 | + docs_root = Path(__file__).resolve().parent |
| 144 | + src_dir = docs_root.parent / "examples" |
| 145 | + if not src_dir.exists(): |
| 146 | + return |
| 147 | + |
| 148 | + lang_cfgs = { |
| 149 | + "en": { |
| 150 | + "dir": docs_root / "en", |
| 151 | + "readme_name": "README.md", |
| 152 | + "title": "External Examples (Auto Synced)", |
| 153 | + "note": "This section is auto-generated from repository examples/ by copying README.md of each example.", |
| 154 | + }, |
| 155 | + "zh": { |
| 156 | + "dir": docs_root / "zh", |
| 157 | + # primary preferred name; will fallback to README.md |
| 158 | + "readme_name": "README_zh.md", |
| 159 | + "title": "外部示例 (自动同步)", |
| 160 | + "note": "本节由构建时自动从仓库 examples/ 复制示例文档生成;优先使用 README_zh.md,若不存在则回退到 README.md。", |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + for lang, cfg in lang_cfgs.items(): |
| 165 | + lang_dir = cfg["dir"] |
| 166 | + if not lang_dir.exists(): |
| 167 | + continue |
| 168 | + out_dir = lang_dir / "_examples_synced" |
| 169 | + if out_dir.exists(): |
| 170 | + shutil.rmtree(out_dir) |
| 171 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 172 | + |
| 173 | + entries = [] # (example_name, readme_rel_path) |
| 174 | + for d in sorted(src_dir.iterdir()): |
| 175 | + if not d.is_dir(): |
| 176 | + continue |
| 177 | + # language-specific selection with fallback for zh |
| 178 | + if lang == "zh": |
| 179 | + primary = d / cfg["readme_name"] # README_zh.md |
| 180 | + fallback = d / "README.md" |
| 181 | + candidate = primary if primary.exists() else fallback |
| 182 | + else: |
| 183 | + candidate = d / cfg["readme_name"] |
| 184 | + if not candidate.exists(): |
| 185 | + continue # skip entirely if nothing suitable |
| 186 | + target_dir = out_dir / d.name |
| 187 | + target_dir.mkdir(parents=True, exist_ok=True) |
| 188 | + shutil.copy2(candidate, target_dir / "README.md") |
| 189 | + entries.append((d.name, f"_examples_synced/{d.name}/README.md")) |
| 190 | + |
| 191 | + |
133 | 192 | def setup(app): |
134 | 193 | app.add_css_file("css/custom_log.css") |
| 194 | + # ensure examples are synced before reading source files |
| 195 | + app.connect("builder-inited", _sync_examples) |
135 | 196 |
|
136 | 197 |
|
137 | 198 | myst_enable_extensions = [ |
|
0 commit comments