Files
MechanicalNotes/重写规范/tools/verify_chapter.py
T

91 lines
3.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""Verify a rewritten chapter folder (机械原理 在学练习 pattern).
Checks (per chapter folder passed as argv[1]):
1. required files exist and are non-empty: README.md, 01-知识点盘点.md,
01b-覆盖对照与接口.md, 10-速查卡.md, plus >=4 点 files 02-xx..09-xx
2. no line contains "$$" mixed with other content on the same line
(block math must have $$ alone on its own line; inline math uses single $)
3. every 点 file carries at least one 考点频率 marker (★★★/★★☆/★☆☆/⭐)
4. every 点 file carries a 出处 marker (出处:)
5. Obsidian double links [[ ]] appear in README / 01 / 10 and point files
6. 01 file links to 点 files and 10; 点 files mention [[01-知识点盘点]] or [[10-速查卡]]
7. no 旧版-snapshot folder got new files (skip; informational)
Usage: python verify_chapter.py <chapter_dir>
Exit code 0 = all pass; 1 = issues found (printed).
"""
import re
import sys
from pathlib import Path
REQ = ["README.md", "01-知识点盘点.md", "01b-覆盖对照与接口.md", "10-速查卡.md"]
FREQ = re.compile(r"(★★★|★★☆|★☆☆|⭐|必考|大题|选择|简答|了解|不考)")
SRC = re.compile(r"出处[::]")
def main():
ch = Path(sys.argv[1])
if not ch.is_dir():
print("NOT A DIR:", ch)
sys.exit(1)
issues = []
mds = sorted(ch.glob("*.md"))
names = {p.name for p in mds}
# 1. required files
for r in REQ:
if r not in names:
issues.append("缺少必需文件: " + r)
points = [p for p in mds if re.match(r"^0[2-9]-点\d+-", p.name)]
if len(points) < 4:
issues.append("点文件不足 4 个: %d" % len(points))
# per-file checks
for p in mds:
text = p.read_text(encoding="utf-8")
if len(text.strip()) == 0:
issues.append("空文件: " + p.name)
continue
# 2. $$ must stand alone on its line (strip inline-code spans first,
# so prose like "use `$$` as block math" is not flagged)
for ln, line in enumerate(text.splitlines(), 1):
line_nocode = re.sub(r"`[^`]*`", "", line)
if "$$" in line_nocode:
stripped = line_nocode.strip()
if stripped != "$$":
issues.append("%s:%d 同行块公式: %s" % (p.name, ln, stripped[:40]))
# 3/4. 点 files need frequency + source
if re.match(r"^0[2-9]-点\d+-", p.name):
if not FREQ.search(text):
issues.append("%s 缺考点频率标注" % p.name)
if not SRC.search(text):
issues.append("%s 缺出处标注" % p.name)
if "【图:" not in text and "图" not in text[:2000]:
pass # 配图占位非强制每点
# 5/6. links
if p.name in ("README.md", "01-知识点盘点.md", "10-速查卡.md", "01b-覆盖对照与接口.md"):
if "[[" not in text:
issues.append("%s 无双链" % p.name)
# 01 must link to each 点 file basename
if "01-知识点盘点.md" in names:
t01 = (ch / "01-知识点盘点.md").read_text(encoding="utf-8")
for pt in points:
if pt.stem not in t01:
issues.append("01 未链接点文件: " + pt.name)
# README must list 点 files
if "README.md" in names:
tr = (ch / "README.md").read_text(encoding="utf-8")
for pt in points:
if pt.stem not in tr:
issues.append("README 未列出点文件: " + pt.name)
if issues:
print("ISSUES (%d):" % len(issues))
for i in issues:
print(" -", i)
sys.exit(1)
print("OK: %s | %d md files, %d 点 files" % (ch.name, len(mds), len(points)))
sys.exit(0)
if __name__ == "__main__":
main()