feat(series-studio): initial implementation of unified series studio engine (PDF & DOCX)
This commit is contained in:
commit
03d9922066
20 files changed
+7082
No files matched your search
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
renderer_docx.py
|
||||
Moteur de rendu Word (.docx) natif OMML depuis la description JSON d'une série.
|
||||
Applique le thème graphique et effectue les contrôles bloquants post-génération.
|
||||
"""
|
||||
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import zipfile
|
||||
from serie_docx_builder import SerieDocxBuilder
|
||||
|
||||
def render_docx(source: dict, theme: dict, output_path: str, figures_map: dict = None) -> dict:
|
||||
"""Génère le document DOCX complet et exécute les vérifications d'intégrité bloquantes."""
|
||||
figures_map = figures_map or {}
|
||||
c = source.get("cartouche", {})
|
||||
|
||||
builder = SerieDocxBuilder(
|
||||
titre=c.get("titre", ""),
|
||||
sous_titre=c.get("sous_titre", ""),
|
||||
discipline=c.get("discipline", "Mathématiques"),
|
||||
classe=c.get("classe", "4ème Sciences Techniques"),
|
||||
enseignant=c.get("enseignant", "Heykel ZAYANI"),
|
||||
annee=c.get("annee", "2026 / 2027"),
|
||||
lycee=c.get("lycee", "Lycée Pilote"),
|
||||
titre_enseignant=c.get("titre_enseignant", "P.P.É.C.E."),
|
||||
type_doc=c.get("type_doc", "SÉRIE D'EXERCICES"),
|
||||
theme=theme
|
||||
)
|
||||
|
||||
for ex in source.get("exercices", []):
|
||||
num_str = str(ex.get("numero", ""))
|
||||
titre_str = ex.get("titre")
|
||||
points_str = ex.get("points")
|
||||
builder.add_exercise_banner(num_str, titre_str, points_str)
|
||||
|
||||
for el in ex.get("elements", []):
|
||||
el_type = el.get("type")
|
||||
sb = el.get("space_before", 2)
|
||||
sa = el.get("space_after", 2)
|
||||
indent = el.get("indent_level", 0)
|
||||
|
||||
if el_type == "question":
|
||||
text = el.get("text_docx") or el.get("text", "")
|
||||
builder.add_question(text, indent_level=indent, space_before=sb, space_after=sa)
|
||||
|
||||
elif el_type == "display_equation":
|
||||
latex = el.get("latex", "")
|
||||
builder.add_display_equation(latex, space_before=sb, space_after=sa)
|
||||
|
||||
elif el_type == "part_header":
|
||||
prefix = el.get("bold_prefix", "")
|
||||
text = el.get("text_docx") or el.get("text", "")
|
||||
builder.add_part_header(prefix, text, space_before=sb, space_after=sa)
|
||||
|
||||
elif el_type == "page_break":
|
||||
builder.add_page_break()
|
||||
|
||||
# Annexe éventuelle
|
||||
annexe = source.get("annexe")
|
||||
if annexe:
|
||||
cols = []
|
||||
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))
|
||||
cols.append({
|
||||
"titre": col.get("titre", ""),
|
||||
"image_path": resolved_img,
|
||||
"width_docx_inches": col.get("width_docx_inches", 3.2)
|
||||
})
|
||||
builder.add_annexe_table(annexe.get("titre", "ANNEXE"), cols)
|
||||
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
builder.save(output_path)
|
||||
|
||||
# =========================================================================
|
||||
# CONTRÔLES BLOQUANTS POST-GÉNÉRATION DOCX (T3)
|
||||
# =========================================================================
|
||||
with open(output_path, "rb") as f:
|
||||
docx_bytes = f.read()
|
||||
|
||||
z = zipfile.ZipFile(io.BytesIO(docx_bytes))
|
||||
doc_xml = z.read("word/document.xml").decode("utf-8")
|
||||
|
||||
# 1. Aucun $ ni LaTeX brut dans le XML
|
||||
dollars = doc_xml.count("$")
|
||||
if dollars > 0:
|
||||
raise ValueError(f"Contrôle bloquant échoué : {dollars} signe(s) '$' résiduel(s) dans word/document.xml")
|
||||
|
||||
# 2. Formules OMML
|
||||
omath_count = len(re.findall(r"<m:oMath[ >]", doc_xml))
|
||||
if omath_count == 0:
|
||||
raise ValueError("Contrôle bloquant échoué : aucune formule OMML <m:oMath> générée dans le DOCX")
|
||||
|
||||
# 3. Dessins
|
||||
draw_count = len(re.findall(r"<w:drawing", doc_xml))
|
||||
|
||||
# 4. Auteur dans docProps/core.xml
|
||||
core_xml = z.read("docProps/core.xml").decode("utf-8")
|
||||
m_author = re.search(r"<dc:creator>([^<]*)</dc:creator>", core_xml)
|
||||
author = m_author.group(1) if m_author else ""
|
||||
expected_author = c.get("enseignant", "").strip()
|
||||
if author.lower() == "python-docx" or author.lower() != expected_author.lower():
|
||||
raise ValueError(f"Contrôle bloquant échoué : auteur DOCX = {author!r}, attendu {expected_author!r}")
|
||||
|
||||
brut = re.sub(r"<m:oMath[ >].*?</m:oMath>", " ", doc_xml, flags=re.S)
|
||||
extracted_text = " ".join(re.findall(r"<w:t[^>]*>([^<]*)</w:t>", brut))
|
||||
|
||||
return {
|
||||
"omath": omath_count,
|
||||
"draw": draw_count,
|
||||
"dollar": dollars,
|
||||
"auteur": author,
|
||||
"texte": extracted_text
|
||||
}
|
||||
Reference in new issue
Block a user