|
@@ -0,0 +1,204 @@
|
|
|
|
|
+#!/home/moi/ProtoMemoires/venv/bin/python
|
|
|
|
|
+
|
|
|
|
|
+# de docx ou odt ver LaTeX
|
|
|
|
|
+#
|
|
|
|
|
+import sys,pypandoc,os,re,subprocess,yaml,hashlib
|
|
|
|
|
+#---------------------------------------------
|
|
|
|
|
+sys.path.append(f"{os.getcwd()}/..")
|
|
|
|
|
+from memmod import *
|
|
|
|
|
+#----
|
|
|
|
|
+def fancyshort(s,t):
|
|
|
|
|
+ # restreindre le texte de fancyhead (s est le nom, t le titre,
|
|
|
|
|
+ # qu'il faut éventuellement racourcir pour qu'il tienne sur une seule
|
|
|
|
|
+ # ligne.
|
|
|
|
|
+ #
|
|
|
|
|
+ # s (le nom de l'orateur, par exemple) est toujours affiché.
|
|
|
|
|
+ # t est éventuellement raccourci.
|
|
|
|
|
+ #
|
|
|
|
|
+ limit =75 #taille max en caratères
|
|
|
|
|
+ if len(s+t)<limit:
|
|
|
|
|
+ return s,t
|
|
|
|
|
+ else:
|
|
|
|
|
+ tl=t.split()
|
|
|
|
|
+ tl.pop()
|
|
|
|
|
+ while sum(len(x) for x in tl)>limit:
|
|
|
|
|
+ tl.pop()
|
|
|
|
|
+ return s, " ".join(tl)+"\\ldots"# pointillés à la fin.
|
|
|
|
|
+
|
|
|
|
|
+def star_sec(l):
|
|
|
|
|
+ # \section est transformée en \section* et on ne garde comme argument
|
|
|
|
|
+ # que ce qui est nécessaire.
|
|
|
|
|
+ it=iter(L)
|
|
|
|
|
+ ret=[]
|
|
|
|
|
+ passed= False
|
|
|
|
|
+ for x in it:
|
|
|
|
|
+ if "begin{document" in x:
|
|
|
|
|
+ passed= True
|
|
|
|
|
+ if passed and "section[" in x:
|
|
|
|
|
+ s=x
|
|
|
|
|
+ while "label" not in x:
|
|
|
|
|
+ x=next(it)
|
|
|
|
|
+ s=s+" "+x
|
|
|
|
|
+
|
|
|
|
|
+ if not "}" in s:
|
|
|
|
|
+ x=next(it)
|
|
|
|
|
+ s=s+x
|
|
|
|
|
+ k=s.find("]")
|
|
|
|
|
+ r=(s[:k]+"}").replace("[","*{")+" "
|
|
|
|
|
+ ret.append(r)
|
|
|
|
|
+ ret.append("\n")
|
|
|
|
|
+ else:
|
|
|
|
|
+ ret.append(x)
|
|
|
|
|
+ return ret
|
|
|
|
|
+#---------------------------------------------------------------------
|
|
|
|
|
+# ligne de command [nocompile]
|
|
|
|
|
+#
|
|
|
|
|
+# nocompile pour ne pas compiler
|
|
|
|
|
+#
|
|
|
|
|
+#
|
|
|
|
|
+compile= not "nocompile" in sys.argv
|
|
|
|
|
+#-----------------------------------------------------------------------
|
|
|
|
|
+# Construire le titre à partir du fichier data.yml.
|
|
|
|
|
+#-----------------------------------------------------------------------
|
|
|
|
|
+#---clés du fichier data.yml
|
|
|
|
|
+data_yml_mandatory_keys=['fichier', 'date', 'auteur', 'titre']
|
|
|
|
|
+data_yml_order=['date','type','auteur','statut','titre']
|
|
|
|
|
+#
|
|
|
|
|
+with open("data.yml") as f:
|
|
|
|
|
+ lt=yaml.safe_load(f)
|
|
|
|
|
+ print("data_yml lu")
|
|
|
|
|
+# on vérifie que le fichier "data.yml" est correct:
|
|
|
|
|
+for x in data_yml_mandatory_keys: #vérifier la présence des clés obligatoires
|
|
|
|
|
+ if not x in lt.keys():
|
|
|
|
|
+ raise Exception(f"\n\n Il manque la clé {x} dans data.yml\n\n")
|
|
|
|
|
+# si statut est défini, type doit l'être aussi:
|
|
|
|
|
+if "statut" in lt.keys(): # membre t=itulaire, correspondant, d'honneur...
|
|
|
|
|
+ if "type" not in lt.keys():
|
|
|
|
|
+ raise Exception(f"\n\n statut est défini, mais pas type\n\n")
|
|
|
|
|
+ lt['statut']=", "+lt["statut"]
|
|
|
|
|
+else: # si statut n'est pas défini, on le définit à " " et type à "Conférence".
|
|
|
|
|
+ lt["statut"]=" "
|
|
|
|
|
+ if "type" not in lt.keys():
|
|
|
|
|
+ lt["type"]="Conférence"
|
|
|
|
|
+print("data.yml analysé")
|
|
|
|
|
+# ---les paramètres:
|
|
|
|
|
+auteur= lt["auteur"]
|
|
|
|
|
+titre= lt["titre"]
|
|
|
|
|
+label=dolabel(auteur,titre)
|
|
|
|
|
+# biblio ?
|
|
|
|
|
+has_bib= "biblio"in lt.keys()
|
|
|
|
|
+if has_bib:
|
|
|
|
|
+ if len(lt["biblio"].split()) ==2:
|
|
|
|
|
+ bibfile= lt["biblio"].split()[0]
|
|
|
|
|
+ citeall = lt["biblio"].split()[1] == "citeall"
|
|
|
|
|
+ else:
|
|
|
|
|
+ bibfile= lt["biblio"]
|
|
|
|
|
+ citeall=False
|
|
|
|
|
+#-------------------------------------------------
|
|
|
|
|
+#
|
|
|
|
|
+# le fichier (docx, odt) qu'on va traiter:
|
|
|
|
|
+#
|
|
|
|
|
+fich=lt["fichier"].strip()
|
|
|
|
|
+#
|
|
|
|
|
+# fabriquer le titre à partir des données yml
|
|
|
|
|
+#
|
|
|
|
|
+titre_elements="}{".join([lt[x].replace("\n","") for x in data_yml_order])
|
|
|
|
|
+s,t=fancyshort(lt['auteur'],lt['titre'])
|
|
|
|
|
+titre_elements= titre_elements+"}{"+t
|
|
|
|
|
+le_titre= f"\\titre{{{titre_elements}}}\n"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+#--------------------------------------------------------------------
|
|
|
|
|
+template="../tpl.tex" # le template
|
|
|
|
|
+#---------------------------------------------------------------------
|
|
|
|
|
+exten=fich.split(".")[-1]
|
|
|
|
|
+#
|
|
|
|
|
+# Traitement du fichier source
|
|
|
|
|
+# ---------------------------
|
|
|
|
|
+if exten=="docx" or exten=="odt":
|
|
|
|
|
+ futurTex= fich.replace(exten,"tex")
|
|
|
|
|
+else:
|
|
|
|
|
+ s=f"\n{fich} : extension incorrecte"
|
|
|
|
|
+ raise Exception(s)
|
|
|
|
|
+
|
|
|
|
|
+#répertoire pour les images
|
|
|
|
|
+images= fich.split(".")[0]+"-media"
|
|
|
|
|
+print("Répertoire des images : ",images)
|
|
|
|
|
+if not os.path.isdir(images):
|
|
|
|
|
+ os.mkdir(images)
|
|
|
|
|
+#
|
|
|
|
|
+# conversion en latex
|
|
|
|
|
+#
|
|
|
|
|
+#extra_args= ["-s",f"--template={template}",f"--extract-media={images}"]
|
|
|
|
|
+extra_args= ["-s",f"--template={template}",f"--extract-media={images}",
|
|
|
|
|
+ f"--lof=true"]
|
|
|
|
|
+L = pypandoc.convert_file(fich, 'tex',
|
|
|
|
|
+ extra_args=extra_args).splitlines()
|
|
|
|
|
+
|
|
|
|
|
+#-----------------------------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+# section -> section*
|
|
|
|
|
+rechsec= r"section[{\[]"
|
|
|
|
|
+rechsecc=re.compile(rechsec)
|
|
|
|
|
+
|
|
|
|
|
+# pour modifier le latex en changeant le chemin vers les images.
|
|
|
|
|
+if exten=="docx":
|
|
|
|
|
+ rechgra=r"includegraphics\[.*\]{media.*}"
|
|
|
|
|
+ repPath=("media",f"{images}/word/media")
|
|
|
|
|
+elif exten=="odt":
|
|
|
|
|
+ rechgra=r"includegraphics\[.*\]{Pictures.*}"
|
|
|
|
|
+ repPath=("Pictures",f"{images}/Pictures")
|
|
|
|
|
+
|
|
|
|
|
+rechgrac=re.compile(rechgra)
|
|
|
|
|
+#
|
|
|
|
|
+# On crée le fichier LaTex
|
|
|
|
|
+#
|
|
|
|
|
+with open(futurTex,"w") as f:
|
|
|
|
|
+ L1=star_sec(L)
|
|
|
|
|
+
|
|
|
|
|
+ rp0,rp1= repPath
|
|
|
|
|
+ for l in L1:
|
|
|
|
|
+ if has_bib and "addbibresource{}" in l:
|
|
|
|
|
+ #bibfile=lt['biblio']
|
|
|
|
|
+ l=l.replace("{","{"+bibfile)
|
|
|
|
|
+ while (q:=rechgrac.search(l)) != None:
|
|
|
|
|
+ l=l.replace(rp0,rp1)
|
|
|
|
|
+ l=treatline(l)
|
|
|
|
|
+
|
|
|
|
|
+ #
|
|
|
|
|
+ if "@@fin" in l:
|
|
|
|
|
+
|
|
|
|
|
+ if has_bib:
|
|
|
|
|
+ #f.write("\\begin{center}\\textbf{Bibliographie}\\end{center}\n")
|
|
|
|
|
+ #f.write("\\citeall[\\fullcite]\n")
|
|
|
|
|
+ f.write("\\section*{Bibliographie}")
|
|
|
|
|
+ if citeall:
|
|
|
|
|
+ f.write(nocite_all(bibfile)+"\n")
|
|
|
|
|
+ f.write("\\printbibliography[heading=none]\n")
|
|
|
|
|
+ f.write("\\FloatBarrier\n")
|
|
|
|
|
+ f.write("\\end{refsection}\n")
|
|
|
|
|
+ f.write(l)
|
|
|
|
|
+
|
|
|
|
|
+ f.write("\n")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if "@@debut" in l:
|
|
|
|
|
+ f.write("\\begin{refsection}\n")
|
|
|
|
|
+ f.write("\\fancyhead{}\n")
|
|
|
|
|
+ f.write("\\fancyhead[CE]{Mémoires: 2\\ieme{} partie}\n")
|
|
|
|
|
+ #f.write(f"\\fancyhead[CO]{{{s} {t}}}\n")
|
|
|
|
|
+ f.write(le_titre)
|
|
|
|
|
+ f.write(f"\\label{{{label}}}\n")
|
|
|
|
|
+
|
|
|
|
|
+#
|
|
|
|
|
+#---fin d'éciture du fichier tex
|
|
|
|
|
+#
|
|
|
|
|
+# lualatex, 2 fois si pas de bib, 3 fois + biber sinon:
|
|
|
|
|
+#
|
|
|
|
|
+if compile:
|
|
|
|
|
+ subprocess.run(["/usr/bin/lualatex", futurTex])
|
|
|
|
|
+ if has_bib:
|
|
|
|
|
+ subprocess.run(["/usr/bin/biber", futurTex.replace(".tex","")])
|
|
|
|
|
+ subprocess.run(["/usr/bin/lualatex", futurTex])
|
|
|
|
|
+ subprocess.run(["/usr/bin/lualatex", futurTex])
|
|
|
|
|
+#--- fin
|