fix: preserve nested Word structures and synchronize valid font families

This commit is contained in:
2026-11-05 21:54:52 +08:00
parent 0e072eed3b
commit 7f81c2015e
12 changed files with 292 additions and 28 deletions
+17 -4
View File
@@ -248,7 +248,10 @@ def _pandoc_list_tables(source: str) -> str:
if match:
prefix, marker, spacing = match.groups()
indent = prefix + ' ' * (len(marker) + len(spacing))
lines[row] = match.group() + '\n' + prefix + '\n' + indent + lines[row][match.end():]
# A bare '-' after parent text is a Setext heading underline.
# '+' is the same bullet without changing the parent's tightness.
marker = '+' if marker == '-' else marker
lines[row] = prefix + marker + spacing + '\n' + prefix + '\n' + indent + lines[row][match.end():]
return ''.join(lines)
@@ -324,6 +327,7 @@ def _add_python_docx_table(document: object, block: TableBlock) -> None:
if block.list_target:
table._tbl.set(LIST_TARGET, block.list_target)
table._tbl.set(LIST_LEVEL, str(block.list_level))
if block.quote_depth: table._tbl.set(QUOTE_DEPTH, str(block.quote_depth))
table.style = "Table Grid"
for row_index, row in enumerate(block.rows):
for col_index in range(column_count):
@@ -371,6 +375,7 @@ class TableBlock:
alignments: list[str] = field(default_factory=list)
list_target: str = ''
list_level: int = 0
quote_depth: int = 0
class _DocxBuilder:
@@ -415,6 +420,8 @@ class _DocxBuilder:
elif token.type == "blockquote_close":
self.quote_depth = max(0, self.quote_depth - 1)
elif token.type in ('bullet_list_open', 'ordered_list_open'):
if self.item_pending and self.item_pending[-1]:
self._append_list_marker()
self.list_stack.append('bullet' if token.type == 'bullet_list_open' else 'number')
self.list_sequence += 1
self.list_ids.append(self.list_sequence)
@@ -427,7 +434,7 @@ class _DocxBuilder:
self.list_quote_depths.pop()
elif token.type == "table_open":
if self.item_pending and self.item_pending[-1]:
self._append_paragraph([], 'Normal', self.list_stack[-1])
self._append_list_marker()
self.blocks.append(self._collect_table())
continue
elif token.type == "hr":
@@ -435,6 +442,12 @@ class _DocxBuilder:
self.index += 1
return self.blocks
def _append_list_marker(self) -> None:
self._append_paragraph([], 'Normal', self.list_stack[-1])
# The marker belongs to the list item, outside its leading quote/table.
self.blocks[-1].quote_depth = self.list_quote_depths[-1]
self.blocks[-1].inner_quote = False
def _add_next_inline(self, style: str, list_kind: str = "") -> None:
inline = self.tokens[self.index + 1] if self.index + 1 < len(self.tokens) else None
runs = _inline_runs(inline.children if inline is not None else None)
@@ -474,7 +487,7 @@ class _DocxBuilder:
self.index += 1
break
self.index += 1
return TableBlock(rows, alignments, self.item_targets[-1] if self.item_targets else '', max(0, len(self.list_stack)-1))
return TableBlock(rows, alignments, self.item_targets[-1] if self.item_targets else '', max(0, len(self.list_stack)-1), self.quote_depth)
def _inline_runs(children: list[Token] | None) -> list[tuple[str, dict[str, object]]]:
@@ -599,7 +612,7 @@ def _numbering_xml(blocks):
def _table_xml(block: TableBlock) -> str:
marker = f' xmlns:mdh="urn:mdhelper:source" mdh:list-target="{block.list_target}" mdh:list-level="{block.list_level}"' if block.list_target else ''
marker = f' xmlns:mdh="urn:mdhelper:source" mdh:list-target="{block.list_target}" mdh:list-level="{block.list_level}" mdh:quote-depth="{block.quote_depth}"' if block.list_target or block.quote_depth else ''
rows = []
for row in block.rows:
cells = []