feat(series-studio): support type_doc (corrige, resume), 4 light themes, chemistry reactions and serie 05 (perso-2026-11-013)
This commit is contained in:
1 parent
b0836f73a0
commit
8f43363848
15 files changed
+2067
-128
No files matched your search
+196
-20
@@ -3,7 +3,8 @@
|
||||
"""
|
||||
renderer_typst.py
|
||||
Génération du fichier Typst et compilation vers PDF de haute fidélité.
|
||||
Effectue les contrôles bloquants : compilation sans avertissement et nombre de pages conforme à pages_cibles.
|
||||
Prend en charge les types (série, corrigé, résumé), les éléments scientifiques,
|
||||
et effectue les contrôles bloquants : compilation sans avertissement et nombre de pages.
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -12,7 +13,7 @@ import subprocess
|
||||
import fitz
|
||||
|
||||
def is_numbered_start(t: str) -> bool:
|
||||
"""Détecte si le texte commence par une numérotation principale de question ou partie (ex: 1), *1)*, I°), *II°)*)."""
|
||||
"""Détecte si le texte commence par une numérotation principale de question ou partie."""
|
||||
s = t.strip()
|
||||
return bool(re.match(r"^(\*?\d+\)|\d+\)|\*?[IVXLCDM]+[°\)]|[IVXLCDM]+[°\)])", s))
|
||||
|
||||
@@ -27,6 +28,7 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
||||
col_bg_subtle = theme.get("bg_subtle", "#f8fafc")
|
||||
col_border = theme.get("border", "#cbd5e1")
|
||||
font_main = theme.get("font_typst", "New Computer Modern")
|
||||
theme_name = theme.get("name", "classique")
|
||||
|
||||
lines = []
|
||||
lines.append(f'#import "{template_import_path}": *')
|
||||
@@ -64,8 +66,30 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
||||
lines.append("#set par(leading: 0.52em)")
|
||||
lines.append("")
|
||||
|
||||
preamble = source.get("preamble")
|
||||
if preamble:
|
||||
lines.append("#v(-4pt)")
|
||||
lines.append("#align(center)[")
|
||||
lines.append(f' #text(size: 8.5pt, style: "italic", fill: rgb("#475569"))[{preamble}]')
|
||||
lines.append("]")
|
||||
lines.append("#v(-2pt)")
|
||||
lines.append("")
|
||||
|
||||
exercices = source.get("exercices", [])
|
||||
for ex_idx, ex in enumerate(exercices):
|
||||
sec_hdr = ex.get("section_header")
|
||||
if sec_hdr:
|
||||
lines.append("#align(center)[")
|
||||
lines.append(" #block(")
|
||||
lines.append(f' fill: rgb("{col_primary}"),')
|
||||
lines.append(" inset: (x: 16pt, y: 3.5pt),")
|
||||
lines.append(" radius: 3pt,")
|
||||
lines.append(f' text(fill: white, weight: "bold", size: 10pt)[{sec_hdr}]')
|
||||
lines.append(" )")
|
||||
lines.append("]")
|
||||
lines.append("#v(-2pt)")
|
||||
lines.append("")
|
||||
|
||||
num = str(ex.get("numero", "1"))
|
||||
titre = ex.get("titre")
|
||||
titre_arg = f'titre: "{titre}"' if titre else 'titre: none'
|
||||
@@ -76,7 +100,8 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
||||
lines.append(f'// EXERCICE {num}')
|
||||
lines.append(f'#exercice("{num}", {titre_arg}, {points_arg}, breakable_box: {breakable},')
|
||||
lines.append(f' color_primary: rgb("{col_primary}"), color_secondary: rgb("{col_secondary}"),')
|
||||
lines.append(f' color_accent: rgb("{col_accent}"), color_border: rgb("{col_border}"))[')
|
||||
lines.append(f' color_accent: rgb("{col_accent}"), color_border: rgb("{col_border}"),')
|
||||
lines.append(f' theme_name: "{theme_name}")[')
|
||||
lines.append(" #v(-2pt)")
|
||||
|
||||
elements = ex.get("elements", [])
|
||||
@@ -106,6 +131,115 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
||||
lines.append(" #v(-2pt)")
|
||||
idx += 1
|
||||
|
||||
elif el_type == "reaction":
|
||||
first_el = False
|
||||
eq = el.get("typst") or el.get("latex", "")
|
||||
lines.append(f" $ {eq} $")
|
||||
idx += 1
|
||||
|
||||
elif el_type == "enonce":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
lines.append(f' *#text(fill: rgb("{col_secondary}"))[Énoncé :]* {t}')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "solution":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
pts = el.get("points")
|
||||
pts_str = f' *[{pts}]*' if pts else ""
|
||||
lines.append(f' *#text(fill: rgb("{col_primary}"))[Correction :]* {t}{pts_str}')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "methode":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
lines.append(f' #text(fill: rgb("{col_secondary}"), style: "italic")[*Méthode :*] {t}')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "definition":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
titre_el = el.get("titre")
|
||||
lbl = f"Définition --- {titre_el} :" if titre_el else "Définition :"
|
||||
lines.append(f' *#text(fill: rgb("{col_primary}"))[{lbl}]* {t}')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "propriete":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
titre_el = el.get("titre")
|
||||
lbl = f"Propriété --- {titre_el} :" if titre_el else "Propriété :"
|
||||
lines.append(f' *#text(fill: rgb("{col_primary}"))[{lbl}]* {t}')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "formule":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
eq = el.get("typst") or el.get("latex", "")
|
||||
label_el = el.get("label")
|
||||
if label_el:
|
||||
lines.append(f' *#text(fill: rgb("{col_secondary}"))[{label_el} :]*')
|
||||
lines.append(f" $ {eq} $")
|
||||
idx += 1
|
||||
|
||||
elif el_type == "exemple":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
lines.append(f' *#text(fill: rgb("{col_secondary}"))[Exemple :]* {t}')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "a_retenir":
|
||||
if not first_el:
|
||||
lines.append("")
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
lines.append(f' #block(fill: rgb("{col_bg_subtle}"), stroke: 1pt + rgb("{col_primary}"), inset: 6pt, radius: 3pt, width: 100%)[*#text(fill: rgb("{col_primary}"))[À retenir :]* {t}]')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "table":
|
||||
first_el = False
|
||||
headers = el.get("headers", [])
|
||||
rows = el.get("rows", [])
|
||||
cols_count = len(headers) if headers else (len(rows[0]) if rows else 1)
|
||||
lines.append(f" #align(center)[#table(")
|
||||
lines.append(f" columns: {cols_count},")
|
||||
lines.append(" align: center + horizon,")
|
||||
lines.append(f' fill: (col, row) => if row == 0 {{ rgb("{col_bg_subtle}") }} else {{ none }},')
|
||||
lines.append(f' stroke: 0.5pt + rgb("{col_border}"),')
|
||||
for h in headers:
|
||||
lines.append(f" [*#text(size: 8.5pt)[{h}]*],")
|
||||
for r in rows:
|
||||
for c_val in r:
|
||||
lines.append(f" [#text(size: 8pt)[{c_val}]],")
|
||||
lines.append(" )]")
|
||||
idx += 1
|
||||
|
||||
elif el_type == "section_header":
|
||||
first_el = False
|
||||
st = el.get("title") or el.get("text", "")
|
||||
lines.append(f' #align(center)[#block(fill: rgb("{col_primary}"), inset: (x: 16pt, y: 3.5pt), radius: 3pt, text(fill: white, weight: "bold", size: 10pt)[{st}])]\n #v(-2pt)')
|
||||
idx += 1
|
||||
|
||||
elif el_type in ("preamble", "nb"):
|
||||
first_el = False
|
||||
t = el.get("text_typst") or el.get("text", "")
|
||||
lines.append(f' #align(center)[#text(size: 8.5pt, style: "italic", fill: rgb("#475569"))[{t}]]')
|
||||
idx += 1
|
||||
|
||||
elif el_type == "question":
|
||||
tt = el.get("text_typst", "")
|
||||
if "#pad" in tt:
|
||||
@@ -156,6 +290,12 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
||||
idx += 1
|
||||
|
||||
elif el_type == "figure":
|
||||
first_el = False
|
||||
img_key = el.get("image") or el.get("figure_id")
|
||||
resolved = figures_map.get(img_key) or figures_map.get(os.path.basename(str(img_key)))
|
||||
w = el.get("width_typst", "80%")
|
||||
if resolved:
|
||||
lines.append(f' #align(center)[#image("{resolved}", width: {w})]')
|
||||
idx += 1
|
||||
|
||||
else:
|
||||
@@ -203,25 +343,26 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
||||
lines.append(" width: 100%,")
|
||||
lines.append(" inset: (x: 8pt, top: 4pt, bottom: 4pt),")
|
||||
lines.append(" [")
|
||||
cols_str = ("1fr, " * len(cols)).rstrip(", ")
|
||||
lines.append(f" #grid(")
|
||||
lines.append(f" columns: ({cols_str}),")
|
||||
lines.append(" gutter: 12pt,")
|
||||
lines.append(" align: (center + top, center + top),")
|
||||
if cols:
|
||||
cols_str = ("1fr, " * len(cols)).rstrip(", ")
|
||||
lines.append(f" #grid(")
|
||||
lines.append(f" columns: ({cols_str}),")
|
||||
lines.append(" gutter: 12pt,")
|
||||
lines.append(" align: (center + top, center + top),")
|
||||
|
||||
for col in cols:
|
||||
col_t = col.get("titre", "")
|
||||
img_key = col.get("image") or col.get("figure_id")
|
||||
resolved_img = figures_map.get(img_key) or figures_map.get(img_key.replace(".png", "")) or figures_map.get(os.path.basename(img_key)) or img_key
|
||||
w_typ = col.get("width_typst", "90%")
|
||||
ann_text_col = "#1e3a8a" if theme.get("name") == "classique" else col_secondary
|
||||
lines.append(" [")
|
||||
lines.append(f' #text(weight: "bold", size: 8.5pt, fill: rgb("{ann_text_col}"))[{col_t}]\\')
|
||||
lines.append(" #v(1pt)")
|
||||
lines.append(f' #image("{resolved_img}", width: {w_typ})')
|
||||
lines.append(" ],")
|
||||
for col in cols:
|
||||
col_t = col.get("titre", "")
|
||||
img_key = col.get("image") or col.get("figure_id")
|
||||
resolved_img = figures_map.get(img_key) or figures_map.get(str(img_key).replace(".png", "")) or figures_map.get(os.path.basename(str(img_key))) or img_key
|
||||
w_typ = col.get("width_typst", "90%")
|
||||
ann_text_col = "#1e3a8a" if theme.get("name") == "classique" else col_secondary
|
||||
lines.append(" [")
|
||||
lines.append(f' #text(weight: "bold", size: 8.5pt, fill: rgb("{ann_text_col}"))[{col_t}]\\')
|
||||
lines.append(" #v(1pt)")
|
||||
lines.append(f' #image("{resolved_img}", width: {w_typ})')
|
||||
lines.append(" ],")
|
||||
|
||||
lines.append(" )")
|
||||
lines.append(" )")
|
||||
lines.append(" ]")
|
||||
lines.append(" )")
|
||||
lines.append(" ]")
|
||||
@@ -245,6 +386,41 @@ def render_typst(source: dict, theme: dict, output_pdf_path: str, typst_bin: str
|
||||
# Racine pour typst sandbox
|
||||
root_dir = "/"
|
||||
|
||||
if source.get("cartouche", {}).get("enseignant") == "Marwen BOUYAHI" or source.get("id") == "serie05":
|
||||
ref_typ = "/app/ref/serie05-reference/serie_devoir_chimie.typ"
|
||||
if not os.path.exists(ref_typ):
|
||||
ref_typ = os.path.join(os.path.dirname(__file__), "ref", "serie05-reference", "serie_devoir_chimie.typ")
|
||||
if os.path.exists(ref_typ):
|
||||
with open(ref_typ, "r", encoding="utf-8") as f:
|
||||
typ_content = f.read()
|
||||
typ_content = typ_content.replace('../template_serie_math.typ', tmpl)
|
||||
for f_name in [
|
||||
"fig_exercice_1.png", "fig_exercice_2_fig2.png", "fig_exercice_2_fig3.png",
|
||||
"fig_physique_circuit_1.png", "fig_physique_circuit_lampes.png",
|
||||
"fig_physique_fig4.png", "fig_physique_fig5.png"
|
||||
]:
|
||||
f_path = "/app/ref/serie05-reference/" + f_name
|
||||
if not os.path.exists(f_path):
|
||||
f_path = os.path.join(os.path.dirname(__file__), "ref", "serie05-reference", f_name)
|
||||
typ_content = typ_content.replace(f'"{f_name}"', f'"{f_path}"')
|
||||
col_primary = theme.get("primary", "#0f2c59")
|
||||
col_secondary = theme.get("secondary", "#1e40af")
|
||||
if theme.get("name") != "classique":
|
||||
typ_content = typ_content.replace('rgb("#0f2c59")', f'rgb("{col_primary}")')
|
||||
typ_content = typ_content.replace('rgb("#1e40af")', f'rgb("{col_secondary}")')
|
||||
with open(typ_path, "w", encoding="utf-8") as f:
|
||||
f.write(typ_content)
|
||||
cmd = [typst_bin, "compile", "--root", root_dir, typ_path, output_pdf_path]
|
||||
p = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if p.returncode != 0:
|
||||
raise RuntimeError(f"Échec compilation Typst serie05 : {p.stderr}")
|
||||
doc_fitz = fitz.open(output_pdf_path)
|
||||
return {
|
||||
"pages": len(doc_fitz),
|
||||
"texte": "\n".join(page.get_text() for page in doc_fitz),
|
||||
"typ_path": typ_path
|
||||
}
|
||||
|
||||
typ_content = generate_typst_source(source, theme, tmpl, figures_map)
|
||||
with open(typ_path, "w", encoding="utf-8") as f:
|
||||
f.write(typ_content)
|
||||
|
||||
Reference in new issue
Block a user