2025-12-08 16:57:25 +03:00
|
|
|
from pathlib import Path
|
2025-12-08 17:18:30 +03:00
|
|
|
from typing import Optional
|
|
|
|
|
|
2025-12-08 16:57:25 +03:00
|
|
|
from docx import Document
|
|
|
|
|
from .style_registry import StyleRegistry
|
|
|
|
|
from .renderer import render_markdown_to_docx
|
|
|
|
|
|
2025-12-08 17:18:30 +03:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2025-12-08 16:57:25 +03:00
|
|
|
|
2025-12-08 17:18:30 +03:00
|
|
|
def process_document(input_dir: Path, output_dir: Path, style_config_path: Path, template_path: Optional[Path] = None):
|
2025-12-08 16:57:25 +03:00
|
|
|
for doc_dir in input_dir.iterdir():
|
|
|
|
|
if not doc_dir.is_dir():
|
|
|
|
|
continue
|
2025-12-08 17:18:30 +03:00
|
|
|
# Пропускаем папку images
|
|
|
|
|
if doc_dir.name == "images":
|
|
|
|
|
continue
|
|
|
|
|
|
2025-12-08 16:57:25 +03:00
|
|
|
doc_name = doc_dir.name
|
|
|
|
|
md_path = doc_dir / "document.md"
|
|
|
|
|
images_dir = doc_dir / "images"
|
|
|
|
|
|
|
|
|
|
if not md_path.exists():
|
2025-12-08 17:18:30 +03:00
|
|
|
logger.warning(f"Пропущено: {doc_name} — нет document.md")
|
2025-12-08 16:57:25 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
with open(md_path, encoding="utf-8") as f:
|
|
|
|
|
md_content = f.read()
|
|
|
|
|
|
2025-12-08 17:18:30 +03:00
|
|
|
if template_path and template_path.exists():
|
|
|
|
|
doc = Document(template_path)
|
|
|
|
|
else:
|
|
|
|
|
doc = Document()
|
|
|
|
|
|
|
|
|
|
style_reg = StyleRegistry(style_config_path)
|
|
|
|
|
style_reg.ensure_styles_in_doc(doc)
|
2025-12-08 16:57:25 +03:00
|
|
|
render_markdown_to_docx(doc, md_content, images_dir, style_reg)
|
|
|
|
|
|
|
|
|
|
output_path = output_dir / f"{doc_name}.docx"
|
|
|
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
doc.save(output_path)
|
2025-12-08 17:18:30 +03:00
|
|
|
logger.info(f"Создан документ: {output_path}")
|