- 测试数量从 83 个增加到 193 个 (+132%) - 代码覆盖率从 48% 提升到 69% (+44%) - 为每种文档格式的所有 Reader 实现创建独立测试 - 添加跨 Reader 的一致性验证测试 - 新增 4 个测试规范 (cli-testing, exception-testing, reader-testing, test-fixtures) - 更新 README 测试统计信息 测试覆盖: - DOCX: python-docx, markitdown, docling, native-xml, pypandoc, unstructured - PDF: pypdf, markitdown, docling, docling-ocr, unstructured, unstructured-ocr - HTML: html2text, markitdown, trafilatura, domscribe - PPTX: python-pptx, markitdown, docling, native-xml, unstructured - XLSX: pandas, markitdown, docling, native-xml, unstructured - CLI: 所有命令行选项和错误处理 所有 193 个测试通过。
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""统一解析调度器,负责根据输入类型选择合适的 reader 进行解析。"""
|
|
|
|
import argparse
|
|
import sys
|
|
from typing import List, Optional, Tuple
|
|
|
|
from scripts.core.exceptions import FileDetectionError, ReaderNotFoundError
|
|
from scripts.core.markdown import (
|
|
normalize_markdown_whitespace,
|
|
remove_markdown_images,
|
|
)
|
|
from scripts.readers import BaseReader
|
|
|
|
|
|
def parse_input(
|
|
input_path: str,
|
|
readers: List[BaseReader],
|
|
) -> Tuple[Optional[str], List[str]]:
|
|
"""
|
|
统一解析入口函数,遍历 readers 选择合适的 reader 进行解析。
|
|
|
|
返回: (content, failures)
|
|
- content: 成功时返回 Markdown 内容,失败时返回 None
|
|
- failures: 各解析器的失败原因列表
|
|
"""
|
|
if not input_path:
|
|
raise FileDetectionError("输入路径不能为空")
|
|
|
|
for reader in readers:
|
|
if reader.supports(input_path):
|
|
return reader.parse(input_path)
|
|
|
|
raise ReaderNotFoundError(f"未找到支持的 reader: {input_path}")
|
|
|
|
|
|
def process_content(content: str) -> str:
|
|
"""处理解析后的 Markdown 内容"""
|
|
content = remove_markdown_images(content)
|
|
content = normalize_markdown_whitespace(content)
|
|
return content
|
|
|
|
|
|
def output_result(
|
|
content: str,
|
|
args: argparse.Namespace,
|
|
) -> None:
|
|
"""根据命令行参数输出结果"""
|
|
if args.count:
|
|
print(len(content.replace("\n", "")))
|
|
elif args.lines:
|
|
print(len(content.split("\n")))
|
|
elif args.titles:
|
|
from scripts.core.markdown import extract_titles
|
|
|
|
titles = extract_titles(content)
|
|
for title in titles:
|
|
print(title)
|
|
elif args.title_content:
|
|
from scripts.core.markdown import extract_title_content
|
|
|
|
title_content = extract_title_content(content, args.title_content)
|
|
if title_content is None:
|
|
print(f"错误: 未找到标题 '{args.title_content}'")
|
|
sys.exit(1)
|
|
print(title_content, end="")
|
|
elif args.search:
|
|
from scripts.core.markdown import search_markdown
|
|
|
|
search_result = search_markdown(content, args.search, args.context)
|
|
if search_result is None:
|
|
print(f"错误: 正则表达式无效或未找到匹配: '{args.search}'")
|
|
sys.exit(1)
|
|
print(search_result, end="")
|
|
else:
|
|
print(content, end="")
|