sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -36,6 +36,17 @@ BODY_SIZE_HALF_POINTS = 32 # 三号 / 16 pt
TABLE_SIZE_HALF_POINTS = 28 # 四号 / 14 pt
EXACT_LINE_TWIPS = 579 # 28.95 pt * 20
DEFAULT_HEADING_NUMBERING_STYLE = "hybrid_decimal"
HEADING_NUMBERING_STYLES = frozenset(
{
DEFAULT_HEADING_NUMBERING_STYLE,
"official_chinese",
"decimal",
"chapter_section",
"none",
}
)
A4_WIDTH_TWIPS = 11_906
A4_HEIGHT_TWIPS = 16_838
MARGIN_TOP_TWIPS = 2_098 # 3.7 cm
@@ -230,9 +241,78 @@ def _strip_inline_markdown(text: str) -> str:
return re.sub(r"[`*_~]", "", text).strip()
_ATX_HEADING = re.compile(
r"^[ \t\u3000]{0,3}(?P<marks>[##]{1,6})(?![##])"
r"(?P<gap>[ \t\u3000]*)(?P<text>.*?)"
r"(?:[ \t\u3000]+[##]+)?[ \t\u3000]*$"
)
_CHINESE_NUMBER = "一二三四五六七八九十百千零〇两"
def _match_atx_heading(line: str) -> tuple[int, str] | None:
"""Recognize common model variations of an ATX Markdown heading."""
match = _ATX_HEADING.match(line)
if not match:
return None
text = match.group("text").strip()
if not text:
return None
# A missing space is tolerated for generated headings, but fragments such
# as ``#/route`` and Markdown image/link syntax must remain ordinary text.
if not match.group("gap") and text[0] in "/\\[]()!<>":
return None
return len(match.group("marks")), text
def _heading_level_from_number(
text: str,
heading_numbering_style: str = DEFAULT_HEADING_NUMBERING_STYLE,
*,
allow_ambiguous_single: bool = False,
) -> int | None:
"""Infer the Word section level from a visible, model-written prefix."""
plain = _strip_inline_markdown(text).strip()
hierarchy = re.match(r"^(\d{1,3}(?:[..]\d{1,3}){1,4})(?:[..、])?(?![..]?\d)", plain)
if hierarchy:
component_count = len(re.split(r"[..]", hierarchy.group(1)))
return min(component_count - 1, 4)
chinese_plain = rf"[{_CHINESE_NUMBER}]+"
if heading_numbering_style == "chapter_section":
if re.match(rf"^第\s*[{_CHINESE_NUMBER}\d]+\s*章", plain):
return 0
if re.match(rf"^第\s*[{_CHINESE_NUMBER}\d]+\s*节", plain):
return 1
if re.match(rf"^{chinese_plain}[、..]", plain):
return 2
if re.match(rf"^[((]\s*{chinese_plain}\s*[))]", plain):
return 3
if allow_ambiguous_single and re.match(r"^\d+[..、](?!\d)", plain):
return 4
return None
if re.match(rf"^{chinese_plain}[、..]", plain):
return 0
if re.match(rf"^[((]\s*{chinese_plain}\s*[))]", plain):
return 1
if heading_numbering_style == "official_chinese":
if re.match(r"^[((]\s*\d+\s*[))]", plain):
return 3
if allow_ambiguous_single and re.match(r"^\d+[..、](?!\d)", plain):
return 2
if allow_ambiguous_single and re.match(r"^\d+[))](?!\d)", plain):
return 4
elif re.match(r"^[((]\s*\d+\s*[))]", plain):
# Legacy fourth-level numbering remains a safe, unambiguous fallback.
return 3
return None
def _strip_manual_heading_number(text: str) -> str:
return re.sub(
r"^\s*(?:"
rf"第\s*[{_CHINESE_NUMBER}\d]+\s*[章节篇部]"
r"|"
r"[一二三四五六七八九十百千]+[、..]"
r"|[((]\s*[一二三四五六七八九十百千]+\s*[))]"
r"|[((]\s*\d+(?:[..]\d+)*\s*[))]"
@@ -244,6 +324,44 @@ def _strip_manual_heading_number(text: str) -> str:
).strip()
def _clean_heading_text(
text: str,
heading_numbering_style: str = DEFAULT_HEADING_NUMBERING_STYLE,
section_level: int | None = None,
) -> str:
plain = _strip_inline_markdown(text)
if heading_numbering_style == "decimal" and section_level == 0:
plain = re.sub(r"^\s*\d+\s+", "", plain)
return _strip_manual_heading_number(plain)
def _match_standalone_numbered_heading(
lines: list[str],
index: int,
heading_numbering_style: str,
) -> tuple[int, str] | None:
"""Recover a short numbered heading when a model omitted Markdown hashes."""
raw = lines[index]
plain = _strip_inline_markdown(raw).strip()
if not plain or len(plain) > 120 or plain.endswith(("。", "!", "?", ";", ";")):
return None
# Multi-part decimal numbers and 第…章/第…节 are strong heading signals,
# so tolerate models that also omitted the surrounding blank line. More
# ambiguous forms such as 一、 and (一) still require paragraph separation
# to avoid turning ordinary enumerations into headings.
unambiguous = bool(
re.match(r"^\d{1,3}(?:[..]\d{1,3}){1,4}(?:[..、])?", plain)
or re.match(rf"^第\s*[{_CHINESE_NUMBER}\d]+\s*[章节篇部]", plain)
)
if index > 0 and lines[index - 1].strip() and not unambiguous:
return None
level = _heading_level_from_number(plain, heading_numbering_style)
if level is None:
return None
title = _strip_manual_heading_number(plain)
return (level, title) if title else None
def _split_table_row(line: str) -> list[str]:
value = re.sub(r"^\s*\|", "", line)
value = re.sub(r"\|\s*$", "", value)
@@ -271,10 +389,10 @@ def _extract_title(markdown: str, explicit_title: str | None) -> tuple[str | Non
in_fence = not in_fence
continue
if not in_fence:
match = re.match(r"^#\s+(.+)$", line)
if match:
heading = _match_atx_heading(line)
if heading and heading[0] == 1:
del lines[index]
return _strip_inline_markdown(match.group(1)) or (explicit_title or "").strip() or None, lines
return _strip_inline_markdown(heading[1]) or (explicit_title or "").strip() or None, lines
return (explicit_title or "").strip() or None, lines
@@ -332,6 +450,7 @@ def _parse_markdown(
markdown: str,
explicit_title: str | None,
embedded_images: dict[str, str] | None = None,
heading_numbering_style: str = DEFAULT_HEADING_NUMBERING_STYLE,
) -> tuple[str | None, list[Block]]:
title, lines = _extract_title(markdown, explicit_title)
blocks: list[Block] = []
@@ -362,12 +481,33 @@ def _parse_markdown(
index += 1
continue
heading = re.match(r"^(#{1,6})\s+(.+)$", raw)
heading = _match_atx_heading(raw)
if heading:
flush_paragraph()
markdown_level = max(2, len(heading.group(1)))
markdown_level = max(2, heading[0])
section_level = max(0, min(markdown_level - 2, 4))
blocks.append(ParagraphBlock("heading", _strip_manual_heading_number(heading.group(2)), section_level))
numbered_level = _heading_level_from_number(
heading[1],
heading_numbering_style,
allow_ambiguous_single=True,
)
if numbered_level is not None:
section_level = numbered_level
blocks.append(
ParagraphBlock(
"heading",
_clean_heading_text(heading[1], heading_numbering_style, section_level),
section_level,
)
)
index += 1
continue
numbered_heading = _match_standalone_numbered_heading(lines, index, heading_numbering_style)
if numbered_heading:
flush_paragraph()
section_level, heading_text = numbered_heading
blocks.append(ParagraphBlock("heading", heading_text, section_level))
index += 1
continue
@@ -667,7 +807,11 @@ def _table_xml(block: TableBlock, relationships: _DocumentRelationships) -> str:
return f"<w:tbl>{properties}<w:tblGrid>{grid}</w:tblGrid>{''.join(rows)}</w:tbl>"
def _blocks_xml(blocks: list[Block], relationships: _DocumentRelationships) -> str:
def _blocks_xml(
blocks: list[Block],
relationships: _DocumentRelationships,
heading_numbering_style: str,
) -> str:
output: list[str] = []
for block in blocks:
if isinstance(block, ImageBlock):
@@ -689,7 +833,7 @@ def _blocks_xml(blocks: list[Block], relationships: _DocumentRelationships) -> s
_paragraph_xml(
style,
_inline_xml(block.text, relationships, font=font),
numbering_level=block.level,
numbering_level=block.level if heading_numbering_style != "none" else None,
numbering_id=1,
keep_next=True,
)
@@ -910,12 +1054,20 @@ def _styles_xml() -> str:
return f'{_XML_DECL}<w:styles xmlns:w="{_W_NS}">{defaults}{"".join(styles)}</w:styles>'
def _heading_level_xml(level: int, format_value: str, text: str, font: str) -> str:
def _heading_level_xml(
level: int,
format_value: str,
text: str,
font: str,
*,
legal_numbering: bool = False,
) -> str:
restart = f'<w:lvlRestart w:val="{level}"/>' if level > 0 else ""
legal = "<w:isLgl/>" if legal_numbering 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"/>'
f'<w:lvl w:ilvl="{level}"><w:start w:val="1"/><w:numFmt w:val="{format_value}"/>'
f'{restart}{legal}<w:lvlText w:val="{_xml_attr(text)}"/><w:lvlJc w:val="left"/>'
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>'
)
@@ -938,15 +1090,55 @@ def _list_abstract_xml(abstract_id: int, *, ordered: bool) -> str:
return f'<w:abstractNum w:abstractNumId="{abstract_id}"><w:multiLevelType w:val="multilevel"/>{"".join(levels)}</w:abstractNum>'
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(2, "decimal", "%3.", BODY_FONT_FAMILY),
_heading_level_xml(3, "decimal", "(%4)", BODY_FONT_FAMILY),
_heading_level_xml(4, "decimal", "%5)", BODY_FONT_FAMILY),
def _heading_numbering_config(style: str) -> list[tuple[str, str, str, bool]]:
"""Return ``(format, text, font, legal)`` for Word heading levels 1-5."""
if style == "official_chinese":
return [
("chineseCounting", "%1、", HEADING_1_FONT_FAMILY, False),
("chineseCounting", "(%2)", HEADING_2_FONT_FAMILY, False),
("decimal", "%3.", BODY_FONT_FAMILY, False),
("decimal", "(%4)", BODY_FONT_FAMILY, False),
("decimal", "%5)", BODY_FONT_FAMILY, False),
]
if style == "decimal":
return [
("decimal", "%1", HEADING_1_FONT_FAMILY, False),
("decimal", "%1.%2", HEADING_2_FONT_FAMILY, False),
("decimal", "%1.%2.%3", BODY_FONT_FAMILY, False),
("decimal", "%1.%2.%3.%4", BODY_FONT_FAMILY, False),
("decimal", "%1.%2.%3.%4.%5", BODY_FONT_FAMILY, False),
]
if style == "chapter_section":
return [
("chineseCounting", "第%1章", HEADING_1_FONT_FAMILY, False),
("chineseCounting", "第%2节", HEADING_2_FONT_FAMILY, False),
("chineseCounting", "%3、", BODY_FONT_FAMILY, False),
("chineseCounting", "(%4)", BODY_FONT_FAMILY, False),
("decimal", "%5.", BODY_FONT_FAMILY, False),
]
# ``none`` keeps an unused valid heading definition in numbering.xml;
# headings omit numPr entirely in _blocks_xml.
return [
("chineseCounting", "%1、", HEADING_1_FONT_FAMILY, False),
("decimal", "%1.%2", HEADING_2_FONT_FAMILY, True),
("decimal", "%1.%2.%3", BODY_FONT_FAMILY, True),
("decimal", "%1.%2.%3.%4", BODY_FONT_FAMILY, True),
("decimal", "%1.%2.%3.%4.%5", BODY_FONT_FAMILY, True),
]
def _numbering_xml(heading_numbering_style: str = DEFAULT_HEADING_NUMBERING_STYLE) -> str:
heading_levels = "".join(
_heading_level_xml(
level,
format_value,
text,
font,
legal_numbering=legal,
)
for level, (format_value, text, font, legal) in enumerate(
_heading_numbering_config(heading_numbering_style)
)
)
return (
f'{_XML_DECL}<w:numbering xmlns:w="{_W_NS}">'
@@ -1012,7 +1204,12 @@ def _settings_xml() -> str:
)
def _document_xml(title: str | None, blocks: list[Block], relationships: _DocumentRelationships) -> str:
def _document_xml(
title: str | None,
blocks: list[Block],
relationships: _DocumentRelationships,
heading_numbering_style: str,
) -> str:
body: list[str] = []
if title:
body.append(
@@ -1023,7 +1220,7 @@ def _document_xml(title: str | None, blocks: list[Block], relationships: _Docume
alignment="center",
)
)
body.append(_blocks_xml(blocks, relationships))
body.append(_blocks_xml(blocks, relationships, heading_numbering_style))
section = (
'<w:sectPr><w:footerReference w:type="default" r:id="rId5"/>'
'<w:footerReference w:type="even" r:id="rId6"/>'
@@ -1097,6 +1294,7 @@ def build_markdown_docx(
*,
font_path: Path | None = None,
embedded_images: dict[str, str] | None = None,
heading_numbering_style: str = DEFAULT_HEADING_NUMBERING_STYLE,
) -> bytes:
"""Build a formal DOCX and embed the validated server-side title font."""
if not markdown or not markdown.strip():
@@ -1105,11 +1303,18 @@ def build_markdown_docx(
raise WordExportError("文档内容超过 200 万字符,无法导出。")
if embedded_images and len(embedded_images) > _MAX_EMBEDDED_IMAGES:
raise WordExportError("嵌入图片数量超过限制。")
if heading_numbering_style not in HEADING_NUMBERING_STYLES:
raise WordExportError("未知的 Word 标题编号格式。")
font = load_title_font(font_path)
resolved_title, blocks = _parse_markdown(markdown, title, embedded_images)
resolved_title, blocks = _parse_markdown(
markdown,
title,
embedded_images,
heading_numbering_style,
)
relationships = _DocumentRelationships()
document_xml = _document_xml(resolved_title, blocks, relationships)
document_xml = _document_xml(resolved_title, blocks, relationships, heading_numbering_style)
font_key = str(uuid.uuid4()).upper()
media_files = {
f"word/media/{block.media_name}": block.data for block in blocks if isinstance(block, ImageBlock)
@@ -1125,7 +1330,7 @@ def build_markdown_docx(
"word/document.xml": document_xml,
"word/_rels/document.xml.rels": relationships.xml(),
"word/styles.xml": _styles_xml(),
"word/numbering.xml": _numbering_xml(),
"word/numbering.xml": _numbering_xml(heading_numbering_style),
"word/settings.xml": _settings_xml(),
"word/fontTable.xml": _font_table_xml(font_key),
"word/_rels/fontTable.xml.rels": _font_table_relationships_xml(),
@@ -1143,6 +1348,8 @@ def build_markdown_docx(
__all__ = [
"DOCX_MEDIA_TYPE",
"DEFAULT_HEADING_NUMBERING_STYLE",
"HEADING_NUMBERING_STYLES",
"TITLE_FONT_FAMILY",
"WordExportError",
"WordExportFontError",