#!/usr/bin/env python3 """Assemble 电子书小红书包 — 先同步微信同源 BOOK_DATA,再扁平打包。""" from __future__ import annotations import shutil import sys from pathlib import Path from subprocess import check_call ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "tool" OUT = ROOT / "release-book" def main() -> int: check_call([sys.executable, str(ROOT / "scripts" / "sync_data.py")]) if OUT.exists(): shutil.rmtree(OUT, ignore_errors=True) OUT.mkdir(parents=True) parts = [] for name in ("book/book-data.js", "book/markdown.js", "book/xhs-bridge.js", "book-app.js"): p = SRC / name if not p.is_file(): print(f"missing {name}", file=sys.stderr) return 1 text = p.read_text(encoding="utf-8") if name == "book-app.js": # 历史误把整份 BOOK_DATA 拼进 book-app;只保留运行时 marker = "(function ()" idx = text.find(marker) if idx < 0: idx = text.find("function getBook") if idx > 0 and "var BOOK_DATA" in text[:idx]: text = "/* book-app runtime */\n" + text[idx:] parts.append(text) html_src = SRC / "book.html" css_src = SRC / "book-styles.css" if not html_src.is_file() or not css_src.is_file(): print("missing book.html / book-styles.css", file=sys.stderr) return 1 shutil.copy2(html_src, OUT / "index.html") shutil.copy2(css_src, OUT / "styles.css") (OUT / "app.js").write_text( "/* bundled xhs ebook — data = weapp nomadCodeBook */\n" + "\n".join(parts) + "\n", encoding="utf-8", ) nested = [p for p in OUT.rglob("index.html") if p != OUT / "index.html"] if nested: print("FATAL nested index.html", nested, file=sys.stderr) return 1 print(f"assembled {OUT}") for p in sorted(OUT.iterdir()): if p.is_file(): print(f" {p.name}\t{p.stat().st_size}") return 0 if __name__ == "__main__": raise SystemExit(main())