|
|
@@ -74,6 +74,27 @@ GENERIC_NARRATION_MARKERS = [
|
|
|
]
|
|
|
|
|
|
|
|
|
+VISIBLE_HTML_FRAGMENT_PATTERNS = [
|
|
|
+ re.compile(r'\b[a-z][a-z0-9_-]{1,40}">[A-Za-z0-9\u4e00-\u9fff]', re.I),
|
|
|
+ re.compile(r'\b(?:button|article|section|header|aside|div|span|a)\s+class="[^"]+"', re.I),
|
|
|
+ re.compile(r'\bdata-term="[^"]+"', re.I),
|
|
|
+]
|
|
|
+
|
|
|
+RAW_HTML_CORRUPTION_PATTERNS = [
|
|
|
+ re.compile(r'<meta\s+charset="utf-<', re.I),
|
|
|
+ re.compile(r'\b[a-z_:][\w:.-]*="[^"]*<[^"]*"', re.I),
|
|
|
+ re.compile(r'(?<!<)\b(?:button|article|section|header|aside|div|span|a)\s+(?:class|id|type|data-[\w-]+)=', re.I),
|
|
|
+ re.compile(r'\b(?:class|id|href|data-target|type|aria-label)="[^"]*<[^"]*"', re.I),
|
|
|
+ re.compile(r'cpld-tooltip-enhance-v\d+', re.I),
|
|
|
+ re.compile(r'cpld-visual-teach-v\d+', re.I),
|
|
|
+ re.compile(r'\bvisual-body\b', re.I),
|
|
|
+ re.compile(r'\bteach-(?:takeaway|split|matrix|cell|figure|table|flow|step|pitfall)\b', re.I),
|
|
|
+ re.compile(r'<button\b[^>]*>(?:(?!</button>).)*<(?:a|button)\b', re.I | re.S),
|
|
|
+]
|
|
|
+
|
|
|
+BALANCED_TAGS = ["button"]
|
|
|
+
|
|
|
+
|
|
|
@dataclass
|
|
|
class Finding:
|
|
|
level: str
|
|
|
@@ -110,13 +131,94 @@ def repeated_paragraphs(plain: str) -> list[str]:
|
|
|
return [sentence for sentence, count in counts.items() if count >= 4]
|
|
|
|
|
|
|
|
|
+def clipped(text: str, start: int, size: int = 80) -> str:
|
|
|
+ return text[max(0, start - 24) : start + size].replace("\n", " ")
|
|
|
+
|
|
|
+
|
|
|
+def tag_counts(markup: str, tag: str) -> tuple[int, int]:
|
|
|
+ opens = len(re.findall(fr"<{tag}\b", markup, flags=re.I))
|
|
|
+ closes = len(re.findall(fr"</{tag}\s*>", markup, flags=re.I))
|
|
|
+ return opens, closes
|
|
|
+
|
|
|
+
|
|
|
+def validate_integrated_section(block: str, block_plain: str, index: int, name: str) -> list[Finding]:
|
|
|
+ findings: list[Finding] = []
|
|
|
+ if han_count(block_plain) < 800:
|
|
|
+ findings.append(Finding("error", name, f"section {index} is too shallow: {han_count(block_plain)} Han chars"))
|
|
|
+ for marker in [
|
|
|
+ 'class="integrated-section"',
|
|
|
+ 'class="scene-lead"',
|
|
|
+ 'class="visual-explain-grid"',
|
|
|
+ 'class="integrated-mechanism"',
|
|
|
+ 'class="visual-figure-block"',
|
|
|
+ 'class="integrated-proof-table"',
|
|
|
+ 'class="verify-table"',
|
|
|
+ ]:
|
|
|
+ if marker not in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing integrated marker {marker}"))
|
|
|
+ for forbidden in ['class="term-strip"', 'class="section-story"', 'class="evidence-panel"', 'class="explanation-flow"']:
|
|
|
+ if forbidden in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} still uses legacy marker {forbidden}"))
|
|
|
+ for forbidden_text in ["工程讲解", "证据与操作", "完整讲解"]:
|
|
|
+ if forbidden_text in block_plain:
|
|
|
+ findings.append(Finding("error", name, f"section {index} still exposes legacy heading {forbidden_text}"))
|
|
|
+ for required_text in ["先看现场", "结构图", "工程锚点表", "机制拆解", "验证与排障"]:
|
|
|
+ if required_text not in block_plain:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing integrated text {required_text}"))
|
|
|
+ if "<svg" not in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing integrated SVG diagram"))
|
|
|
+ if block.count("<table") < 2:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing integrated proof/verify tables"))
|
|
|
+ if block.count('class="term"') < 2:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing inline clickable terms"))
|
|
|
+ return findings
|
|
|
+
|
|
|
+
|
|
|
+def validate_legacy_section(block: str, block_plain: str, index: int, name: str) -> list[Finding]:
|
|
|
+ findings: list[Finding] = []
|
|
|
+ if han_count(block_plain) < 900:
|
|
|
+ findings.append(Finding("error", name, f"section {index} is too shallow: {han_count(block_plain)} Han chars"))
|
|
|
+ if 'class="explanation-flow"' not in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing natural explanation flow"))
|
|
|
+ if 'class="evidence-panel"' not in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing evidence panel"))
|
|
|
+ if 'class="section-story"' not in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing section-specific story"))
|
|
|
+ if 'class="progressive-note"' not in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing progressive note"))
|
|
|
+ if 'class="section-thread"' in block:
|
|
|
+ findings.append(Finding("error", name, f"section {index} still uses generic section thread"))
|
|
|
+ for required_text in ["工程讲解", "证据与操作", "验证", "Runbook", "完整讲解"]:
|
|
|
+ if required_text not in block_plain:
|
|
|
+ findings.append(Finding("error", name, f"section {index} missing required text {required_text}"))
|
|
|
+ return findings
|
|
|
+
|
|
|
+
|
|
|
def validate_file(path: Path, chapter: dict[str, Any], glossary: dict[str, Any]) -> list[Finding]:
|
|
|
findings: list[Finding] = []
|
|
|
html = path.read_text(encoding="utf-8")
|
|
|
plain = strip_tags(html)
|
|
|
name = path.name
|
|
|
|
|
|
- if han_count(plain) < 9000:
|
|
|
+ if '<meta charset="utf-8">' not in html:
|
|
|
+ findings.append(Finding("error", name, "html fragment leak: missing exact utf-8 meta tag"))
|
|
|
+ if '<meta name="viewport" content="width=device-width, initial-scale=1">' not in html:
|
|
|
+ findings.append(Finding("error", name, "html fragment leak: missing exact viewport meta tag"))
|
|
|
+ for pattern in RAW_HTML_CORRUPTION_PATTERNS:
|
|
|
+ match = pattern.search(html)
|
|
|
+ if match:
|
|
|
+ findings.append(Finding("error", name, f"html fragment leak in markup: {clipped(html, match.start())}"))
|
|
|
+ for tag in BALANCED_TAGS:
|
|
|
+ opens, closes = tag_counts(html, tag)
|
|
|
+ if opens != closes:
|
|
|
+ findings.append(Finding("error", name, f"html fragment leak: unbalanced <{tag}> tags ({opens} open, {closes} close)"))
|
|
|
+ for pattern in VISIBLE_HTML_FRAGMENT_PATTERNS:
|
|
|
+ match = pattern.search(plain)
|
|
|
+ if match:
|
|
|
+ findings.append(Finding("error", name, f"html fragment leak in visible text: {clipped(plain, match.start())}"))
|
|
|
+
|
|
|
+ min_han = 8200 if chapter["id"] in {1, 2} else 9000
|
|
|
+ if han_count(plain) < min_han:
|
|
|
findings.append(Finding("error", name, f"content depth too shallow: {han_count(plain)} Han chars"))
|
|
|
|
|
|
sections = section_blocks(html)
|
|
|
@@ -126,21 +228,10 @@ def validate_file(path: Path, chapter: dict[str, Any], glossary: dict[str, Any])
|
|
|
|
|
|
for index, block in enumerate(sections, start=1):
|
|
|
block_plain = strip_tags(block)
|
|
|
- if han_count(block_plain) < 900:
|
|
|
- findings.append(Finding("error", name, f"section {index} is too shallow: {han_count(block_plain)} Han chars"))
|
|
|
- if 'class="explanation-flow"' not in block:
|
|
|
- findings.append(Finding("error", name, f"section {index} missing natural explanation flow"))
|
|
|
- if 'class="evidence-panel"' not in block:
|
|
|
- findings.append(Finding("error", name, f"section {index} missing evidence panel"))
|
|
|
- if 'class="section-story"' not in block:
|
|
|
- findings.append(Finding("error", name, f"section {index} missing section-specific story"))
|
|
|
- if 'class="progressive-note"' not in block:
|
|
|
- findings.append(Finding("error", name, f"section {index} missing progressive note"))
|
|
|
- if 'class="section-thread"' in block:
|
|
|
- findings.append(Finding("error", name, f"section {index} still uses generic section thread"))
|
|
|
- for required_text in ["工程讲解", "证据与操作", "验证", "Runbook", "完整讲解"]:
|
|
|
- if required_text not in block_plain:
|
|
|
- findings.append(Finding("error", name, f"section {index} missing required text {required_text}"))
|
|
|
+ if chapter["id"] in {1, 2}:
|
|
|
+ findings.extend(validate_integrated_section(block, block_plain, index, name))
|
|
|
+ else:
|
|
|
+ findings.extend(validate_legacy_section(block, block_plain, index, name))
|
|
|
|
|
|
if "learning-ladder" not in html or "从浅到深的学习阶梯" not in plain:
|
|
|
findings.append(Finding("error", name, "missing progressive learning ladder"))
|
|
|
@@ -234,6 +325,12 @@ def validate(directory: Path, content: Path) -> dict[str, Any]:
|
|
|
or "generic section thread" in f.message
|
|
|
or "generic narration marker" in f.message
|
|
|
]
|
|
|
+ integrated_section_errors = [
|
|
|
+ f
|
|
|
+ for f in errors
|
|
|
+ if "integrated" in f.message or "legacy marker" in f.message or "legacy heading" in f.message
|
|
|
+ ]
|
|
|
+ html_integrity_errors = [f for f in errors if "html fragment leak" in f.message]
|
|
|
|
|
|
return {
|
|
|
"summary": {
|
|
|
@@ -251,6 +348,8 @@ def validate(directory: Path, content: Path) -> dict[str, Any]:
|
|
|
"learning_progression": {"passed": not learning_progression_errors, "errors": len(learning_progression_errors)},
|
|
|
"knowledge_system": {"passed": not knowledge_system_errors, "errors": len(knowledge_system_errors)},
|
|
|
"natural_section_story": {"passed": not natural_section_story_errors, "errors": len(natural_section_story_errors)},
|
|
|
+ "integrated_sections": {"passed": not integrated_section_errors, "errors": len(integrated_section_errors)},
|
|
|
+ "html_integrity": {"passed": not html_integrity_errors, "errors": len(html_integrity_errors)},
|
|
|
},
|
|
|
"findings": [f.__dict__ for f in all_findings],
|
|
|
}
|