"""把《小学成语卡》注入工作台:建 IDIOM 表 + 在解析「知识点」页显示释义。
跑法:python gen_idiom.py
它做三件事(都可重复执行,靠 标记做幂等替换):
1. 在 /* */ 之后插入 `const IDIOM = {...}` 与 `idiomIn()` / `idmHtml()`;
2. 在 buildAnalysis 开头补 `const idm = idiomIn(q.stem);`;
3. 给三处 tab1 赋值末尾追加 `+ idmHtml(idm)`。
为什么内联而不是外链:工作台要能 file:// 双击打开,外链会 404(品牌标踩过一次)。
"""
import json
from pathlib import Path
from openpyxl import load_workbook
HERE = Path(__file__).resolve().parent
HTML = HERE / "prototype" / "workbench" / "index.html"
XLSX = HERE / "report" / "错题毕业-小学-成语卡.xlsx"
BEGIN = "/* */"
END = "/* */"
def read_idioms():
wb = load_workbook(XLSX, read_only=True)
ws = wb["成语卡"]
rows = list(ws.iter_rows(min_row=2, values_only=True))
out = {}
for r in rows:
if not r or not r[1]:
continue
# 序号, 成语, 拼音, 年级, 释义, 色彩, 易错字, 例句, 来源, 知识点
out[r[1]] = {"p": r[2], "g": r[3], "d": r[4], "c": r[5], "t": r[6], "e": r[7]}
return out
def block(idioms):
payload = json.dumps(idioms, ensure_ascii=False, separators=(",", ":"))
return (
BEGIN
+ "\n// 小学高频成语卡(120 条),由 gen_idiom.py 从 xlsx 生成。改表 → 重跑脚本,不要手改这里。\n"
+ f"const IDIOM = {payload};\n\n"
+ "/* 题干里出现的第一个表内成语;没有就返回 null */\n"
+ "function idiomIn(stem) {\n"
+ " const text = String(stem || \"\");\n"
+ " let best = null;\n"
+ " for (const w of Object.keys(IDIOM)) {\n"
+ " if (text.includes(w) && (!best || w.length > best.length)) best = w;\n"
+ " }\n"
+ " return best ? { w: best, ...IDIOM[best] } : null;\n"
+ "}\n\n"
+ "/* 知识点页里的「这个成语是什么意思」一块 */\n"
+ "function idmHtml(idm) {\n"
+ " if (!idm) return \"\";\n"
+ " const tone = idm.c === \"贬\" ? \"贬义\" : idm.c === \"褒\" ? \"褒义\" : \"中性\";\n"
+ " return `「${esc(idm.w)}」${esc(idm.p)}"
+ "${esc(tone)}
"
+ "
${esc(idm.d)}
"
+ "
例:${esc(idm.e)}
"
+ "
易错字:${esc(idm.t)}
`;\n"
+ "}\n"
+ END
)
CSS = """
/* 成语卡:解析「知识点」页里的释义块(gen_idiom.py 注入) */
.idm { margin-top: 10px; padding: 11px 13px; background: #FDF7EC; border: 1px solid #EEDFC6; border-radius: 10px; }
.idm-h { font-size: 14px; font-weight: 700; color: #24211F; }
.idm-h span { font-weight: 400; font-size: 11.5px; color: #8A7B66; margin-left: 6px; letter-spacing: .3px; }
.idm-h em { font-style: normal; font-size: 10.5px; color: #A9761A; border: 1px solid #E7CE9B;
border-radius: 6px; padding: 1px 5px; margin-left: 6px; }
.idm-d { margin-top: 6px; font-size: 13px; line-height: 1.65; color: #3B342C; }
.idm-e { margin-top: 4px; font-size: 12px; color: #6E6252; }
.idm-t { margin-top: 4px; font-size: 11.5px; color: #8A6A3E; }
"""
def main():
html = HTML.read_text(encoding="utf-8")
idioms = read_idioms()
# 1) 替换/插入 IDIOM 段
if BEGIN in html:
head = html[: html.index(BEGIN)]
tail = html[html.index(END) + len(END):]
html = head + block(idioms) + tail
else:
anchor = "/* */"
assert anchor in html, "找不到 标记"
html = html.replace(anchor, anchor + "\n\n" + block(idioms), 1)
# 2) CSS(只插一次)
if ".idm-h" not in html:
html = html.replace("", CSS + "", 1)
# 3) buildAnalysis 里取一次 idm
if "const idm = idiomIn(q.stem);" not in html:
old = ' const ans = q.answer == null ? "" : String(q.answer);\n const isChoice ='
new = ' const idm = idiomIn(q.stem);\n const ans = q.answer == null ? "" : String(q.answer);\n const isChoice ='
assert old in html, "buildAnalysis 开头的 ans 赋值没找到"
html = html.replace(old, new, 1)
# 4) 三处 tab1 赋值追加 idmHtml(idm)
patched = 0
for old_tail, new_tail in (
("知识点:${esc(plan.know)}
${esc(plan.idea)}
`;",
"知识点:${esc(plan.know)}
${esc(plan.idea)}${idmHtml(idm)}`;"),
("是最稳的做法。\"}`;", "是最稳的做法。\"}${idmHtml(idm)}`;"),
("而不是把同一道背下来。`;", "而不是把同一道背下来。${idmHtml(idm)}`;"),
):
if old_tail in html and "idmHtml(idm)`;" not in html.split(old_tail)[0][-60:]:
html = html.replace(old_tail, new_tail, 1)
patched += 1
HTML.write_text(html, encoding="utf-8", newline="\n")
print(f"已注入 {len(idioms)} 条成语,tab1 追加 {patched} 处,文件 {len(html)} 字节")
if __name__ == "__main__":
main()