- Introduced a 30-minute timeout mechanism to prevent the script from getting stuck in invalid states, with configurable timeout settings. - Unified path configurations across the project to enhance maintainability and avoid hardcoded paths. - Updated documentation and flowcharts to reflect the new timeout feature and improved path management. - Fixed issues with template and screenshot path configurations to ensure consistency across the project.
77 lines
1.9 KiB
Python
77 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""检查所有Markdown文件的编码和完整性"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def check_md_file(file_path):
|
|
"""检查单个MD文件"""
|
|
issues = []
|
|
try:
|
|
# 尝试以UTF-8读取
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
# 检查文件大小
|
|
file_size = os.path.getsize(file_path)
|
|
if file_size == 0:
|
|
issues.append("文件为空")
|
|
|
|
# 检查是否有BOM
|
|
if content.startswith("\ufeff"):
|
|
issues.append("包含UTF-8 BOM标记")
|
|
|
|
# 检查是否有异常字符
|
|
try:
|
|
content.encode("utf-8")
|
|
except UnicodeEncodeError as e:
|
|
issues.append(f"编码错误: {e}")
|
|
|
|
# 检查基本格式
|
|
if not content.strip():
|
|
issues.append("文件内容为空(只有空白字符)")
|
|
|
|
return issues
|
|
|
|
except UnicodeDecodeError as e:
|
|
return [f"无法以UTF-8解码: {e}"]
|
|
except Exception as e:
|
|
return [f"读取文件时出错: {e}"]
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
project_root = Path(__file__).parent
|
|
md_files = list(project_root.rglob("*.md"))
|
|
|
|
print(f"找到 {len(md_files)} 个Markdown文件\n")
|
|
|
|
all_ok = True
|
|
for md_file in sorted(md_files):
|
|
relative_path = md_file.relative_to(project_root)
|
|
issues = check_md_file(md_file)
|
|
|
|
if issues:
|
|
print(f"[ERROR] {relative_path}")
|
|
for issue in issues:
|
|
print(f" - {issue}")
|
|
all_ok = False
|
|
else:
|
|
file_size = os.path.getsize(md_file)
|
|
print(f"[OK] {relative_path} ({file_size} bytes)")
|
|
|
|
print("\n" + "=" * 60)
|
|
if all_ok:
|
|
print("[SUCCESS] All Markdown files are valid!")
|
|
else:
|
|
print("[WARNING] Found some issues, please check above")
|
|
|
|
return 0 if all_ok else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|