retpl.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/home/moi/ProtoMemoires/venv/bin/python
  2. #
  3. # Moderniser un fichier source.
  4. #
  5. import yaml
  6. def dolabel(k,t):
  7. return str(hash(k+t)).replace("-","m")
  8. ft=input("fichier .tex ?")
  9. #
  10. # fichier de config:
  11. #
  12. with open("data.yml") as f:
  13. lt=yaml.safe_load(f)
  14. has_bib= "biblio"in lt.keys()
  15. #--------------------------------------------------------------------
  16. template="../tpl.tex" # le template
  17. #-----------------------------------------------------------------
  18. with open(template) as f:
  19. Ltmpl=f.readlines()
  20. #
  21. # ne garder du template que le début
  22. # et mentionner les ressouces pour biblatex
  23. #
  24. Lall=[]
  25. for l in Ltmpl:
  26. if "%@@debut" in l:
  27. break
  28. elif has_bib and "addbibresource" in l:
  29. # ajouter le chemin vers le fichier bib
  30. Lall.append(f"\\addbibresource{{{lt['biblio']}}}\n")
  31. Lall.append(l)
  32. #
  33. with open(ft) as f:
  34. LL=f.readlines()
  35. # insérer ce qui entre %@@debut et "%@@fin":
  36. start= False
  37. for x in LL:
  38. if "@@debut" in x:
  39. start= True
  40. print("--> start")
  41. if "@@fin" in x:
  42. break
  43. if start and "newcounter{none}" not in x:
  44. Lall.append(x)
  45. fr=input("fichier à construire ?")
  46. with open(fr,"w") as f:
  47. for x in Lall[:-1]:
  48. f.write(x)
  49. f.write("\\FloatBarrier\n")
  50. f.write("%@@fin\n")
  51. f.write("\\tableofcontents\n")
  52. f.write("\\end{document}\n")
  53. #---
  54. print("fin\n")