#!/usr/bin/env python3 """Écrit un fichier d'étiquettes (\\newlabel) pour tous les chapitres et annexes, afin que \\cref{ch:NN} / \\cref{app:x} se résolvent lors de la compilation isolée d'un chapitre (tools/compile_chapter.sh). Usage : make_labels.py """ import json, sys, pathlib ROOT = pathlib.Path(__file__).resolve().parents[1] t = json.load(open(ROOT / "manuscript/chapters/TITRES.json", encoding="utf-8")) exclure = int(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[2].isdigit() else -1 out = ["\\makeatletter"] def lab(name, ref, title, anchor, kind): out.append("\\@newl@bel{r}{%s}{{%s}{1}{%s}{%s}{}}" % (name, ref, title, anchor)) out.append("\\@newl@bel{r}{%s@cref}{{[%s][%s][]%s}{[1][1][]1}{}{}{}}" % (name, kind, ref, ref)) for n, title in t.items(): n = int(n) if n == exclure: continue lab(f"ch:{n:02d}", str(n), title, f"chapter.{n}", "chapter") if n < 10: lab(f"ch:{n}", str(n), title, f"chapter.{n}", "chapter") for L, k in zip("ABCDEF", ["glossaire", "formulaires-inspection", "modeles-rapport", "conventions-calcul", "scripts-classeurs", "solutions"]): lab(f"app:{k}", L, "", f"appendix.{L}", "chapter") out.append("\\makeatother") pathlib.Path(sys.argv[1]).write_text("\n".join(out) + "\n", encoding="utf-8")