feat: standardize Word export and Office conversion

This commit is contained in:
吉鑫升 committed 2026-08-27 00:09:08 +08:00
1 parent 5be21f2c3d
commit c5eefe60f0
12 files changed
+410 -60

No files matched your search

@@ -214,7 +214,13 @@ def _strip_inline_markdown(text: str) -> str:
def _strip_manual_heading_number(text: str) -> str:
return re.sub(
r"^\s*(?:[一二三四五六七八九十百千]+、|[((][一二三四五六七八九十百千]+[))]|\d+[.、]|[((]\d+[))]|\d+[))])\s*",
r"^\s*(?:"
r"[一二三四五六七八九十百千]+[、..]"
r"|[((]\s*[一二三四五六七八九十百千]+\s*[))]"
r"|[((]\s*\d+(?:[..]\d+)*\s*[))]"
r"|\d+(?:[..]\d+)+(?:[..、])?"
r"|\d+[..、))]"
r")\s*",
"",
text,
).strip()
@@ -338,7 +344,6 @@ def _parse_markdown(markdown: str, explicit_title: str | None) -> tuple[str | No
if re.fullmatch(r"\s*(?:---|\*\*\*|___)\s*", raw):
flush_paragraph()
blocks.append(ParagraphBlock("horizontal_rule", "────────"))
index += 1
continue
@@ -584,8 +589,6 @@ def _blocks_xml(blocks: list[Block], relationships: _DocumentRelationships) -> s
output.append(_paragraph_xml("QuoteText", _inline_xml(block.text, relationships)))
elif block.kind == "image":
output.append(_image_paragraph_xml(block.text, relationships))
elif block.kind == "horizontal_rule":
output.append(_paragraph_xml("BodyText", _run_xml(block.text), alignment="center"))
else:
output.append(_paragraph_xml("BodyText", _inline_xml(block.text, relationships)))
return "".join(output)
@@ -677,6 +680,7 @@ def _styles_xml() -> str:
font=HEADING_1_FONT_FAMILY,
size=BODY_SIZE_HALF_POINTS,
alignment="left",
first_line_chars=200,
keep_next=True,
keep_lines=True,
outline_level=0,
@@ -687,6 +691,7 @@ def _styles_xml() -> str:
font=HEADING_2_FONT_FAMILY,
size=BODY_SIZE_HALF_POINTS,
alignment="left",
first_line_chars=200,
keep_next=True,
keep_lines=True,
outline_level=1,
@@ -700,6 +705,7 @@ def _styles_xml() -> str:
font=BODY_FONT_FAMILY,
size=BODY_SIZE_HALF_POINTS,
alignment="left",
first_line_chars=200 if level == 3 else 0,
keep_next=True,
keep_lines=True,
outline_level=level - 1,
@@ -769,10 +775,11 @@ def _styles_xml() -> str:
def _heading_level_xml(level: int, format_value: str, text: str, font: str) -> str:
restart = f'<w:lvlRestart w:val="{level}"/>' if level > 0 else ""
first_line_chars = ' w:firstLineChars="200"' if level <= 2 else ""
return (
f'<w:lvl w:ilvl="{level}"><w:start w:val="1"/>{restart}<w:numFmt w:val="{format_value}"/>'
f'<w:lvlText w:val="{_xml_attr(text)}"/><w:lvlJc w:val="left"/>'
'<w:pPr><w:ind w:left="0" w:hanging="0"/></w:pPr>'
f'<w:pPr><w:ind w:left="0" w:hanging="0"{first_line_chars}/></w:pPr>'
f'<w:rPr>{_font_run_properties(font, BODY_SIZE_HALF_POINTS)}</w:rPr></w:lvl>'
)
@@ -798,7 +805,7 @@ def _numbering_xml() -> str:
heading_levels = "".join(
[
_heading_level_xml(0, "chineseCounting", "%1、", HEADING_1_FONT_FAMILY),
_heading_level_xml(1, "chineseCounting", "(%2)", HEADING_2_FONT_FAMILY),
_heading_level_xml(1, "chineseCounting", "(%2)", HEADING_2_FONT_FAMILY),
_heading_level_xml(2, "decimal", "%3.", BODY_FONT_FAMILY),
_heading_level_xml(3, "decimal", "(%4)", BODY_FONT_FAMILY),
_heading_level_xml(4, "decimal", "%5)", BODY_FONT_FAMILY),