code plus agréable

Cette révision appartient à :
Yann Salmon
2022-02-22 14:18:10 +01:00
Parent 6b38d2f986
révision 0115b18566

Voir le fichier

@@ -1,10 +1,23 @@
import os, shutil
def extraire_caml(nf_tex, mlref, nf_texo, tag) :
def nomFonctionCaml(ligne) :
lstrip = ligne.strip()
if lstrip.startswith("let") :
nomfonc = lstrip[4:]
if nomfonc.startswith("rec ") :
nomfonc = nomfonc[4:]
nomfonc = nomfonc.split()[0]
return nomfonc.split("=")[0]
else :
return None
def extraireCaml(nf_tex, mlref, tag) :
nf_tex = nf_tex.replace("file://", "")
nf_tex_bak = nf_tex + ".bak"
shutil.copy2(nf_tex, nf_tex_bak)
nf_ml = os.path.join(os.path.dirname(nf_tex), mlref)
nf_texo = nf_texo.replace("file://", "")
output_tex = open(nf_texo, "w")
contenu = open(nf_tex).readlines()
output_tex = open(nf_tex, "w")
output_ml = open(nf_ml, "w")
incode = False
nomfonc = ""
@@ -17,54 +30,36 @@ def extraire_caml(nf_tex, mlref, nf_texo, tag) :
else :
compteursNoms[s] += 1
return "{}__{}".format(s, compteursNoms[s])
for l in open(nf_tex) :
lstrip = l.strip()
if lstrip.startswith(r"\begin{"+tag+"}") :
incode = True
nomfonc = ""
complement = None
if "[" in lstrip :
complement = lstrip.split("[")[1].split("]")[0]
continue
if incode :
if nomfonc == "" :
if lstrip == "" :
continue
if "let" in lstrip :
nomfonc = l[4:]
if nomfonc.startswith("rec ") :
nomfonc = nomfonc[4:]
nomfonc = nomfonc.split()[0]
nomfonc = setNom(nomfonc)
else :
nomfonc = setNom("anon")
output_ml.write("(*** " + nomfonc + " ***)\n")
if lstrip == r"\end{"+tag+"}" :
output_ml.write("(*** " + nomfonc + "_fin ***)\n\n")
if complement is not None :
output_tex.write(r"\inputcaml["+nomfonc+"]{"+ mlref + "}["+complement+"]\n")
else :
output_tex.write(r"\inputcaml["+nomfonc+"]{"+ mlref + "}\n")
incode = False
continue
output_ml.write(l)
else :
output_tex.write(l)
def caml(s, start=0) :
try :
pos_let = s.index("\nlet", start)
fin = s.index(r";;", pos_let)
print(s[pos_let:fin+3])
if s[pos_let+4:].startswith(" rec") :
delta = 5
else :
delta = 0
pos_nom = s.index(" ", pos_let + delta) + 1
fin_nom = s.index(" ", pos_nom)
nom = s[pos_nom:fin_nom]
print(nom)
news = s[:pos_let] + "(*** " + nom + " ***)" + s[pos_let:fin+3] + "(*** " + nom + "_fin ***)\n" + s[fin+3:]
return caml(news, fin + 3 + len("(*** " + nom + " ***)")+ len("(*** " + nom + "_fin ***)\n"))
except ValueError :
return s
for l in contenu :
lstrip = l.strip()
if lstrip.startswith(r"\begin{"+tag+"}") :
incode = True
nomfonc = ""
complement = None
if "[" in lstrip :
complement = lstrip.split("[")[1].split("]")[0]
continue
if incode :
if nomfonc == "" :
if lstrip == "" :
continue
nomfonc = nomFonctionCaml(lstrip)
if nomfonc is None :
nomfonc = "anon"
nomfonc = setNom(nomfonc)
output_ml.write("(*** " + nomfonc + " ***)\n")
if lstrip == r"\end{"+tag+"}" :
output_ml.write("(*** " + nomfonc + "_fin ***)\n\n")
if complement is not None :
output_tex.write(r"\inputcaml["+nomfonc+"]{"+ mlref + "}["+complement+"]\n")
else :
output_tex.write(r"\inputcaml["+nomfonc+"]{"+ mlref + "}\n")
incode = False
continue
output_ml.write(l)
else :
output_tex.write(l)
finally :
output_ml.close()
output_tex.close()