feat(md2doc): recursive list renderer with manual numbering
This commit is contained in:
@@ -323,6 +323,48 @@ def _render_paragraph(doc, node, style: dict):
|
||||
_add_runs(node)
|
||||
|
||||
|
||||
def _render_list(doc, node, style: dict, level: int = 0, num_state: dict = None):
|
||||
"""Render <ul>/<ol> recursively. Uses register_multilevel_numbering + attach_list_numbering.
|
||||
|
||||
`num_state` carries the registered numId across recursion (one numbering definition
|
||||
per top-level list, all children share it).
|
||||
|
||||
Spec §4.2: mandatory manual numbering — List Bullet 2/3 styles don't work.
|
||||
"""
|
||||
is_ordered = node.name == "ol"
|
||||
kind = "decimal" if is_ordered else "bullet"
|
||||
|
||||
# Top-level call: register fresh numbering definition
|
||||
if num_state is None:
|
||||
num_id = register_multilevel_numbering(doc, levels=3, kind=kind)
|
||||
num_state = {"num_id": num_id}
|
||||
|
||||
for li in node.find_all("li", recursive=False):
|
||||
# The li's direct text (excluding nested lists)
|
||||
nested_lists = []
|
||||
for child in li.children:
|
||||
if hasattr(child, "name") and child.name in ("ul", "ol"):
|
||||
nested_lists.append(child)
|
||||
|
||||
# Get text content of the li (excluding nested list text)
|
||||
text_parts = []
|
||||
for child in li.children:
|
||||
if hasattr(child, "name") and child.name in ("ul", "ol"):
|
||||
continue
|
||||
if isinstance(child, str):
|
||||
text_parts.append(child.strip())
|
||||
else:
|
||||
text_parts.append(child.get_text(strip=True))
|
||||
text = " ".join(p for p in text_parts if p)
|
||||
|
||||
p = doc.add_paragraph(text)
|
||||
attach_list_numbering(p, num_id=num_state["num_id"], ilvl=level)
|
||||
|
||||
# Recurse into nested lists
|
||||
for nested in nested_lists:
|
||||
_render_list(doc, nested, style=style, level=level + 1, num_state=num_state)
|
||||
|
||||
|
||||
def convert_md_to_doc(md_file: Path, output_file: Path, style: str = "elegant") -> Path:
|
||||
"""Convert a single markdown file to DOCX.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user