import yaml,sys,os
#
# Fabriquer la table des auteurs.
#
excludedirs={"ChapitresEtParties","Academie"}
#---------------------------------------------

sys.path.append(f"{os.getcwd()}/..")
from memmod import *
#
path2add= "../Sources/Courante"
with open("all_texts") as f:
    L= f.readlines()
D={}
for x in L:
    d0=x.split('/')[0]
    
    if not d0 in excludedirs and d0[0]!="#":
        d= f"{path2add}/{d0}/data.yml"
        with open(d) as f:
            lt=yaml.safe_load(f)
            auteur= lt["auteur"]
            titre= lt["titre"]
            D.setdefault(auteur,[]).append(titre)

# On a un dictionnaire D: auteur -> liste des textes.
# fabriquer le LaTeX.
with open("refauteur.tex","w") as f:
    s="\\begin{center} \\Large{\\bfseries INDEX DES AUTEURS}\\end{center}\n"
    f.write(s)
    f.write("\n")
    # trier les clés pas noms (pas forcément exact)
    DK=sorted([x for x in D.keys()], key=lambda x: x.split()[-1])
    for k in DK:
        print(k,len(D[k]))
        if len(D[k])==1:
            x=D[k][0]
            cle= dolabel(k,x)
            print(k,"/",x,f"\n\\label{{{cle}}}","\n")
            s= f"{k}, \\textit{{{x}}} \\dotfill \\pageref{{{cle}}}\n\n"
            f.write(s)
        else:
            f.write(f"{k}:\n\n")
            for x in D[k]:
                space="\\hspace*{0.75cm}"
                cle= dolabel(k,x)
                print("Clé:",k,"/",x,cle,"\n")
                s= f"{space}\\textit{{{x}}} \\dotfill \\pageref{{{cle}}}\n\n"
                f.write(s)

        f.write("\n")
