Files
cuotibiye/gen_idiom.py
T

123 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""把《小学成语卡》注入工作台:建 IDIOM 表 + 在解析「知识点」页显示释义。
跑法:python gen_idiom.py
它做三件事(都可重复执行,靠 <IDIOM> 标记做幂等替换):
1. 在 /* </KPT> */ 之后插入 `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 = "/* <IDIOM> */"
END = "/* </IDIOM> */"
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 `<div class=\"idm\"><div class=\"idm-h\">「${esc(idm.w)}」<span>${esc(idm.p)}</span>"
+ "<em>${esc(tone)}</em></div>"
+ "<div class=\"idm-d\">${esc(idm.d)}</div>"
+ "<div class=\"idm-e\">例:${esc(idm.e)}</div>"
+ "<div class=\"idm-t\">易错字:<b>${esc(idm.t)}</b></div></div>`;\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 = "/* </KPT> */"
assert anchor in html, "找不到 </KPT> 标记"
html = html.replace(anchor, anchor + "\n\n" + block(idioms), 1)
# 2) CSS(只插一次)
if ".idm-h" not in html:
html = html.replace("</style>", CSS + "</style>", 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 (
("知识点:<b>${esc(plan.know)}</b><br>${esc(plan.idea)}</p>`;",
"知识点:<b>${esc(plan.know)}</b><br>${esc(plan.idea)}</p>${idmHtml(idm)}`;"),
("是最稳的做法。\"}</p>`;", "是最稳的做法。\"}</p>${idmHtml(idm)}`;"),
("而不是把同一道背下来。</p>`;", "而不是把同一道背下来。</p>${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()