Last active
August 29, 2021 17:10
-
-
Save ThibGiauffret/9235301afdec319e16c838e53eba2c50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from subprocess import call | |
import os | |
from pathlib import Path | |
# On precise le dossier contenant le tex (attention à la syntaxe !) ainsi que le nom du fichier | |
compiler = 'lualatex' | |
tex_path = r'./exemple/' | |
tex_file = r'master.tex' | |
full_path = str(Path(tex_path+tex_file)) | |
# On teste si le fichier existe. | |
if os.path.exists(full_path): | |
os.chdir(os.path.normpath(tex_path)) | |
# On l'ouvre pour lecture ("r") | |
f = open(tex_file,"r") | |
# On récupère chacune des lignes et on les stocke dans une variable | |
contents = f.readlines() | |
c = 0 | |
audiences = [] | |
# On parcourt toutes les lignes | |
for i in range (0,len(contents),1) : | |
line = str(contents[i]) | |
# On recherche les déclarations d'audiences | |
check = line.find("SetNewAudience") | |
if check != -1 : | |
m = re.search(r"\{([A-Za-z0-9_]+)\}", contents[i]) | |
# On ajoute les audiences trouvées à la liste | |
audiences.append(m.group(1)) | |
# On vérifie que l'on ait bien trouvé des audiences | |
if audiences == [] : | |
print('Pas d\'audiences trouvées... La compilation simple du fichier master va être lancée !') | |
# On change l'emplacement pour l'emplacement du fichier tex | |
call('cd ' + tex_path, shell=True) | |
options = '"\input{'+tex_file+'}"' | |
# On lance la compilation en précisant le moteur de compilation ainsi que les options | |
call([compiler, jobname, options]) | |
else : | |
print('Les audiences trouvées sont : ' + str(audiences) + '.') | |
for j in audiences : | |
call('cd ' + tex_path, shell=True) | |
jobname = '-jobname=' + j | |
# Ici deux points particuliers. "\def\ismulticompile{1}" indique à LaTeX qu'il s'agit du | |
# logiciel MultiCompile et permet d'ignorer les commandes "\def\CurrentAudience{...}" | |
# dans le document | |
options = '"\def\ismulticompile{1}\def\CurrentAudience{' + j + '}\input{'+tex_file+'}"' | |
call([compiler, jobname, options]) | |
print('Compilation terminée.') | |
else: | |
print("Le fichier tex n'existe pas !") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment