#!/home/moi/ProtoMemoires/venv/bin/python
#
# Moderniser un fichier source.
#
import yaml
def dolabel(k,t):
    return str(hash(k+t)).replace("-","m")
ft=input("fichier .tex ?")
#
# fichier de config:
#
with open("data.yml") as f:
    lt=yaml.safe_load(f)
has_bib= "biblio"in lt.keys()

#--------------------------------------------------------------------
template="../tpl.tex" # le template
#-----------------------------------------------------------------
with open(template) as f:
    Ltmpl=f.readlines()
#
# ne garder du template que le début
# et mentionner les ressouces pour biblatex
#
Lall=[]
for l in Ltmpl:
    if "%@@debut" in l:
        break
    elif has_bib and  "addbibresource" in l:
        # ajouter le chemin vers le fichier bib
        Lall.append(f"\\addbibresource{{{lt['biblio']}}}\n")
                
    Lall.append(l)
#
with open(ft) as f:
    LL=f.readlines()
    # insérer ce qui entre %@@debut et "%@@fin":
    start= False
    for x in LL:
        if "@@debut" in x:
            start= True
            print("--> start")
        if "@@fin" in x:
            break
        if start and "newcounter{none}" not in x:
            Lall.append(x)

fr=input("fichier à construire ?")
with open(fr,"w") as f:
    for x in Lall[:-1]:
        f.write(x)
    f.write("\\FloatBarrier\n")
    f.write("%@@fin\n")
    f.write("\\tableofcontents\n")
    f.write("\\end{document}\n")
#---
print("fin\n")
