sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -8,6 +8,7 @@ import re
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from xml.etree import ElementTree
|
||||
|
||||
import pytest
|
||||
@@ -97,8 +98,11 @@ def test_formal_docx_contains_required_layout_numbering_and_embedded_font(tmp_pa
|
||||
|
||||
assert '<w:numFmt w:val="chineseCounting"/>' in numbering
|
||||
assert '<w:lvlText w:val="%1、"/>' in numbering
|
||||
assert '<w:lvlText w:val="(%2)"/>' in numbering
|
||||
assert '<w:lvlText w:val="%3."/>' in numbering
|
||||
assert '<w:lvl w:ilvl="1">' in numbering
|
||||
assert '<w:lvlRestart w:val="1"/><w:isLgl/><w:lvlText w:val="%1.%2"/>' in numbering
|
||||
assert '<w:lvlRestart w:val="2"/><w:isLgl/><w:lvlText w:val="%1.%2.%3"/>' in numbering
|
||||
assert '<w:lvlRestart w:val="3"/><w:isLgl/><w:lvlText w:val="%1.%2.%3.%4"/>' in numbering
|
||||
assert '<w:lvlRestart w:val="4"/><w:isLgl/><w:lvlText w:val="%1.%2.%3.%4.%5"/>' in numbering
|
||||
assert numbering.count('w:firstLineChars="200"') == 3
|
||||
assert '<w:evenAndOddHeaders/>' in settings
|
||||
assert '<w:updateFields w:val="true"/>' in settings
|
||||
@@ -175,3 +179,246 @@ def test_export_rejects_wrong_font(tmp_path: Path) -> None:
|
||||
)
|
||||
def test_manual_heading_number_is_removed_as_one_complete_prefix(source: str, expected: str) -> None:
|
||||
assert word_export._strip_manual_heading_number(source) == expected
|
||||
|
||||
|
||||
def test_all_markdown_heading_levels_map_to_word_heading_levels() -> None:
|
||||
markdown = """# 文档主标题
|
||||
|
||||
## 一、一级标题
|
||||
### 1.1 二级标题
|
||||
#### 1.1.1 三级标题
|
||||
##### 1.1.1.1 四级标题
|
||||
###### 1.1.1.1.1 五级标题
|
||||
"""
|
||||
|
||||
title, blocks = word_export._parse_markdown(markdown, None)
|
||||
headings = [block for block in blocks if isinstance(block, word_export.ParagraphBlock)]
|
||||
|
||||
assert title == "文档主标题"
|
||||
assert [(block.kind, block.text, block.level) for block in headings] == [
|
||||
("heading", "一级标题", 0),
|
||||
("heading", "二级标题", 1),
|
||||
("heading", "三级标题", 2),
|
||||
("heading", "四级标题", 3),
|
||||
("heading", "五级标题", 4),
|
||||
]
|
||||
|
||||
payload = word_export.build_markdown_docx(markdown)
|
||||
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
|
||||
document = archive.read("word/document.xml").decode("utf-8")
|
||||
|
||||
mapped_levels = re.findall(
|
||||
r'<w:pStyle w:val="(Heading\d)"/><w:numPr><w:ilvl w:val="(\d)"/>',
|
||||
document,
|
||||
)
|
||||
assert mapped_levels == [
|
||||
("Heading1", "0"),
|
||||
("Heading2", "1"),
|
||||
("Heading3", "2"),
|
||||
("Heading4", "3"),
|
||||
("Heading5", "4"),
|
||||
]
|
||||
|
||||
|
||||
def test_tolerates_model_generated_atx_heading_variations() -> None:
|
||||
markdown = """ #模型生成的标题
|
||||
|
||||
##一级标题 ##
|
||||
###**1.1 二级标题**
|
||||
#### 1.1.1 三级标题
|
||||
#####1.1.1.1 四级标题
|
||||
######1.1.1.1.1 五级标题
|
||||
"""
|
||||
|
||||
title, blocks = word_export._parse_markdown(markdown, None)
|
||||
headings = [block for block in blocks if getattr(block, "kind", "") == "heading"]
|
||||
|
||||
assert title == "模型生成的标题"
|
||||
assert [(block.text, block.level) for block in headings] == [
|
||||
("一级标题", 0),
|
||||
("二级标题", 1),
|
||||
("三级标题", 2),
|
||||
("四级标题", 3),
|
||||
("五级标题", 4),
|
||||
]
|
||||
|
||||
|
||||
def test_number_prefix_corrects_wrong_hash_depth_and_recovers_missing_hashes() -> None:
|
||||
markdown = """# 标题
|
||||
|
||||
### 一、一级标题
|
||||
正文。
|
||||
|
||||
## 1.1 二级标题
|
||||
正文。
|
||||
|
||||
**1.2 缺少井号的二级标题**
|
||||
正文。
|
||||
|
||||
1.2.1 缺少井号的三级标题
|
||||
正文。
|
||||
|
||||
(2)兼容旧格式四级标题
|
||||
正文。
|
||||
"""
|
||||
|
||||
_, blocks = word_export._parse_markdown(markdown, None)
|
||||
headings = [block for block in blocks if getattr(block, "kind", "") == "heading"]
|
||||
|
||||
assert [(block.text, block.level) for block in headings] == [
|
||||
("一级标题", 0),
|
||||
("二级标题", 1),
|
||||
("缺少井号的二级标题", 1),
|
||||
("缺少井号的三级标题", 2),
|
||||
("兼容旧格式四级标题", 3),
|
||||
]
|
||||
|
||||
|
||||
def test_hierarchical_heading_without_hashes_or_blank_line_is_recovered() -> None:
|
||||
markdown = "正文没有空行。\n1.1 模型漏掉井号与空行\n下一段正文。"
|
||||
|
||||
_, blocks = word_export._parse_markdown(markdown, "标题")
|
||||
|
||||
headings = [block for block in blocks if getattr(block, "kind", "") == "heading"]
|
||||
assert [(block.text, block.level) for block in headings] == [
|
||||
("模型漏掉井号与空行", 1)
|
||||
]
|
||||
|
||||
|
||||
def test_heading_tolerance_does_not_consume_routes_lists_or_fenced_code() -> None:
|
||||
markdown = """#/public/scheduled-tasks/123
|
||||
|
||||
1. 普通有序列表
|
||||
|
||||
```markdown
|
||||
###1.1 代码块里的示例
|
||||
```
|
||||
"""
|
||||
|
||||
title, blocks = word_export._parse_markdown(markdown, "显式标题")
|
||||
|
||||
assert title == "显式标题"
|
||||
assert not any(getattr(block, "kind", "") == "heading" for block in blocks)
|
||||
assert any(getattr(block, "kind", "") == "ordered_list" for block in blocks)
|
||||
assert any(getattr(block, "kind", "") == "code" and "###1.1" in block.text for block in blocks)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("style", "markdown", "expected"),
|
||||
[
|
||||
(
|
||||
"hybrid_decimal",
|
||||
"## 一、一级\n### 1.1 二级\n#### 1.1.1 三级",
|
||||
[("一级", 0), ("二级", 1), ("三级", 2)],
|
||||
),
|
||||
(
|
||||
"official_chinese",
|
||||
"## 一、一级\n### (一)二级\n#### 1. 三级\n##### (1)四级\n###### 1)五级",
|
||||
[("一级", 0), ("二级", 1), ("三级", 2), ("四级", 3), ("五级", 4)],
|
||||
),
|
||||
(
|
||||
"decimal",
|
||||
"## 1 一级\n### 1.1 二级\n#### 1.1.1 三级",
|
||||
[("一级", 0), ("二级", 1), ("三级", 2)],
|
||||
),
|
||||
(
|
||||
"chapter_section",
|
||||
"## 第一章 一级\n### 第一节 二级\n#### 一、三级\n##### (一)四级\n###### 1. 五级",
|
||||
[("一级", 0), ("二级", 1), ("三级", 2), ("四级", 3), ("五级", 4)],
|
||||
),
|
||||
(
|
||||
"none",
|
||||
"## 一级\n### 二级\n#### 三级",
|
||||
[("一级", 0), ("二级", 1), ("三级", 2)],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_heading_prefixes_are_parsed_according_to_selected_preset(
|
||||
style: str,
|
||||
markdown: str,
|
||||
expected: list[tuple[str, int]],
|
||||
) -> None:
|
||||
_, blocks = word_export._parse_markdown(
|
||||
markdown,
|
||||
None,
|
||||
heading_numbering_style=style,
|
||||
)
|
||||
|
||||
headings = [block for block in blocks if getattr(block, "kind", "") == "heading"]
|
||||
assert [(block.text, block.level) for block in headings] == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("style", "expected_level_texts"),
|
||||
[
|
||||
("hybrid_decimal", ["%1、", "%1.%2", "%1.%2.%3", "%1.%2.%3.%4", "%1.%2.%3.%4.%5"]),
|
||||
("official_chinese", ["%1、", "(%2)", "%3.", "(%4)", "%5)"]),
|
||||
("decimal", ["%1", "%1.%2", "%1.%2.%3", "%1.%2.%3.%4", "%1.%2.%3.%4.%5"]),
|
||||
("chapter_section", ["第%1章", "第%2节", "%3、", "(%4)", "%5."]),
|
||||
],
|
||||
)
|
||||
def test_heading_numbering_presets_emit_expected_word_patterns(
|
||||
style: str,
|
||||
expected_level_texts: list[str],
|
||||
) -> None:
|
||||
numbering = word_export._numbering_xml(style)
|
||||
|
||||
for expected in expected_level_texts:
|
||||
assert f'<w:lvlText w:val="{expected}"/>' in numbering
|
||||
ElementTree.fromstring(numbering)
|
||||
|
||||
|
||||
def test_no_numbering_preset_keeps_heading_styles_without_numpr() -> None:
|
||||
payload = word_export.build_markdown_docx(
|
||||
"# 文档标题\n\n## 一级标题\n\n### 二级标题",
|
||||
heading_numbering_style="none",
|
||||
)
|
||||
|
||||
with zipfile.ZipFile(io.BytesIO(payload)) as archive:
|
||||
document = archive.read("word/document.xml").decode("utf-8")
|
||||
|
||||
assert '<w:pStyle w:val="Heading1"/>' in document
|
||||
assert '<w:pStyle w:val="Heading2"/>' in document
|
||||
assert "<w:numPr>" not in document
|
||||
|
||||
|
||||
def test_unknown_heading_numbering_preset_is_rejected() -> None:
|
||||
with pytest.raises(word_export.WordExportError, match="未知的 Word 标题编号格式"):
|
||||
word_export.build_markdown_docx("正文", heading_numbering_style="unknown")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_export_route_uses_current_heading_numbering_setting(monkeypatch) -> None:
|
||||
from app.gateway.routers import writing
|
||||
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
def fake_build(markdown, title, *, embedded_images, heading_numbering_style):
|
||||
captured.update(
|
||||
markdown=markdown,
|
||||
title=title,
|
||||
embedded_images=embedded_images,
|
||||
heading_numbering_style=heading_numbering_style,
|
||||
)
|
||||
return b"docx"
|
||||
|
||||
monkeypatch.setattr(
|
||||
writing,
|
||||
"load_system_settings",
|
||||
lambda: SimpleNamespace(
|
||||
prompt_prefix=SimpleNamespace(word_heading_numbering_style="official_chinese")
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr(writing, "build_markdown_docx", fake_build)
|
||||
|
||||
response = await writing.export_markdown_docx(
|
||||
writing.MarkdownDocxExportRequest(markdown="# 标题", title="测试")
|
||||
)
|
||||
|
||||
assert response.body == b"docx"
|
||||
assert captured == {
|
||||
"markdown": "# 标题",
|
||||
"title": "测试",
|
||||
"embedded_images": None,
|
||||
"heading_numbering_style": "official_chinese",
|
||||
}
|
||||
Reference in new issue
Block a user