#!/usr/bin/env python3
"""Assemble xhs/release — 单一门户包(首页+导航+小册+社群+VIP),对齐微信小程序。"""
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"
def main() -> int:
# 数据由 build:xhs 链路预先写入(export → pack-hackrobot-book);此处不再 sync 覆盖
if not (SRC / "portal-data.js").is_file():
check_call([sys.executable, str(ROOT / "scripts" / "sync_data.py")])
if OUT.exists():
shutil.rmtree(OUT, ignore_errors=True)
OUT.mkdir(parents=True)
# 微信同源样式:hub 首页 + nav + hero 封面 + VIP landing + 公众号弹窗
css_parts = []
for name in (
"hub.css",
"styles.css",
"community-navbar.css",
"weapp-hero.css",
"reader-md.css",
"ebook-book.css",
"vip-landing.css",
"woa-modal.css",
"vip-offline.css",
):
p = SRC / name
if p.is_file():
css_parts.append(f"/* ===== {name} ===== */\n" + p.read_text(encoding="utf-8"))
if not css_parts:
print("FATAL missing styles", file=sys.stderr)
return 1
data_js = (SRC / "portal-data.js").read_text(encoding="utf-8")
# 兜底:微信分包图路径 → 离线 ./images/(export/pack 漏改时不白屏)
data_js = data_js.replace("/packageBook/assets/book-images/", "./images/").replace(
"/assets/book-images/", "./images/"
)
md_js = (SRC / "book" / "markdown.js").read_text(encoding="utf-8")
app_js = (SRC / "app.js").read_text(encoding="utf-8")
bundle = (
"/* nomadro xhs portal — same modules as weapp: home/nav/book/community/vip */\n"
+ data_js
+ "\n"
+ md_js
+ "\n"
+ app_js
+ "\n"
)
html = (SRC / "index.html").read_text(encoding="utf-8")
# release 只保留打包后的 styles.css + app.js(tool 可多文件预览)
import re
html = re.sub(
r'(?is)]*>\s*',
"",
html,
count=0,
)
html = html.replace(
"",
' \n',
1,
)
html = re.sub(r'(?is)\s*', "", html)
html = html.replace(
"