65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
# isg.py
|
|
|
|
import re
|
|
import yaml
|
|
from ..utils.pastille import pastille
|
|
|
|
def _synth_isg(md: str) -> str:
|
|
yaml_block = re.search(r"```yaml\n(.+?)\n```", md, re.DOTALL)
|
|
if not yaml_block:
|
|
return "*(aucune donnée de pays trouvée)*"
|
|
|
|
yaml_data = yaml.safe_load(yaml_block.group(1))
|
|
lignes = ["| Pays | WGI | FSI | NDGAIN | ISG |", "| :-- | :-- | :-- | :-- | :-- |"]
|
|
sorted_pays = sorted(yaml_data.items(), key=lambda x: x[1]['pays'].lower())
|
|
|
|
for identifiant, data in sorted_pays:
|
|
pays = data['pays']
|
|
wgi_ps = data['wgi_ps']
|
|
fsi = data['fsi']
|
|
ndgain = data['ndgain']
|
|
isg = data['isg']
|
|
pastille_isg = pastille("ISG", isg)
|
|
lignes.append(f"| {pays} | {wgi_ps} | {fsi} | {ndgain} | {pastille_isg} {isg} |")
|
|
|
|
return "\n".join(lignes)
|
|
|
|
def build_isg_sections(md: str) -> str:
|
|
"""
|
|
Fonction principale pour générer les sections dynamiques dans le markdown, spécifiquement dédiée à l'analyse des indices ISG.
|
|
|
|
La fonction gère :
|
|
- La structure YAML front-matter pour vérifier si c'est bien un tableau ISG
|
|
- L'extraction et tri du pays selon la valeur ISG
|
|
- Le formatage des données de WGI, FSI, NDGain et ISG dans le tableau final
|
|
|
|
Args:
|
|
md (str): Contenu brut du fichier Markdown contenant les structures YAML à analyser.
|
|
|
|
Returns:
|
|
str: Le markdown enrichi avec le tableau de donnée analysé pour l'indice ISG, ou le contenu original inchangé si aucun bloc structuré n'est trouvé.
|
|
"""
|
|
front_match = re.match(r"(?s)^---\n(.*?)\n---\n", md)
|
|
if front_match:
|
|
front_matter = yaml.safe_load(front_match.group(1))
|
|
if front_matter.get("indice_court") != "ISG":
|
|
return md
|
|
|
|
synth_table = _synth_isg(md)
|
|
|
|
md_final = re.sub(
|
|
r"## Tableau de synthèse\s*\n<!---- AUTO-BEGIN:TABLEAU-FINAL -->.*?<!---- AUTO-END:TABLEAU-FINAL -->",
|
|
f"## Tableau de synthèse\n<!---- AUTO-BEGIN:TABLEAU-FINAL -->\n{synth_table}\n<!---- AUTO-END:TABLEAU-FINAL -->",
|
|
md,
|
|
flags=re.S
|
|
)
|
|
|
|
md_final = re.sub(
|
|
r"# Criticité par pays\s*\n```yaml[\s\S]*?```\s*",
|
|
"# Criticité par pays\n\n",
|
|
md_final,
|
|
flags=re.S
|
|
)
|
|
|
|
return md_final
|