39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
#!/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)
|