import html as html_lib
import json
import random
from typing import Any
def render_document(elements: list[dict[str, Any]]) -> str:
"""Assemble le document ENTIER à partir de la liste à plat renvoyée
par document_engine.list_document_elements : les éléments top-niveau
(parent_id NULL) dans l'ordre, chaque rangée ("row") rendant
récursivement ses propres enfants côte à côte. Point d'entrée unique
du rendu, utilisé à la fois par le canevas d'édition et par le Mode
Aperçu (même fonction, voir docs/plan/PLAN.md — "Aperçu réel")."""
children_by_parent: dict[int | None, list[dict[str, Any]]] = {}
for el in elements:
children_by_parent.setdefault(el["parent_id"], []).append(el)
top_level = children_by_parent.get(None, [])
return "".join(render_document_element(el, children_by_parent) for el in top_level)
def render_document_element(el: dict[str, Any], children_by_parent: dict[int | None, list[dict[str, Any]]]) -> str:
"""HTML d'UN élément — dispatch par dict plutôt qu'un enchaînement de
`if` (mirroir de l'esprit de game_engine/scenes/render_scene_object.py,
mais un vrai registre ici puisque le nombre de kinds est plus élevé)."""
renderer = _RENDERERS.get(el["kind"], _render_unknown)
return renderer(el, children_by_parent)
def _render_row(el: dict[str, Any], children_by_parent: dict[int | None, list[dict[str, Any]]]) -> str:
attrs = el["attributes"]
gap = attrs.get("gap", 16)
align = html_lib.escape(str(attrs.get("align", "stretch")))
justify = html_lib.escape(str(attrs.get("justify", "flex-start")))
style = f"display:flex; flex-wrap:wrap; gap:{gap}px; align-items:{align}; justify-content:{justify};"
children = children_by_parent.get(el["id"], [])
inner = "".join(render_document_element(child, children_by_parent) for child in children)
return f'
) ou numérotée () — le kind lui-même décide
la balise, pas un attribut "ordered" séparé (voir
element_kind_labels.element_default_attributes). Une "items" vide
rend une liste vide plutôt qu'un placeholder : contrairement à une
image sans fichier, une liste sans élément n'a rien d'anormal à
afficher (le créateur vient peut-être de tout supprimer avant d'en
retaper un)."""
items = el["attributes"].get("items", [])
tag = "ol" if el["kind"] == "liste_numerotee" else "ul"
items_html = "".join(f"
{html_lib.escape(str(item))}
" for item in items)
return f'<{tag} class="docList" data-element-id="{el["id"]}" data-kind="{el["kind"]}">{items_html}{tag}>'
def _render_button(el: dict[str, Any], _children_by_parent: dict[int | None, list[dict[str, Any]]]) -> str:
a = el["attributes"]
label = html_lib.escape(str(a.get("label", "Bouton")))
target = html_lib.escape(str(a.get("target", "")))
target_attr = f' data-target="{target}"' if target else ""
return (
f'"
)
def _render_minigame_placeholder(
el: dict[str, Any], _children_by_parent: dict[int | None, list[dict[str, Any]]]
) -> str:
from ..labels.element_kind_labels import ELEMENT_KIND_LABELS
a = el["attributes"]
theme_color = html_lib.escape(str(a.get("theme_color", "#ff5f2e")))
label = html_lib.escape(ELEMENT_KIND_LABELS.get(el["kind"], el["kind"]))
return (
f'
'
f'
'
f'{label}'
f'Formulaire de contenu à venir'
f"
"
)
def _render_quiz_player(config: dict[str, Any]) -> str:
"""Questionnaire RÉELLEMENT interactif — affiché uniquement en Mode
Aperçu (voir static/document/document-editor.css,
.docEditor3--preview), pour que le créateur puisse tester son quiz
avant un futur export (voir docs/plan/maquettes/
document-formation-web.html, référence visuelle de ce rendu). Les
questions sont embarquées en JSON dans un attribut data-* (jamais un