File size: 2,007 Bytes
e66cfb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
build.py
--------
Markdown → JSON 的 build pipeline。

職責:
    讀取 paths.json → 對每個任務呼叫 Test2ChMd._export_one → 輸出 JSON

與 app.py 完全解耦,可用兩種方式執行:

  A. 命令列(手動重建):
        python build.py                        # 使用預設 paths.json
        python build.py paths_ch15.json        # 指定 config
        python build.py paths.json --force     # 強制重建全部

  B. 由 app.py import 並在啟動時呼叫:
        from build import build_if_needed
        build_if_needed(CONFIG_PATH, force=False)  # 只重建有異動的任務
"""

from __future__ import annotations
import sys
from pathlib import Path

# Test2ChMd 放在同層或 layout/ 下,依實際位置選一種
from layout.converter.md_pipeline import build_from_config


def build_if_needed(
    config_path: str | Path = "paths.json",
    *,
    force: bool = False,
) -> list[Path]:
    """
    執行 build pipeline。

    config_path:paths.json 路徑(絕對或相對於 CWD)
    force:True → 強制重建全部任務;False → 只重建比 md 舊的 json
    """
    return build_from_config(config_path, force=force)


# ---------------------------------------------------------------------------
# 命令列介面
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(
        description="把 paths.json 中的 Markdown 轉換成 JSON"
    )
    parser.add_argument(
        "config",
        nargs="?",
        default="paths.json",
        help="paths.json 的路徑(預設:./paths.json)",
    )
    parser.add_argument(
        "--force",
        action="store_true",
        help="強制重建全部任務(忽略 up_to_date 設定)",
    )
    args = parser.parse_args()

    outputs = build_if_needed(args.config, force=args.force)
    print(f"\n完成:共輸出 {len(outputs)} 個 JSON 檔案。")