feat(md2doc): copy _bulletize helper from md2pdf (spec §3.3)

This commit is contained in:
2026-07-28 10:21:33 +03:00
parent 2a2c0ed249
commit 0b344d3bb8
2 changed files with 68 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""
Convert markdown files to DOCX (Microsoft Word).
Usage:
md2doc.py input.md # Creates input.docx in same directory
md2doc.py input.md -o output.docx # Specify output file
md2doc.py docs/ # Convert all .md files in directory
md2doc.py docs/ -o docx_output/
md2doc.py input.md --style mono # Use monospace style
Requires: pip install python-docx markdown beautifulsoup4 lxml
"""
import argparse
import sys
from pathlib import Path
def _bulletize(text: str) -> str:
"""Turn markdown "- " list markers into bullets, skipping code/HTML blocks.
Copied verbatim from md2pdf.py per spec §3.3.
"""
import re
out, in_fence, in_html = [], False, False
for line in text.split("\n"):
stripped = line.lstrip()
if stripped.startswith("```") or stripped.startswith("~~~"):
in_fence = not in_fence
elif not in_fence:
if re.match(r"^<(table|div|section|figure)\b", stripped, re.I):
in_html = True
elif re.match(r"^</(table|div|section|figure)>", stripped, re.I):
in_html = False
if not in_fence and not in_html and line.startswith("- "):
line = "" + line[2:]
out.append(line)
return "\n".join(out)
+30
View File
@@ -0,0 +1,30 @@
"""Tests for _bulletize — verifies behavior copied from md2pdf.py."""
from md2doc import _bulletize
def test_simple_dash_list_becomes_bullets():
text = "- item A\n- item B\n"
result = _bulletize(text)
assert "• item A" in result
assert "• item B" in result
def test_code_block_dashes_preserved():
"""Dashes inside ``` blocks must NOT be bulletized."""
text = "```bash\n-r flag\n--verbose\n```\n"
result = _bulletize(text)
assert "-r flag" in result
assert "--verbose" in result
def test_html_block_dashes_preserved():
"""Dashes inside raw HTML blocks (table/div/etc.) must NOT be bulletized."""
text = "<table>\n- not a list\n</table>\n"
result = _bulletize(text)
assert "- not a list" in result
def test_tilde_fence_also_preserved():
text = "~~~bash\n-x\n~~~\n"
result = _bulletize(text)
assert "-x" in result