"""Assemble LaTeX project from markdown (pipeline 1: MD → LaTeX).""" from __future__ import annotations import shutil from pathlib import Path from .emitter import emit_document def package_template_dir() -> Path: return Path(__file__).resolve().parent.parent / "latex" / "mirea" def convert_md_to_latex_project( md_path: str | Path, out_dir: str | Path, *, titlepage: str | Path | None = None, ) -> Path: """ Copy MIREA template into out_dir, write content.tex from markdown. Returns path to main.tex """ md_path = Path(md_path).resolve() out_dir = Path(out_dir).resolve() tpl = package_template_dir() if not tpl.is_dir(): raise FileNotFoundError(f"LaTeX template not found: {tpl}") out_dir.mkdir(parents=True, exist_ok=True) # sync template files (keep user Images/content if regenerating) for item in tpl.iterdir(): dest = out_dir / item.name if item.name == "content.tex": continue if item.is_dir(): if dest.exists(): # merge Settings etc. for sub in item.rglob("*"): if sub.is_file(): rel = sub.relative_to(item) target = dest / rel target.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(sub, target) else: shutil.copytree(item, dest) else: shutil.copy2(item, dest) images_dir = out_dir / "Images" images_dir.mkdir(exist_ok=True) md_text = md_path.read_text(encoding="utf-8") body = emit_document( md_text, work_dir=md_path.parent, images_dir=images_dir, ) (out_dir / "content.tex").write_text(body, encoding="utf-8") if titlepage: tp = Path(titlepage) if tp.is_file(): shutil.copy2(tp, out_dir / "titlepage.pdf") return out_dir / "main.tex"