feat: 统一文档解析器项目 - 迁移 lyxy-reader-office 和 lyxy-reader-html
## 功能特性 - 建立统一的项目结构,包含 core/、readers/、utils/、tests/ 模块 - 迁移 lyxy-reader-office 的所有解析器(docx、xlsx、pptx、pdf) - 迁移 lyxy-reader-html 的所有解析器(html、url 下载) - 统一 CLI 入口为 lyxy_document_reader.py - 统一 Markdown 后处理逻辑 - 按文件类型组织 readers,每个解析器独立文件 - 依赖分组按文件类型细分(docx、xlsx、pptx、pdf、html、http) - PDF OCR 解析器优先,无参数控制 - 使用 logging 模块替代简单 print - 设计完整的单元测试结构 - 重写项目文档 ## 新增目录/文件 - core/ - 核心模块(异常体系、Markdown 工具、解析调度器) - readers/ - 格式阅读器(base.py + docx/xlsx/pptx/pdf/html) - utils/ - 工具函数(文件类型检测) - tests/ - 测试(conftest.py + test_core/ + test_readers/ + test_utils/) - lyxy_document_reader.py - 统一 CLI 入口 ## 依赖分组 - docx - DOCX 文档解析支持 - xlsx - XLSX 文档解析支持 - pptx - PPTX 文档解析支持 - pdf - PDF 文档解析支持(含 OCR) - html - HTML/URL 解析支持 - http - HTTP/URL 下载支持 - office - Office 格式组合(docx/xlsx/pptx/pdf) - web - Web 格式组合(html/http) - full - 完整功能 - dev - 开发依赖
This commit is contained in:
100
core/parser.py
Normal file
100
core/parser.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""统一解析调度器,负责根据输入类型选择合适的 reader 进行解析。"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from core.exceptions import FileDetectionError, ReaderNotFoundError
|
||||
from core.markdown import (
|
||||
normalize_markdown_whitespace,
|
||||
remove_markdown_images,
|
||||
)
|
||||
from readers import BaseReader
|
||||
from utils import detect_file_type, is_html_file, is_url
|
||||
|
||||
|
||||
def parse_input(
|
||||
input_path: str,
|
||||
readers: List[BaseReader],
|
||||
) -> Tuple[Optional[str], List[str]]:
|
||||
"""
|
||||
统一解析入口函数,根据输入类型自动选择合适的 reader。
|
||||
|
||||
返回: (content, failures)
|
||||
- content: 成功时返回 Markdown 内容,失败时返回 None
|
||||
- failures: 各解析器的失败原因列表
|
||||
"""
|
||||
if not input_path:
|
||||
raise FileDetectionError("输入路径不能为空")
|
||||
|
||||
# 检测是否为 URL
|
||||
if is_url(input_path):
|
||||
# URL 交给 HTML reader
|
||||
for reader in readers:
|
||||
if hasattr(reader, "download_and_parse"):
|
||||
return reader.download_and_parse(input_path)
|
||||
raise ReaderNotFoundError("未找到支持 URL 下载的 reader")
|
||||
|
||||
# 检测文件是否存在
|
||||
if not os.path.exists(input_path):
|
||||
raise FileDetectionError(f"文件不存在: {input_path}")
|
||||
|
||||
# 检测文件类型
|
||||
file_type = detect_file_type(input_path)
|
||||
|
||||
if file_type:
|
||||
# Office/PDF 文件
|
||||
for reader in readers:
|
||||
if reader.supports(input_path):
|
||||
return reader.parse(input_path)
|
||||
raise ReaderNotFoundError(f"未找到支持 {file_type.upper()} 格式的 reader")
|
||||
elif is_html_file(input_path):
|
||||
# HTML 文件
|
||||
for reader in readers:
|
||||
if reader.supports(input_path):
|
||||
return reader.parse(input_path)
|
||||
raise ReaderNotFoundError("未找到支持 HTML 格式的 reader")
|
||||
else:
|
||||
raise FileDetectionError(f"无法识别的文件类型: {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: any,
|
||||
) -> None:
|
||||
"""根据命令行参数输出结果"""
|
||||
if args.count:
|
||||
print(len(content.replace("\n", "")))
|
||||
elif args.lines:
|
||||
print(len(content.split("\n")))
|
||||
elif args.titles:
|
||||
from core.markdown import extract_titles
|
||||
|
||||
titles = extract_titles(content)
|
||||
for title in titles:
|
||||
print(title)
|
||||
elif args.title_content:
|
||||
from 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 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="")
|
||||
Reference in New Issue
Block a user