64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
"""
|
||
fiche_utils.py – outils de lecture / rendu des fiches Markdown (indices et opérations)
|
||
|
||
Dépendances :
|
||
pip install python-frontmatter pyyaml jinja2
|
||
|
||
Usage :
|
||
from fiche_utils import load_seuils, render_fiche_markdown
|
||
|
||
seuils = load_seuils("config/indices_seuils.yaml")
|
||
markdown_rendered = render_fiche_markdown(raw_md_text, seuils)
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
import frontmatter, yaml, jinja2, re, textwrap, pathlib
|
||
from typing import Dict
|
||
|
||
|
||
def load_seuils(path: str | pathlib.Path = "config/indices_seuils.yaml") -> Dict:
|
||
"""Charge le fichier YAML des seuils et renvoie le dict 'seuils'."""
|
||
data = yaml.safe_load(pathlib.Path(path).read_text(encoding="utf-8"))
|
||
return data.get("seuils", {})
|
||
|
||
|
||
def _migrate_metadata(meta: Dict) -> Dict:
|
||
"""Normalise les clés YAML (ex : sheet_type → type_fiche)."""
|
||
keymap = {
|
||
"sheet_type": "type_fiche",
|
||
"indice_code": "indice_court", # si besoin
|
||
}
|
||
for old, new in keymap.items():
|
||
if old in meta and new not in meta:
|
||
meta[new] = meta.pop(old)
|
||
return meta
|
||
|
||
|
||
def render_fiche_markdown(md_text: str, seuils: Dict) -> str:
|
||
"""Renvoie la fiche rendue (Markdown) :
|
||
– placeholders Jinja2 remplacés ({{ … }})
|
||
– table seuils injectée via dict 'seuils'.
|
||
"""
|
||
post = frontmatter.loads(md_text)
|
||
meta = _migrate_metadata(dict(post.metadata))
|
||
body_template = post.content
|
||
|
||
# Instancie Jinja2 en 'StrictUndefined' pour signaler les placeholders manquants.
|
||
env = jinja2.Environment(
|
||
undefined=jinja2.StrictUndefined,
|
||
autoescape=False,
|
||
trim_blocks=True,
|
||
lstrip_blocks=True,
|
||
)
|
||
tpl = env.from_string(body_template)
|
||
rendered_body = tpl.render(**meta, seuils=seuils)
|
||
|
||
# Option : ajoute automatiquement titre + tableau version si absent.
|
||
header = f"# {meta.get('indice', meta.get('titre',''))} ({meta.get('indice_court','')})"
|
||
if not re.search(r"^# ", rendered_body, flags=re.M):
|
||
rendered_body = f"""{header}
|
||
|
||
{rendered_body}"""
|
||
|
||
return rendered_body
|