SPB Git
2.1 KB · 33 lines python
1#!/usr/bin/env python32"""Écrit un fichier d'étiquettes (\\newlabel) pour tous les chapitres et annexes, afin que \\cref{ch:NN} / \\cref{app:x}3se résolvent lors de la compilation isolée d'un chapitre (tools/compile_chapter.sh). Usage : make_labels.py <sortie.tex>"""4import json, sys, pathlib5ROOT = pathlib.Path(__file__).resolve().parents[1]6t = json.load(open(ROOT / "manuscript/chapters/TITRES.json", encoding="utf-8"))7exclure = int(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[2].isdigit() else -18out = ["\\makeatletter"]9def lab(name, ref, title, anchor, kind):10    out.append("\\@newl@bel{r}{%s}{{%s}{1}{%s}{%s}{}}" % (name, ref, title, anchor))11    out.append("\\@newl@bel{r}{%s@cref}{{[%s][%s][]%s}{[1][1][]1}{}{}{}}" % (name, kind, ref, ref))12for n, title in t.items():13    n = int(n)14    if n == exclure: continue15    lab(f"ch:{n:02d}", str(n), title, f"chapter.{n}", "chapter")16    if n < 10: lab(f"ch:{n}", str(n), title, f"chapter.{n}", "chapter")17for L, k in zip("ABCDEF", ["glossaire", "formulaires-inspection", "modeles-rapport", "conventions-calcul", "scripts-classeurs", "solutions"]):18    lab(f"app:{k}", L, "", f"appendix.{L}", "chapter")19# étiquettes fines (eq:, fig:, tab:, sec:, def:, prop:, ex:…) des autres chapitres déjà compilés (build/chNN/_wrap_chNN.aux)20import re21seen = set(re.findall(r"\\@newl@bel\{r\}\{([^}]+)\}", "\n".join(out)))22for aux in sorted((ROOT / "build").glob("ch*/_wrap_ch*.aux")) + sorted((ROOT / "build").glob("app-*/_wrap_app-*.aux")):23    if exclure >= 0 and aux.parent.name == f"ch{exclure:02d}": continue24    try: txt_aux = aux.read_text(encoding="utf-8", errors="ignore")25    except OSError: continue26    for m in re.finditer(r"^\\newlabel\{([^}]+)\}(\{.*\})\s*$", txt_aux, re.M):27        if m.group(2).count("{") != m.group(2).count("}"): continue   # ligne tronquée (compilation parallèle)28        name, val = m.group(1), m.group(2)29        if name in seen or name.startswith("ch:") or name.startswith("app:"): continue30        seen.add(name); out.append("\\@newl@bel{r}{%s}%s" % (name, val))31out.append("\\makeatother")32pathlib.Path(sys.argv[1]).write_text("\n".join(out) + "\n", encoding="utf-8")33
Files · 81b1896e4502a619cf4f142fb8809253a8ff0bf3
Menu

Theme