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:
1
tests/__init__.py
Normal file
1
tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests package for lyxy-document."""
|
||||
21
tests/conftest.py
Normal file
21
tests/conftest.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""测试配置和共享 fixtures。"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_markdown():
|
||||
"""示例 Markdown 文本。"""
|
||||
return """# 标题
|
||||
|
||||
这是一段测试文本。
|
||||
|
||||
## 子标题
|
||||
|
||||
- 列表项 1
|
||||
- 列表项 2
|
||||
|
||||
### 另一个标题
|
||||
|
||||
这是更多的文本。
|
||||
"""
|
||||
1
tests/test_core/__init__.py
Normal file
1
tests/test_core/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for core module."""
|
||||
56
tests/test_core/test_markdown.py
Normal file
56
tests/test_core/test_markdown.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""测试 Markdown 工具函数。"""
|
||||
|
||||
from core import (
|
||||
get_heading_level,
|
||||
extract_titles,
|
||||
normalize_markdown_whitespace,
|
||||
remove_markdown_images,
|
||||
)
|
||||
|
||||
|
||||
class TestGetHeadingLevel:
|
||||
"""测试 get_heading_level 函数。"""
|
||||
|
||||
def test_h1(self):
|
||||
assert get_heading_level("# 标题") == 1
|
||||
|
||||
def test_h2(self):
|
||||
assert get_heading_level("## 子标题") == 2
|
||||
|
||||
def test_h6(self):
|
||||
assert get_heading_level("###### 六级标题") == 6
|
||||
|
||||
def test_no_heading(self):
|
||||
assert get_heading_level("普通文本") == 0
|
||||
|
||||
def test_no_space(self):
|
||||
assert get_heading_level("#标题") == 0
|
||||
|
||||
|
||||
class TestExtractTitles:
|
||||
"""测试 extract_titles 函数。"""
|
||||
|
||||
def test_extract_multiple_titles(self, sample_markdown):
|
||||
titles = extract_titles(sample_markdown)
|
||||
assert len(titles) == 3
|
||||
assert "# 标题" in titles
|
||||
assert "## 子标题" in titles
|
||||
assert "### 另一个标题" in titles
|
||||
|
||||
|
||||
class TestNormalizeMarkdownWhitespace:
|
||||
"""测试 normalize_markdown_whitespace 函数。"""
|
||||
|
||||
def test_multiple_blank_lines(self):
|
||||
content = "line1\n\n\n\nline2"
|
||||
result = normalize_markdown_whitespace(content)
|
||||
assert result == "line1\n\nline2"
|
||||
|
||||
|
||||
class TestRemoveMarkdownImages:
|
||||
"""测试 remove_markdown_images 函数。"""
|
||||
|
||||
def test_remove_image(self):
|
||||
content = "Text  more text"
|
||||
result = remove_markdown_images(content)
|
||||
assert "" not in result
|
||||
1
tests/test_readers/__init__.py
Normal file
1
tests/test_readers/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for readers module."""
|
||||
1
tests/test_readers/test_docx/__init__.py
Normal file
1
tests/test_readers/test_docx/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for docx reader."""
|
||||
1
tests/test_readers/test_html/__init__.py
Normal file
1
tests/test_readers/test_html/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for html reader."""
|
||||
1
tests/test_readers/test_pdf/__init__.py
Normal file
1
tests/test_readers/test_pdf/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for pdf reader."""
|
||||
1
tests/test_readers/test_pptx/__init__.py
Normal file
1
tests/test_readers/test_pptx/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for pptx reader."""
|
||||
1
tests/test_readers/test_xlsx/__init__.py
Normal file
1
tests/test_readers/test_xlsx/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for xlsx reader."""
|
||||
1
tests/test_utils/__init__.py
Normal file
1
tests/test_utils/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for utils module."""
|
||||
32
tests/test_utils/test_file_detection.py
Normal file
32
tests/test_utils/test_file_detection.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""测试文件检测工具函数。"""
|
||||
|
||||
from utils import is_url, is_html_file
|
||||
|
||||
|
||||
class TestIsUrl:
|
||||
"""测试 is_url 函数。"""
|
||||
|
||||
def test_http_url(self):
|
||||
assert is_url("http://example.com") is True
|
||||
|
||||
def test_https_url(self):
|
||||
assert is_url("https://example.com") is True
|
||||
|
||||
def test_not_url(self):
|
||||
assert is_url("file.txt") is False
|
||||
|
||||
|
||||
class TestIsHtmlFile:
|
||||
"""测试 is_html_file 函数。"""
|
||||
|
||||
def test_html_extension(self):
|
||||
assert is_html_file("file.html") is True
|
||||
|
||||
def test_htm_extension(self):
|
||||
assert is_html_file("file.htm") is True
|
||||
|
||||
def test_uppercase_extension(self):
|
||||
assert is_html_file("FILE.HTML") is True
|
||||
|
||||
def test_not_html(self):
|
||||
assert is_html_file("file.txt") is False
|
||||
Reference in New Issue
Block a user