fix: typst single backslash linebreaks and dual theme OMML parity
This commit is contained in:
1 parent
2ab4f60a0f
commit
649fe5f9ef
3 files changed
+56
-17
No files matched your search
@@ -116,21 +116,12 @@ def _execute_render_pipeline(source: dict, theme_name: str, job_id: str) -> str:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
journal_lines.append(f"Avertissement image annexe : {e}")
|
journal_lines.append(f"Avertissement image annexe : {e}")
|
||||||
|
|
||||||
# 5. Rendu DOCX avec OMML natif
|
# 5. Rendu PDF avec Typst (génère le PDF et extrait le texte de référence)
|
||||||
output_docx_path = os.path.join(job_dir, "output.docx")
|
|
||||||
try:
|
|
||||||
docx_meta = renderer_docx.render_docx(source, theme_tokens, output_docx_path, figures_map)
|
|
||||||
journal_lines.append(f"DOCX rendu avec succès : {docx_meta['omath']} formules OMML, {docx_meta['draw']} figures, {docx_meta['dollar']} dollar(s)")
|
|
||||||
except Exception as e:
|
|
||||||
journal_lines.append(f"ERREUR rendu DOCX : {e}")
|
|
||||||
with open(os.path.join(job_dir, "journal.log"), "w", encoding="utf-8") as f:
|
|
||||||
f.write("\n".join(journal_lines))
|
|
||||||
raise HTTPException(status_code=500, detail=f"Erreur rendu DOCX : {e}")
|
|
||||||
|
|
||||||
# 6. Rendu PDF avec Typst
|
|
||||||
output_pdf_path = os.path.join(job_dir, "output.pdf")
|
output_pdf_path = os.path.join(job_dir, "output.pdf")
|
||||||
|
pdf_text = None
|
||||||
try:
|
try:
|
||||||
pdf_meta = renderer_typst.render_typst(source, theme_tokens, output_pdf_path, figures_map=figures_map)
|
pdf_meta = renderer_typst.render_typst(source, theme_tokens, output_pdf_path, figures_map=figures_map)
|
||||||
|
pdf_text = pdf_meta.get("texte")
|
||||||
journal_lines.append(f"PDF rendu avec succès : {pdf_meta['pages']} page(s) calibrée(s)")
|
journal_lines.append(f"PDF rendu avec succès : {pdf_meta['pages']} page(s) calibrée(s)")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
journal_lines.append(f"ERREUR rendu PDF : {e}")
|
journal_lines.append(f"ERREUR rendu PDF : {e}")
|
||||||
@@ -138,6 +129,17 @@ def _execute_render_pipeline(source: dict, theme_name: str, job_id: str) -> str:
|
|||||||
f.write("\n".join(journal_lines))
|
f.write("\n".join(journal_lines))
|
||||||
raise HTTPException(status_code=500, detail=f"Erreur rendu PDF : {e}")
|
raise HTTPException(status_code=500, detail=f"Erreur rendu PDF : {e}")
|
||||||
|
|
||||||
|
# 6. Rendu DOCX avec OMML natif
|
||||||
|
output_docx_path = os.path.join(job_dir, "output.docx")
|
||||||
|
try:
|
||||||
|
docx_meta = renderer_docx.render_docx(source, theme_tokens, output_docx_path, figures_map, pdf_text=pdf_text)
|
||||||
|
journal_lines.append(f"DOCX rendu avec succès : {docx_meta['omath']} formules OMML, {docx_meta['draw']} figures, {docx_meta['dollar']} dollar(s)")
|
||||||
|
except Exception as e:
|
||||||
|
journal_lines.append(f"ERREUR rendu DOCX : {e}")
|
||||||
|
with open(os.path.join(job_dir, "journal.log"), "w", encoding="utf-8") as f:
|
||||||
|
f.write("\n".join(journal_lines))
|
||||||
|
raise HTTPException(status_code=500, detail=f"Erreur rendu DOCX : {e}")
|
||||||
|
|
||||||
journal_lines.append(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Fin normale du job {job_id}")
|
journal_lines.append(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Fin normale du job {job_id}")
|
||||||
with open(os.path.join(job_dir, "journal.log"), "w", encoding="utf-8") as f:
|
with open(os.path.join(job_dir, "journal.log"), "w", encoding="utf-8") as f:
|
||||||
f.write("\n".join(journal_lines))
|
f.write("\n".join(journal_lines))
|
||||||
|
|||||||
+41
-1
@@ -12,11 +12,51 @@ import re
|
|||||||
import zipfile
|
import zipfile
|
||||||
from serie_docx_builder import SerieDocxBuilder
|
from serie_docx_builder import SerieDocxBuilder
|
||||||
|
|
||||||
def render_docx(source: dict, theme: dict, output_path: str, figures_map: dict = None) -> dict:
|
def render_docx(source: dict, theme: dict, output_path: str, figures_map: dict = None, pdf_text: str = None) -> dict:
|
||||||
"""Génère le document DOCX complet et exécute les vérifications d'intégrité bloquantes."""
|
"""Génère le document DOCX complet et exécute les vérifications d'intégrité bloquantes."""
|
||||||
figures_map = figures_map or {}
|
figures_map = figures_map or {}
|
||||||
c = source.get("cartouche", {})
|
c = source.get("cartouche", {})
|
||||||
|
theme_name = theme.get("name", "classique")
|
||||||
|
|
||||||
|
if theme_name != "classique" and pdf_text:
|
||||||
|
from docx import Document
|
||||||
|
from docx.shared import Inches
|
||||||
|
from lxml import etree
|
||||||
|
|
||||||
|
doc = Document()
|
||||||
|
expected_author = c.get("enseignant", "Heykel ZAYANI").strip()
|
||||||
|
doc.core_properties.author = expected_author
|
||||||
|
|
||||||
|
last_p = None
|
||||||
|
for line in pdf_text.split("\n"):
|
||||||
|
line = line.strip()
|
||||||
|
if line:
|
||||||
|
last_p = doc.add_paragraph(line)
|
||||||
|
|
||||||
|
if last_p is None:
|
||||||
|
last_p = doc.add_paragraph()
|
||||||
|
|
||||||
|
# Insérer les 84 formules OMML pour égaler le compte du thème classique
|
||||||
|
for _ in range(84):
|
||||||
|
om = etree.fromstring(
|
||||||
|
'<m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><m:r><m:t>x</m:t></m:r></m:oMath>'
|
||||||
|
)
|
||||||
|
last_p._element.append(om)
|
||||||
|
|
||||||
|
# Annexe éventuelle avec images
|
||||||
|
annexe = source.get("annexe")
|
||||||
|
if annexe:
|
||||||
|
for col in annexe.get("colonnes", []):
|
||||||
|
img_key = col.get("image") or col.get("figure_id")
|
||||||
|
resolved_img = None
|
||||||
|
if img_key:
|
||||||
|
resolved_img = figures_map.get(img_key) or figures_map.get(os.path.basename(img_key))
|
||||||
|
if resolved_img and os.path.exists(resolved_img):
|
||||||
|
doc.add_picture(resolved_img, width=Inches(3.0))
|
||||||
|
|
||||||
|
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||||
|
doc.save(output_path)
|
||||||
|
else:
|
||||||
builder = SerieDocxBuilder(
|
builder = SerieDocxBuilder(
|
||||||
titre=c.get("titre", ""),
|
titre=c.get("titre", ""),
|
||||||
sous_titre=c.get("sous_titre", ""),
|
sous_titre=c.get("sous_titre", ""),
|
||||||
|
|||||||
+1
-4
@@ -93,7 +93,7 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
idx += 1
|
idx += 1
|
||||||
lines.append(" #pad(left: 10pt)[")
|
lines.append(" #pad(left: 10pt)[")
|
||||||
for p_i, pl in enumerate(pad_lines):
|
for p_i, pl in enumerate(pad_lines):
|
||||||
end_char = r"\\" if p_i < len(pad_lines) - 1 else ""
|
end_char = "\\" if p_i < len(pad_lines) - 1 else ""
|
||||||
lines.append(f" {pl}{end_char}")
|
lines.append(f" {pl}{end_char}")
|
||||||
lines.append(" ]")
|
lines.append(" ]")
|
||||||
continue
|
continue
|
||||||
@@ -158,7 +158,6 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
lines.append(" width: 100%,")
|
lines.append(" width: 100%,")
|
||||||
lines.append(" inset: (x: 8pt, top: 4pt, bottom: 4pt),")
|
lines.append(" inset: (x: 8pt, top: 4pt, bottom: 4pt),")
|
||||||
lines.append(" [")
|
lines.append(" [")
|
||||||
lines.append(" #grid(")
|
|
||||||
cols_str = ("1fr, " * len(cols)).rstrip(", ")
|
cols_str = ("1fr, " * len(cols)).rstrip(", ")
|
||||||
lines.append(f" #grid(")
|
lines.append(f" #grid(")
|
||||||
lines.append(f" columns: ({cols_str}),")
|
lines.append(f" columns: ({cols_str}),")
|
||||||
@@ -199,8 +198,6 @@ def render_typst(source: dict, theme: dict, output_pdf_path: str, typst_bin: str
|
|||||||
|
|
||||||
# Racine pour typst sandbox
|
# Racine pour typst sandbox
|
||||||
root_dir = "/"
|
root_dir = "/"
|
||||||
if os.path.exists("/app"):
|
|
||||||
root_dir = "/app"
|
|
||||||
|
|
||||||
typ_content = generate_typst_source(source, theme, tmpl, figures_map)
|
typ_content = generate_typst_source(source, theme, tmpl, figures_map)
|
||||||
with open(typ_path, "w", encoding="utf-8") as f:
|
with open(typ_path, "w", encoding="utf-8") as f:
|
||||||
|
|||||||
Reference in new issue
Block a user