Skip to content

Commit b1e2a31

Browse files
committed
docs: mkdocs前処理の自動同期とスクリプト説明を整理
1 parent 74cea67 commit b1e2a31

4 files changed

Lines changed: 55 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88

99
```shell
1010
$ uv sync
11-
$ uv run python scripts/sync_library_index.py --write
1211
$ uv run mkdocs serve
1312
```
1413

15-
`mkdocs.yml``nav` を変更していない場合は、2行目(`sync_library_index.py --write`)は省略可能です。
14+
### 開発用スクリプト
1615

17-
### ヘッダ結合(1ファイル化)について
18-
19-
- `mkdocs serve/build` の前に `include/**/*.hpp` から `bundled/` を自動生成します(`scripts/mkdocs_hooks.py`)。
20-
- 各ライブラリページの「Bundled (Copy & Paste)」はこの `bundled/` を参照します。
16+
スクリプトに関する説明は [`scripts/README.md`](scripts/README.md) に記載しています。
2117

2218
## ライブラリ使用方法
2319

scripts/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# scripts/
2+
3+
ドキュメント生成まわりの補助スクリプトです。
4+
5+
## `sync_library_index.py`
6+
7+
- 役割: `mkdocs.yml``nav` から `docsrc/library/index.md` を生成・同期します。
8+
- 通常運用: `mkdocs serve/build` 実行時に `scripts/mkdocs_hooks.py` から自動実行されるため、手動実行は不要です。
9+
- 手動チェック: `uv run python scripts/sync_library_index.py`
10+
- 手動更新: `uv run python scripts/sync_library_index.py --write`
11+
12+
## `bundle_header.py`
13+
14+
- 役割: `#include` を展開して、1ファイル化済みヘッダ(`bundled/`)を生成します。
15+
- 通常運用: `mkdocs serve/build` 実行時に `scripts/mkdocs_hooks.py` から自動実行されます。
16+
17+
## `mkdocs_hooks.py`
18+
19+
- 役割: MkDocs の `on_pre_build` フックで前処理を実行します。
20+
- 実行内容:
21+
- `sync_library_index.py``docsrc/library/index.md` を同期
22+
- `bundle_header.py``bundled/` を生成

scripts/mkdocs_hooks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BUNDLE_ROOT = ROOT / "bundled"
1010
VERIFY_ROOT = ROOT / "verify"
1111
BUNDLE_SCRIPT = ROOT / "scripts" / "bundle_header.py"
12+
SYNC_SCRIPT = ROOT / "scripts" / "sync_library_index.py"
1213

1314

1415
def _load_bundler():
@@ -20,7 +21,19 @@ def _load_bundler():
2021
return module.bundle_header
2122

2223

24+
def _load_library_index_syncer():
25+
spec = importlib.util.spec_from_file_location("sync_library_index", SYNC_SCRIPT)
26+
if spec is None or spec.loader is None:
27+
raise RuntimeError(f"failed to load: {SYNC_SCRIPT}")
28+
module = importlib.util.module_from_spec(spec)
29+
spec.loader.exec_module(module)
30+
return module.sync_library_index
31+
32+
2333
def on_pre_build(config):
34+
sync_library_index = _load_library_index_syncer()
35+
sync_library_index(write=True)
36+
2437
bundle_header = _load_bundler()
2538
for path in INCLUDE_ROOT.rglob("*.hpp"):
2639
rel = path.relative_to(INCLUDE_ROOT)

scripts/sync_library_index.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,33 @@ def render_library_index(groups: list[tuple[str, list[tuple[str, str]]]]) -> str
7575
return "\n".join(lines).rstrip() + "\n"
7676

7777

78+
def sync_library_index(*, write: bool) -> tuple[bool, str, str]:
79+
expected = render_library_index(load_library_nav_groups())
80+
current = LIBRARY_INDEX_MD.read_text(encoding="utf-8") if LIBRARY_INDEX_MD.exists() else ""
81+
changed = current != expected
82+
83+
if write and changed:
84+
LIBRARY_INDEX_MD.parent.mkdir(parents=True, exist_ok=True)
85+
LIBRARY_INDEX_MD.write_text(expected, encoding="utf-8")
86+
87+
return changed, current, expected
88+
89+
7890
def main() -> int:
7991
parser = argparse.ArgumentParser()
8092
parser.add_argument("--write", action="store_true", help="docsrc/library/index.md を更新する")
8193
args = parser.parse_args()
8294

83-
expected = render_library_index(load_library_nav_groups())
84-
current = LIBRARY_INDEX_MD.read_text(encoding="utf-8") if LIBRARY_INDEX_MD.exists() else ""
95+
changed, current, expected = sync_library_index(write=args.write)
8596

8697
if args.write:
87-
LIBRARY_INDEX_MD.parent.mkdir(parents=True, exist_ok=True)
88-
LIBRARY_INDEX_MD.write_text(expected, encoding="utf-8")
89-
print(f"updated: {LIBRARY_INDEX_MD}")
98+
if changed:
99+
print(f"updated: {LIBRARY_INDEX_MD}")
100+
else:
101+
print(f"already up-to-date: {LIBRARY_INDEX_MD}")
90102
return 0
91103

92-
if current == expected:
104+
if not changed:
93105
print("OK: docsrc/library/index.md is in sync with mkdocs.yml")
94106
return 0
95107

0 commit comments

Comments
 (0)