# ivc.py import re import yaml from jinja2 import Template IVC_RE = re.compile(r"```yaml\s+minerai:(.*?)```", re.S | re.I) def _synth_ivc(minerais: list[dict]) -> str: """Crée un tableau de synthèse pour les IVC des minerais.""" lignes = [ "| Minerai | IVC | Vulnérabilité |", "| :-- | :-- | :-- |" ] for minerai in minerais: lignes.append( f"| {minerai['nom']} | {minerai['ivc']} | {minerai['vulnerabilite']} |" ) return "\n".join(lignes) def _ivc_segments(md: str): """Yield (dict, segment) pour chaque bloc IVC yaml.""" pos = 0 for m in IVC_RE.finditer(md): bloc = yaml.safe_load("minerai:" + m.group(1)) start = m.end() next_match = IVC_RE.search(md, start) end = next_match.start() if next_match else len(md) yield bloc["minerai"], md[start:end].strip() pos = end yield None, md[pos:] # reste éventuel def build_ivc_sections(md: str) -> str: """Remplace les blocs YAML minerai + segment avec rendu Jinja2, conserve l'intro.""" segments = [] minerais = [] # Pour collecter les données de chaque minerai intro = None matches = list(IVC_RE.finditer(md)) if matches: first = matches[0] intro = md[:first.start()].strip() else: return md # pas de blocs à traiter for m in matches: bloc = yaml.safe_load("minerai:" + m.group(1)) minerais.append(bloc["minerai"]) # Collecte les données start = m.end() next_match = IVC_RE.search(md, start) end = next_match.start() if next_match else len(md) rendered = Template(md[start:end].strip()).render(**bloc["minerai"]) segments.append(rendered) if intro: segments.insert(0, intro) # Créer et insérer le tableau de synthèse synth_table = _synth_ivc(minerais) md_final = "\n\n".join(segments) # Remplacer la section du tableau final md_final = re.sub( r"## Tableau de synthèse\s*\n.*?", f"## Tableau de synthèse\n\n{synth_table}\n", md_final, flags=re.S ) return md_final