fix(layout): typst paragraph breaks and question padding alignment for theme classique (perso-2026-11-011-b)
This commit is contained in:
1 parent
649fe5f9ef
commit
56c36c3879
1 file changed
+70
-24
+70
-24
@@ -7,9 +7,15 @@ Effectue les contrôles bloquants : compilation sans avertissement et nombre de
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import fitz
|
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°)*)."""
|
||||||
|
s = t.strip()
|
||||||
|
return bool(re.match(r"^(\*?\d+\)|\d+\)|\*?[IVXLCDM]+[°\)]|[IVXLCDM]+[°\)])", s))
|
||||||
|
|
||||||
def generate_typst_source(source: dict, theme: dict, template_import_path: str, figures_map: dict = None) -> str:
|
def generate_typst_source(source: dict, theme: dict, template_import_path: str, figures_map: dict = None) -> str:
|
||||||
"""Produit le code source Typst complet à partir de la source JSON et du thème."""
|
"""Produit le code source Typst complet à partir de la source JSON et du thème."""
|
||||||
figures_map = figures_map or {}
|
figures_map = figures_map or {}
|
||||||
@@ -58,7 +64,8 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
lines.append("#set par(leading: 0.52em)")
|
lines.append("#set par(leading: 0.52em)")
|
||||||
lines.append("")
|
lines.append("")
|
||||||
|
|
||||||
for ex in source.get("exercices", []):
|
exercices = source.get("exercices", [])
|
||||||
|
for ex_idx, ex in enumerate(exercices):
|
||||||
num = str(ex.get("numero", "1"))
|
num = str(ex.get("numero", "1"))
|
||||||
titre = ex.get("titre")
|
titre = ex.get("titre")
|
||||||
titre_arg = f'titre: "{titre}"' if titre else 'titre: none'
|
titre_arg = f'titre: "{titre}"' if titre else 'titre: none'
|
||||||
@@ -74,20 +81,68 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
|
|
||||||
elements = ex.get("elements", [])
|
elements = ex.get("elements", [])
|
||||||
idx = 0
|
idx = 0
|
||||||
|
first_el = True
|
||||||
|
|
||||||
while idx < len(elements):
|
while idx < len(elements):
|
||||||
el = elements[idx]
|
el = elements[idx]
|
||||||
el_type = el.get("type")
|
el_type = el.get("type")
|
||||||
|
|
||||||
if el_type == "question":
|
if el_type == "part_header":
|
||||||
|
if not first_el:
|
||||||
|
lines.append("")
|
||||||
|
first_el = False
|
||||||
|
prefix = el.get("bold_prefix", "").strip()
|
||||||
|
t = el.get("text_typst") or el.get("text", "")
|
||||||
|
lines.append(f" *{prefix}* {t}")
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
elif el_type == "display_equation":
|
||||||
|
first_el = False
|
||||||
|
eq = el.get("typst") or el.get("latex", "")
|
||||||
|
lines.append(f" $ {eq} $")
|
||||||
|
next_el = elements[idx+1] if idx+1 < len(elements) else None
|
||||||
|
if next_el and next_el.get("type") == "question":
|
||||||
|
if next_el.get("indent_level", 0) > 0 or not is_numbered_start(next_el.get("text_typst", "") or next_el.get("text", "")):
|
||||||
|
lines.append(" #v(-2pt)")
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
elif el_type == "question":
|
||||||
|
tt = el.get("text_typst", "")
|
||||||
|
if "#pad" in tt:
|
||||||
|
if not first_el:
|
||||||
|
lines.append("")
|
||||||
|
first_el = False
|
||||||
|
manual_block = []
|
||||||
|
while idx < len(elements) and elements[idx].get("type") == "question":
|
||||||
|
sub_t = elements[idx].get("text_typst") or elements[idx].get("text", "")
|
||||||
|
manual_block.append(sub_t)
|
||||||
|
idx += 1
|
||||||
|
if "]" in sub_t:
|
||||||
|
break
|
||||||
|
for m_i, ml in enumerate(manual_block):
|
||||||
|
end_char = "\\" if (m_i < len(manual_block) - 1 and not ml.rstrip().endswith("\\")) else ""
|
||||||
|
if ml.lstrip().startswith(("*b)*", "*c)*", "*d)*", "*a)*")):
|
||||||
|
ml_str = " " + ml.lstrip()
|
||||||
|
else:
|
||||||
|
ml_str = f" {ml}"
|
||||||
|
lines.append(f"{ml_str}{end_char}")
|
||||||
|
continue
|
||||||
|
|
||||||
indent = el.get("indent_level", 0)
|
indent = el.get("indent_level", 0)
|
||||||
if indent == 0:
|
if indent == 0:
|
||||||
t = el.get("text_typst") or el.get("text", "")
|
t = tt or el.get("text", "")
|
||||||
lines.append(f" {t}")
|
if is_numbered_start(t) and not first_el:
|
||||||
|
lines.append("")
|
||||||
|
first_el = False
|
||||||
|
next_el = elements[idx+1] if idx+1 < len(elements) else None
|
||||||
|
if next_el and next_el.get("type") == "question" and next_el.get("indent_level", 0) > 0 and "#pad" not in next_el.get("text_typst", ""):
|
||||||
|
lines.append(f" {t}\\")
|
||||||
|
else:
|
||||||
|
lines.append(f" {t}")
|
||||||
idx += 1
|
idx += 1
|
||||||
else:
|
else:
|
||||||
# Regrouper les sous-questions consécutives indentées dans un #pad(left: 10pt)
|
|
||||||
pad_lines = []
|
pad_lines = []
|
||||||
while idx < len(elements) and elements[idx].get("type") == "question" and elements[idx].get("indent_level", 0) > 0:
|
while idx < len(elements) and elements[idx].get("type") == "question" and elements[idx].get("indent_level", 0) > 0 and "#pad" not in elements[idx].get("text_typst", ""):
|
||||||
sub_t = elements[idx].get("text_typst") or elements[idx].get("text", "")
|
sub_t = elements[idx].get("text_typst") or elements[idx].get("text", "")
|
||||||
pad_lines.append(sub_t)
|
pad_lines.append(sub_t)
|
||||||
idx += 1
|
idx += 1
|
||||||
@@ -96,19 +151,6 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
end_char = "\\" 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
|
|
||||||
|
|
||||||
elif el_type == "display_equation":
|
|
||||||
eq = el.get("typst") or el.get("latex", "")
|
|
||||||
lines.append(f" $ {eq} $")
|
|
||||||
lines.append(" #v(-2pt)")
|
|
||||||
idx += 1
|
|
||||||
|
|
||||||
elif el_type == "part_header":
|
|
||||||
prefix = el.get("bold_prefix", "")
|
|
||||||
t = el.get("text_typst") or el.get("text", "")
|
|
||||||
lines.append(f" *{prefix}* {t}")
|
|
||||||
idx += 1
|
|
||||||
|
|
||||||
elif el_type == "page_break":
|
elif el_type == "page_break":
|
||||||
idx += 1
|
idx += 1
|
||||||
@@ -125,18 +167,21 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
lines.append("]")
|
lines.append("]")
|
||||||
lines.append("")
|
lines.append("")
|
||||||
|
|
||||||
# Vérifier si l'exercice contient un saut de page pour le placer hors de la boîte
|
|
||||||
has_pagebreak = any(e.get("type") == "page_break" for e in elements)
|
has_pagebreak = any(e.get("type") == "page_break" for e in elements)
|
||||||
if has_pagebreak:
|
if has_pagebreak:
|
||||||
lines.append("#pagebreak()")
|
lines.append("#pagebreak()")
|
||||||
lines.append("")
|
lines.append("")
|
||||||
|
elif ex_idx < len(exercices) - 1:
|
||||||
|
lines.append("#v(-1pt)")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
annexe = source.get("annexe")
|
annexe = source.get("annexe")
|
||||||
if annexe:
|
if annexe:
|
||||||
cols = annexe.get("colonnes", [])
|
cols = annexe.get("colonnes", [])
|
||||||
ann_titre = annexe.get("titre", "ANNEXE À RENDRE AVEC LA COPIE — FIGURES OFFICIELLES")
|
ann_titre = annexe.get("titre", "ANNEXE À RENDRE AVEC LA COPIE — FIGURES OFFICIELLES")
|
||||||
lines.append("// ANNEXE OFFICIELLE")
|
lines.append("// ANNEXE OFFICIELLE À RENDRE AVEC LA COPIE")
|
||||||
lines.append("#v(4pt)")
|
lines.append("#v(4pt)")
|
||||||
|
lines.append("")
|
||||||
lines.append("#block(")
|
lines.append("#block(")
|
||||||
lines.append(" width: 100%,")
|
lines.append(" width: 100%,")
|
||||||
lines.append(f' stroke: 1pt + rgb("{col_primary}"),')
|
lines.append(f' stroke: 1pt + rgb("{col_primary}"),')
|
||||||
@@ -150,7 +195,7 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
lines.append(" inset: (x: 10pt, y: 4.5pt),")
|
lines.append(" inset: (x: 10pt, y: 4.5pt),")
|
||||||
lines.append(" align(center)[")
|
lines.append(" align(center)[")
|
||||||
lines.append(' #text(fill: white, weight: "bold", size: 9.5pt)[')
|
lines.append(' #text(fill: white, weight: "bold", size: 9.5pt)[')
|
||||||
lines.append(f' {ann_titre}')
|
lines.append(f" {ann_titre}")
|
||||||
lines.append(" ]")
|
lines.append(" ]")
|
||||||
lines.append(" ]")
|
lines.append(" ]")
|
||||||
lines.append(" )")
|
lines.append(" )")
|
||||||
@@ -167,10 +212,11 @@ def generate_typst_source(source: dict, theme: dict, template_import_path: str,
|
|||||||
for col in cols:
|
for col in cols:
|
||||||
col_t = col.get("titre", "")
|
col_t = col.get("titre", "")
|
||||||
img_key = col.get("image") or col.get("figure_id")
|
img_key = col.get("image") or col.get("figure_id")
|
||||||
resolved_img = figures_map.get(img_key) or figures_map.get(os.path.basename(img_key)) or img_key
|
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%")
|
w_typ = col.get("width_typst", "90%")
|
||||||
|
ann_text_col = "#1e3a8a" if theme.get("name") == "classique" else col_secondary
|
||||||
lines.append(" [")
|
lines.append(" [")
|
||||||
lines.append(f' #text(weight: "bold", size: 8.5pt, fill: rgb("{col_secondary}"))[{col_t}]\\')
|
lines.append(f' #text(weight: "bold", size: 8.5pt, fill: rgb("{ann_text_col}"))[{col_t}]\\')
|
||||||
lines.append(" #v(1pt)")
|
lines.append(" #v(1pt)")
|
||||||
lines.append(f' #image("{resolved_img}", width: {w_typ})')
|
lines.append(f' #image("{resolved_img}", width: {w_typ})')
|
||||||
lines.append(" ],")
|
lines.append(" ],")
|
||||||
|
|||||||
Reference in new issue
Block a user