nomadro/apps/miniprogram/h5/scripts/extract-vip-ebooks.py
eric 98a54730ff Port nomadvip VIP into native WeChat pages and document the flow.
Add vip-course/lesson/ebook with source markdown seeds, VIP order API + admin offers, ban web-view for content, and update READMEs for weapp rebuild/extract scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-04 22:30:27 +08:00

113 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Pack nomadvip ebook.md + cloudphone.md into weapp vipEbooks.ts"""
from __future__ import annotations
import json
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[4]
SRC = ROOT / "src/modules/vip/app/data"
OUT = ROOT / "apps/miniprogram/h5/src/data/vipEbooks.ts"
def parse_headers(md: str) -> list[str]:
h1 = re.findall(r"(?m)^#\s+(.+)$", md)
if h1:
return [re.sub(r"#+\s*", "", x).strip() for x in h1]
return [x.strip() for x in re.findall(r"(?m)^##\s+(.+)$", md)]
def split_sections(md: str) -> list[str]:
h1s = list(re.finditer(r"(?m)^#\s+.+$", md))
if len(h1s) > 1:
parts: list[str] = []
for i, m in enumerate(h1s):
end = h1s[i + 1].start() if i + 1 < len(h1s) else len(md)
chunk = md[m.start() : end].strip()
if chunk:
parts.append(chunk)
return parts
h2s = list(re.finditer(r"(?m)^##\s+.+$", md))
if len(h2s) > 1:
parts = []
if h2s[0].start() > 0:
pre = md[: h2s[0].start()].strip()
if pre:
parts.append(pre)
for i, m in enumerate(h2s):
end = h2s[i + 1].start() if i + 1 < len(h2s) else len(md)
chunk = md[m.start() : end].strip()
if chunk:
parts.append(chunk)
return parts
return [md]
def main() -> None:
books = {
"nomad": {
"id": "nomad",
"title": "数字游民",
"free": True,
"estimatedMinutes": 45,
"file": "ebook.md",
},
"cloudphone": {
"id": "cloudphone",
"title": "云手机",
"free": False,
"estimatedMinutes": 25,
"file": "cloudphone.md",
},
}
payload = {}
for key, meta in books.items():
md = (SRC / meta["file"]).read_text(encoding="utf-8")
payload[key] = {
"id": meta["id"],
"title": meta["title"],
"free": meta["free"],
"estimatedMinutes": meta["estimatedMinutes"],
"headers": parse_headers(md),
"sections": split_sections(md),
"markdown": md,
}
print(
key,
"chars",
len(md),
"headers",
len(payload[key]["headers"]),
"sections",
len(payload[key]["sections"]),
)
body = json.dumps(payload, ensure_ascii=False, indent=2)
OUT.write_text(
"/** Auto from src/modules/vip/app/data — nomadvip EbookViewer SoT */\n"
"export type VipEbookId = 'nomad' | 'cloudphone'\n"
"export type VipEbookPack = {\n"
" id: VipEbookId\n"
" title: string\n"
" free: boolean\n"
" estimatedMinutes: number\n"
" headers: string[]\n"
" sections: string[]\n"
" markdown: string\n"
"}\n"
f"export const VIP_EBOOKS: Record<VipEbookId, VipEbookPack> = {body}\n"
"export function resolveVipEbookId(raw?: string): VipEbookId {\n"
" const id = String(raw || 'nomad').trim()\n"
" if (id === 'cloudphone' || id === '2') return 'cloudphone'\n"
" return 'nomad'\n"
"}\n",
encoding="utf-8",
)
print("wrote", OUT, "bytes", OUT.stat().st_size)
if __name__ == "__main__":
main()