51 lines
1.5 KiB
Python
51 lines
1.5 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:
|
|
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
|