diff --git a/fabnum.py b/fabnum.py index 852b2a8..7f60895 100644 --- a/fabnum.py +++ b/fabnum.py @@ -34,9 +34,10 @@ GITEA_URL = os.getenv("GITEA_URL", "https://fabnum-git.peccini.fr/api/v1") GITEA_TOKEN = os.getenv("GITEA_TOKEN", "") ORGANISATION = os.getenv("ORGANISATION", "fabnum") DEPOT_FICHES = os.getenv("DEPOT_FICHES", "fiches") +DEPOT_CODE = os.getenv("DEPOT_CODE", "code") ENV = os.getenv("ENV") - -DOT_FILE = "schema.txt" +ENV_CODE = os.getenv("ENV_CODE") +DOT_FILE = os.getenv("DOT_FILE") niveau_labels = { 0: "Produit final", @@ -126,7 +127,7 @@ st.markdown(""" def recuperer_date_dernier_commit_schema(): headers = {"Authorization": f"token " + GITEA_TOKEN} - url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_FICHES}/commits?path=schema.txt&sha={ENV}" + url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_CODE}/commits?path={DOT_FILE}&sha={ENV_CODE}" try: response = requests.get(url, headers=headers, timeout=10) @@ -139,12 +140,12 @@ def recuperer_date_dernier_commit_schema(): else: return None except Exception as e: - st.error(f"Erreur lors de la récupération du dernier commit de schema.txt : {e}") + st.error(f"Erreur lors de la récupération du dernier commit de {DOT_FILE} : {e}") return None def charger_schema_depuis_gitea(fichier_local="schema_temp.txt"): headers = {"Authorization": f"token " + GITEA_TOKEN} - url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_FICHES}/contents/schema.txt?ref={ENV}" + url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_CODE}/contents/{DOT_FILE}?ref={ENV_CODE}" try: response = requests.get(url, headers=headers, timeout=10) @@ -162,7 +163,7 @@ def charger_schema_depuis_gitea(fichier_local="schema_temp.txt"): return "OK" except Exception as e: - st.error(f"Erreur lors du chargement de schema.txt depuis Gitea : {e}") + st.error(f"Erreur lors du chargement de {DOT_FILE} depuis Gitea : {e}") return None @st.cache_data(ttl=600) @@ -801,27 +802,50 @@ def afficher_fiches(): reponse_fiche.raise_for_status() contenu_md = reponse_fiche.content.decode("utf-8") - # Traitement du markdown + # Nouveau traitement hiérarchique du markdown lignes = contenu_md.split('\n') - contenu_niveau_1 = [] - sections_niveau_2 = {} - section_actuelle = None + sections_n1 = [] + section_n1_actuelle = {"titre": None, "intro": [], "sections_n2": {}} + dans_section_n1 = False + section_n2_actuelle = None for ligne in lignes: - if re.match(r'^##[^#]', ligne): - section_actuelle = ligne.strip('# ').strip() - sections_niveau_2[section_actuelle] = [] - elif section_actuelle: - sections_niveau_2[section_actuelle].append(ligne) - else: - contenu_niveau_1.append(ligne) + if re.match(r'^#[^#]', ligne): + # Nouveau titre de niveau 1 + if section_n1_actuelle["titre"] or section_n1_actuelle["intro"] or section_n1_actuelle["sections_n2"]: + sections_n1.append(section_n1_actuelle) + section_n1_actuelle = { + "titre": ligne.strip('# ').strip(), + "intro": [], + "sections_n2": {} + } + section_n2_actuelle = None + dans_section_n1 = True - if contenu_niveau_1: - st.markdown("\n".join(contenu_niveau_1), unsafe_allow_html=True) + elif re.match(r'^##[^#]', ligne): + # Nouveau titre de niveau 2 + section_n2_actuelle = ligne.strip('# ').strip() + section_n1_actuelle["sections_n2"][section_n2_actuelle] = [] + elif section_n2_actuelle: + section_n1_actuelle["sections_n2"][section_n2_actuelle].append(ligne) + elif dans_section_n1: + section_n1_actuelle["intro"].append(ligne) - for titre, contenu in sections_niveau_2.items(): - with st.expander(titre): - st.markdown("\n".join(contenu), unsafe_allow_html=True) + # Ajouter la dernière section si présente + if section_n1_actuelle["titre"] or section_n1_actuelle["intro"] or section_n1_actuelle["sections_n2"]: + sections_n1.append(section_n1_actuelle) + + # Affichage + for bloc in sections_n1: + if bloc["titre"]: + st.markdown(f"# {bloc['titre']}") + + if bloc["intro"]: + st.markdown("\n".join(bloc["intro"]), unsafe_allow_html=True) + + for sous_titre, contenu in bloc["sections_n2"].items(): + with st.expander(sous_titre): + st.markdown("\n".join(contenu), unsafe_allow_html=True) gerer_tickets_fiche(fiche_choisie)