#!/usr/bin/env python3 """检查生成型 Markdown 是否保持中文正文。""" from __future__ import annotations import argparse import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DEFAULT_TARGETS = ("docs", "agents", "skills", "assets", "output", "progress.md") FORBIDDEN_PHRASES = [ "Use this skill", "This report", "These standards", "Generated:", "Updated:", "Expected output:", "Scene ID:", "Prompt file:", "Audio Prompt", "Video Prompt", "Negative Prompt", "Candidate Strategy", "Acceptance", "Quality Gate", "Rejection Rules", "Promotion Rules", "Current Run Status", "Current Project Status", "Required Next Actions", "A premium ambient audio app icon", "Apple-level", "light checks passed", "release approved", "ready、missing", ] TECHNICAL_WORDS = { "android", "androidx", "api", "app", "appcontainer", "audacity", "audioldm2", "audio", "comfyui", "compose", "datastore", "exoplayer", "ffmpeg", "ffprobe", "flow", "gradio", "hilt", "i2v", "jetpack", "junit", "kotlin", "ksp", "kapt", "liveData".lower(), "maestro", "media3", "mochi", "mvvm", "retrofit", "room", "runway", "sora", "stable", "stateflow", "superdesign", "timber", "uv", "viewmodel", "wan", } ENGLISH_WORD_RE = re.compile(r"[A-Za-z][A-Za-z'-]*") CJK_RE = re.compile(r"[\u3400-\u9fff]") def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("targets", nargs="*", default=list(DEFAULT_TARGETS), help="要检查的文件或目录") parser.add_argument("--max-errors", type=int, default=80, help="最多打印多少条错误") return parser.parse_args() def iter_markdown(paths: list[str]) -> list[Path]: files: list[Path] = [] for item in paths: path = ROOT / item if path.is_file() and path.suffix == ".md": files.append(path) elif path.is_dir(): files.extend(sorted(path.rglob("*.md"))) return sorted(set(files)) def has_cjk(text: str) -> bool: return bool(CJK_RE.search(text)) def is_code_or_literal(line: str) -> bool: stripped = line.strip() if not stripped: return True if stripped.startswith(("```", "| ---", "---", "- `", "`")): return True if stripped.startswith("|") and re.search(r"[_/@]|\d", stripped): return True if "`" in stripped: return True if "://" in stripped or stripped.startswith(("http", "www.")): return True if re.search(r"\.(kt|kts|py|md|json|yaml|yml|png|mp4|wav|m4a|xml|gradle|pen|sh)\b", stripped): return True if re.search(r"\b[a-zA-Z0-9_./-]+/[a-zA-Z0-9_./-]+\b", stripped): return True if re.match(r"^[A-Za-z0-9_./:-]+$", stripped): return True return False def english_prose_risk(line: str) -> bool: stripped = line.strip() if has_cjk(stripped) or is_code_or_literal(stripped): return False words = [word.lower() for word in ENGLISH_WORD_RE.findall(stripped)] if len(words) < 5: return False non_technical = [word for word in words if word not in TECHNICAL_WORDS] return len(non_technical) >= 4 def check_file(path: Path) -> list[str]: errors: list[str] = [] in_fence = False relative = path.relative_to(ROOT).as_posix() for lineno, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1): stripped = line.strip() if stripped.startswith("```"): in_fence = not in_fence continue if in_fence: continue for phrase in FORBIDDEN_PHRASES: if phrase in line: errors.append(f"{relative}:{lineno}: 发现英文模板短语 `{phrase}`") if english_prose_risk(line): errors.append(f"{relative}:{lineno}: 疑似英文正文 `{stripped[:120]}`") return errors def main() -> int: args = parse_args() errors: list[str] = [] for path in iter_markdown(args.targets): errors.extend(check_file(path)) if len(errors) >= args.max_errors: break if errors: for error in errors[: args.max_errors]: print(error, file=sys.stderr) if len(errors) > args.max_errors: print(f"还有 {len(errors) - args.max_errors} 条错误未打印", file=sys.stderr) return 1 print("Markdown 中文正文检查通过") return 0 if __name__ == "__main__": raise SystemExit(main())