fix: preserve list context and nested CSS decoration behavior
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -15,7 +16,7 @@ from markdown_it import MarkdownIt
|
||||
from markdown_it.token import Token
|
||||
|
||||
from .markdown_compat import cjk_strong_plugin
|
||||
from .docx_inline import source_style_name, LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE, SOURCE_PARAGRAPH_STYLES, CONTINUATION, INNER_QUOTE_STYLE
|
||||
from .docx_inline import source_style_name, LIST_PARAGRAPH_STYLE, QUOTED_LIST_PARAGRAPH_STYLE, SOURCE_PARAGRAPH_STYLES, CONTINUATION, INNER_QUOTE_STYLE, LIST_TARGET
|
||||
|
||||
|
||||
WP_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||
@@ -256,6 +257,7 @@ def _add_python_docx_paragraph(document: object, block: ParagraphBlock) -> None:
|
||||
style = name
|
||||
paragraph = document.add_paragraph(style=style)
|
||||
if block.continuation: paragraph._p.set(CONTINUATION, '1')
|
||||
if block.list_target: paragraph._p.set(LIST_TARGET, block.list_target)
|
||||
if block.list_kind:
|
||||
from docx.oxml import parse_xml
|
||||
paragraph._p.get_or_add_pPr().append(parse_xml(f'<w:numPr xmlns:w="{WP_NS}"><w:ilvl w:val="{block.list_level}"/><w:numId w:val="{block.list_id}"/></w:numPr>'))
|
||||
@@ -336,6 +338,7 @@ class ParagraphBlock:
|
||||
alignment: str = ''
|
||||
continuation: bool = False
|
||||
inner_quote: bool = False
|
||||
list_target: str = ''
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -354,6 +357,7 @@ class _DocxBuilder:
|
||||
self.list_starts: list[int] = []
|
||||
self.list_sequence = 1000
|
||||
self.item_pending: list[bool] = []
|
||||
self.item_targets: list[str] = []
|
||||
self.list_quote_depths: list[int] = []
|
||||
self.quote_depth = 0
|
||||
|
||||
@@ -373,8 +377,10 @@ class _DocxBuilder:
|
||||
self.blocks.append(ParagraphBlock([(line, {"code": True})], style="Code"))
|
||||
elif token.type == 'list_item_open':
|
||||
self.item_pending.append(True)
|
||||
self.item_targets.append('list_ordered' if self.list_stack[-1] == 'number' else 'list_bullet')
|
||||
elif token.type == 'list_item_close':
|
||||
self.item_pending.pop()
|
||||
self.item_targets.pop()
|
||||
elif token.type == "blockquote_open":
|
||||
self.quote_depth += 1
|
||||
elif token.type == "blockquote_close":
|
||||
@@ -401,10 +407,13 @@ class _DocxBuilder:
|
||||
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)
|
||||
if list_kind and self.item_pending[-1] and inline is not None:
|
||||
if re.match(r'^\[[ xX]\]\s', inline.content): self.item_targets[-1] = 'list_task'
|
||||
self.blocks.append(ParagraphBlock(runs, style=style, list_kind=list_kind, quote_depth=self.quote_depth,
|
||||
list_level=min(8, max(0, len(self.list_stack) - 1)), list_id=self.list_ids[-1] if self.list_ids else 0,
|
||||
list_start=self.list_starts[-1] if self.list_starts else 1, loose=not self.tokens[self.index].hidden,
|
||||
continuation=bool(list_kind and self.item_pending and not self.item_pending[-1]),
|
||||
list_target=self.item_targets[-1] if list_kind else '',
|
||||
inner_quote=bool(self.list_quote_depths and self.quote_depth > self.list_quote_depths[-1])))
|
||||
if self.item_pending: self.item_pending[-1] = False
|
||||
self.index += 3
|
||||
@@ -512,7 +521,9 @@ def _paragraph_xml(block: ParagraphBlock) -> str:
|
||||
props.append('<w:ind w:left="720"/>')
|
||||
if block.alignment: props.append(f'<w:jc w:val="{block.alignment}"/>')
|
||||
ppr = f"<w:pPr>{''.join(props)}</w:pPr>" if props else ""
|
||||
marker = ' xmlns:mdh="urn:mdhelper:source" mdh:continuation="1"' if block.continuation else ''
|
||||
marker = ' xmlns:mdh="urn:mdhelper:source"' if block.continuation or block.list_target else ''
|
||||
if block.continuation: marker += ' mdh:continuation="1"'
|
||||
if block.list_target: marker += f' mdh:list-target="{block.list_target}"'
|
||||
return f"<w:p{marker}>{ppr}{''.join(_run_xml(text, style) for text, style in block.runs)}</w:p>"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user