refactor: 重构解析器架构并添加编码检测和配置管理

简化 parse_input() 为纯调度器,通过遍历 readers 的 supports() 方法识别输入类型,移除 URL 特殊处理和文件检查逻辑。各 reader 的 parse() 方法负责完整验证(文件存在、格式有效性)。

新增功能:
- 添加 chardet 编码自动检测,支持多种中文编码回退机制
- 创建统一配置类管理编码、下载超时、日志等级等配置项
- HTML reader 支持本地文件编码检测和 URL 统一处理

安全性改进:
- 修复 safe_open_zip() 路径遍历漏洞,使用 pathlib 规范化路径
- 添加边界检查,search_markdown() 检查负数参数

其他改进:
- 修复类型注解(argparse.Namespace)
- 日志系统仅输出 ERROR 级别,避免干扰 Markdown 输出
- 更新 BaseReader 接口文档,明确 supports() 和 parse() 职责划分
- 同步 delta specs 到主 specs(document-reading、html-reader、configuration、encoding-detection)
This commit is contained in:
2026-03-08 16:33:40 +08:00
parent eb044d37d9
commit 750ef50a8d
17 changed files with 5567 additions and 76 deletions

View File

@@ -16,7 +16,19 @@ class BaseReader(ABC):
@abstractmethod
def supports(self, file_path: str) -> bool:
"""判断是否支持给定的文件。"""
"""
判断是否支持给定的输入(轻量检查)。
仅做初步判断如扩展名、URL 模式),不进行完整验证。
完整验证(文件存在、格式有效性)在 parse() 中进行。
不访问文件系统,不打开文件。
Args:
file_path: 文件路径或 URL
Returns:
True 如果可能支持False 否则
"""
pass
@abstractmethod
@@ -24,7 +36,12 @@ class BaseReader(ABC):
"""
解析文件并返回 Markdown 内容。
返回: (content, failures)
需要检查文件存在和格式有效性,然后执行实际解析逻辑。
Args:
file_path: 文件路径或 URL
Returns: (content, failures)
- content: 成功时返回 Markdown 内容,失败时返回 None
- failures: 各解析器的失败原因列表
"""

View File

@@ -1,5 +1,6 @@
"""DOCX 文件阅读器,支持多种解析方法。"""
import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
@@ -31,10 +32,19 @@ class DocxReader(BaseReader):
return [".docx"]
def supports(self, file_path: str) -> bool:
return is_valid_docx(file_path)
return file_path.endswith('.docx')
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
failures = []
# 检查文件是否存在
if not os.path.exists(file_path):
return None, ["文件不存在"]
# 验证文件格式
if not is_valid_docx(file_path):
return None, ["不是有效的 DOCX 文件"]
content = None
for parser_name, parser_func in PARSERS:

View File

@@ -4,7 +4,8 @@ import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
from utils import is_html_file, is_url
from utils import is_url
import encoding_detection
from . import cleaner
from . import downloader
@@ -30,7 +31,7 @@ class HtmlReader(BaseReader):
return [".html", ".htm"]
def supports(self, file_path: str) -> bool:
return is_url(file_path) or is_html_file(file_path)
return is_url(file_path) or file_path.endswith(('.html', '.htm'))
def download_and_parse(self, url: str) -> Tuple[Optional[str], List[str]]:
"""下载 URL 并解析"""
@@ -69,15 +70,14 @@ class HtmlReader(BaseReader):
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
all_failures = []
# 判断输入类型
if is_url(file_path):
return self.download_and_parse(file_path)
# 读取 HTML 文件
try:
with open(file_path, 'r', encoding='utf-8') as f:
html_content = f.read()
except Exception as e:
return None, [f"- 读取文件失败: {str(e)}"]
# 读取本地 HTML 文件,使用编码检测
html_content, error = encoding_detection.read_text_file(file_path)
if error:
return None, [f"- {error}"]
# 清理 HTML
html_content = cleaner.clean_html_content(html_content)

View File

@@ -1,5 +1,6 @@
"""PDF 文件阅读器支持多种解析方法OCR 优先)。"""
import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
@@ -31,10 +32,19 @@ class PdfReader(BaseReader):
return [".pdf"]
def supports(self, file_path: str) -> bool:
return is_valid_pdf(file_path)
return file_path.endswith('.pdf')
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
failures = []
# 检查文件是否存在
if not os.path.exists(file_path):
return None, ["文件不存在"]
# 验证文件格式
if not is_valid_pdf(file_path):
return None, ["不是有效的 PDF 文件"]
content = None
for parser_name, parser_func in PARSERS:

View File

@@ -1,5 +1,6 @@
"""PPTX 文件阅读器,支持多种解析方法。"""
import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
@@ -29,10 +30,19 @@ class PptxReader(BaseReader):
return [".pptx"]
def supports(self, file_path: str) -> bool:
return is_valid_pptx(file_path)
return file_path.endswith('.pptx')
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
failures = []
# 检查文件是否存在
if not os.path.exists(file_path):
return None, ["文件不存在"]
# 验证文件格式
if not is_valid_pptx(file_path):
return None, ["不是有效的 PPTX 文件"]
content = None
for parser_name, parser_func in PARSERS:

View File

@@ -1,5 +1,6 @@
"""XLSX 文件阅读器,支持多种解析方法。"""
import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
@@ -29,10 +30,19 @@ class XlsxReader(BaseReader):
return [".xlsx"]
def supports(self, file_path: str) -> bool:
return is_valid_xlsx(file_path)
return file_path.endswith('.xlsx')
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
failures = []
# 检查文件是否存在
if not os.path.exists(file_path):
return None, ["文件不存在"]
# 验证文件格式
if not is_valid_xlsx(file_path):
return None, ["不是有效的 XLSX 文件"]
content = None
for parser_name, parser_func in PARSERS: