from __future__ import annotations import json import re from pathlib import Path from edkb_system.contracts import Context def _has_doc_meta(text: str) -> bool: flat = " ".join(text.split()) has_header = bool( re.search(r"制定部门.+制/修订日期.+页码", flat) or re.search(r"Created Dept\..+Date.+Page", flat) ) has_meta = bool( re.search(r"文件编号.+文件级别.+版本", flat) or re.search(r"Doc\.\s*No\..+File\s*Level.+Rev\.?", flat) ) return has_header or has_meta def _expect_doc_meta(text: str) -> bool: keys = ["文件编号", "文件级别", "制/修订日期", "制定部门", "页码"] return any(k in text for k in keys) def _expect_flow_artifacts(text: str) -> bool: if "作业流程图" not in text: return False hints = ["序号", "流程说明", "责任", "相关记录"] return sum(1 for h in hints if h in text) >= 2 def _expect_related_forms_table(text: str) -> bool: if "相关表单" not in text: return False return ("表单编号" in text) or bool(re.search(r"PB-\d{2,}", text)) def _has_source_images(out_dir: Path) -> bool: raw = out_dir / "01_raw_layout.json" if not raw.exists(): return False try: data = json.loads(raw.read_text(encoding="utf-8")) if isinstance(data, list): for b in data: if isinstance(b, dict) and b.get("type") == "image": return True except Exception: return False return False def _has_markdown_image_reference(text: str) -> bool: for m in re.finditer(r"!\[[^\]]*\]\(([^)]+)\)", text): target = (m.group(1) or "").strip() if target.startswith("data:image/"): return True if target.startswith("http://") or target.startswith("https://"): return True return False def _has_remote_image_url(text: str) -> bool: for m in re.finditer(r"!\[[^\]]*\]\(([^)]+)\)", text): target = (m.group(1) or "").strip() if target.startswith("http://") or target.startswith("https://"): return True return False def _expect_revision_table(text: str) -> bool: if "文件修订履历" in text: return True hint = ["日期", "修订章节", "主要修订内容", "版本", "修改人"] return sum(1 for h in hint if h in text) >= 4 def _collect_issues(text: str) -> list[dict]: issues: list[dict] = [] lines = text.splitlines() num_pat = re.compile(r"^\s*\d+(?:\.\d+){1,4}\s+") non_empty = [(i + 1, ln.strip()) for i, ln in enumerate(lines) if ln.strip()] if len(non_empty) >= 2: l1, s1 = non_empty[0] l2, s2 = non_empty[1] if "文件编号" in s1 and "文件编号" not in s2 and not s2.startswith("制定部门"): if not re.match(r"^[一二三四五六七八九十]+、", s2): issues.append( { "code": "title_below_meta", "line": l1, "text": f"meta before title: {s1[:80]}", } ) for i, ln in enumerate(lines, start=1): s = ln.strip() if not s: continue if s.startswith("|"): continue if re.search(r"\d+\.\d+(?:\.\d+)?\s+.+\s+\d+\.\d+(?:\.\d+)?\s+", s): issues.append( {"code": "inline_numbered_sections", "line": i, "text": s[:180]} ) if re.search(r"[七八]、参考文件\s+[七八]、相关表单", s): issues.append({"code": "inline_section_titles", "line": i, "text": s[:180]}) for i in range(1, len(lines) - 1): if lines[i].strip(): continue prev = lines[i - 1].strip() nxt = lines[i + 1].strip() if num_pat.match(prev) and num_pat.match(nxt): issues.append( { "code": "blank_between_numbered_sections", "line": i + 1, "text": f"{prev[:70]} / {nxt[:70]}", } ) return issues def run(input_data: dict, context: Context) -> dict: out_dir = Path(input_data["out_dir"]) clean_md = input_data.get("clean_md", "") checks = { "has_doc_meta": _has_doc_meta(clean_md), "has_revision_table": bool( (re.search(r"\|\s*(?:日期|文件日期)\b", clean_md) is not None) and re.search(r"\|\s*.*修订章节", clean_md) and re.search(r"\|\s*版本\b", clean_md) ), "has_flow_table": "| 序号 | 流程说明 | 责任部门 | 相关记录 |" in clean_md, "has_related_forms_table": bool( re.search(r"\|\s*表单编号\s*\|", clean_md) and re.search(r"\|\s*[-:]{3,}\s*\|", clean_md) ), "has_flow_file": (out_dir / "02_flow_procedure.md").exists(), "has_embedded_images": _has_markdown_image_reference(clean_md), "has_remote_image_urls": _has_remote_image_url(clean_md), } flow_expected = _expect_flow_artifacts(clean_md) expected = { "has_doc_meta": _expect_doc_meta(clean_md), "has_revision_table": _expect_revision_table(clean_md), "has_flow_table": flow_expected, "has_related_forms_table": _expect_related_forms_table(clean_md), "has_flow_file": flow_expected, "has_embedded_images": _has_source_images(out_dir), } failed_required = [k for k, v in expected.items() if v and not checks.get(k, False)] status = "pass" if not failed_required else "fail" issues = _collect_issues(clean_md) if expected["has_embedded_images"] and not checks["has_embedded_images"]: issues.append( { "code": "image_not_embedded", "line": 0, "text": "source has image blocks but markdown has no image references", } ) if "data:image/" in clean_md: issues.append( { "code": "legacy_base64_image", "line": 0, "text": "markdown still contains legacy data:image base64 payload", } ) return { "status": status, "checks": checks, "expected": expected, "failed_required": failed_required, "issues": issues, "issue_count": len(issues), "warnings_count": len(context.warnings), }