Méthodes pour initialiser les fichiers de la formation

This commit is contained in:
Benjamin Bohard 2015-07-06 14:45:39 +02:00
parent 96e93882b2
commit 003cf96b4d
2 changed files with 94 additions and 51 deletions

View File

@ -25,13 +25,86 @@ def escape_tex(value):
def main(): def main():
parser = argparse.ArgumentParser(description="Préparation du fichier maître tex")
parser.add_argument('-n', '--name', help="Nom du fichier à créer") def init(args):
parser.add_argument('-a', '--author', help="Auteur de la formation") """
parser.add_argument('-t', '--title', help="Titre de la formation") init function
parser.add_argument('-d', '--directory', help="Sous-répertoires où créer le fichier") """
args = parser.parse_args() root = '../'
if args.directory:
root = root + re.sub(r'[\w]+/?', '../', args.directory)
else:
root = '../'
name = args.name
if name:
if path.splitext(name)[1] == '':
name = name.decode('utf-8') + u'.tex'
else:
name = get_unique_name('formation')
title = args.title
if not title:
title = u'FIXME'
else:
title = title.decode('utf-8')
author = args.author
if not author:
author = u'Cadoles'
else:
author = author.decode('utf-8')
directory = args.directory
if not directory:
directory = ''
env = {'root': root,
'title': title,
'author': author}
resources = [(path.join('presentations', directory), 'main.tex'),]
#('slides', 'license-cc-by-sa-2.0.tex'),
#('slides', 'preambule.tex'),
#('slides', 'title.tex')]
for directory, template_file in resources:
template = jinja_env.get_template(template_file)
rendered_template = template.render(**env)
if not path.exists(directory):
makedirs(directory)
with open(path.join(directory, name), 'w') as rendered_file:
rendered_file.write(rendered_template.encode('utf-8'))
def update(args):
"""
update function
"""
re_class = re.compile(r'\\documentclass\{(?P<document_class>.*)\}')
#skbconfig_re = re.compile(r'\\skbconfig\[\s*root\s*=\s*(?P<root>.*),\s*rep\s*=\s*(?P<rep>.*),\s*pub\s*=\s*(?P<pub>.*),\s*fig\s*=\s*(?P<fig>.*),\s*sli\s*=\s*(?P<sli>.*),\s*acr\s*=\s*(?P<acr>.*),\s*bib\s*=\s*(?P<bib>.*),\s*\]\{skblocal.tex\}', re.M)
skbconfig_re = re.compile(r'\\skbconfig\[\n\s*root\s*=\s*(?P<root>.*),\n\s*rep\s*=\s*(?P<rep>.*),\n\s*pub\s*=\s*(?P<pub>.*),\n\s*fig\s*=\s*(?P<fig>.*),\n\s*sli\s*=\s*(?P<sli>.*),\n\s*acr\s*=\s*(?P<acr>.*),\n\s*bib\s*=\s*(?P<bib>.*)\n\s*\]\{skblocal.tex\}', re.M)
skbinput_re = re.compile(r'\\skbinput\[from=(?P<rep>.*)\]\{(?P<tex>.*?)\}')
with open(args.master, 'r') as master:
tex_master = master.read()
tex_class = re_class.search(tex_master)
tex_skbconfig = skbconfig_re.search(tex_master)
tex_skbinputs = skbinput_re.finditer(tex_master)
for skbinput in tex_skbinputs:
rep = path.dirname(skbinput.group('tex'))
rep = path.join(tex_skbconfig.group(skbinput.group('rep')), rep)
basename = '{0}.tex'.format(skbinput.group('tex'))
dest = path.join(rep, basename)
if not path.isfile(dest):
if not path.isdir(rep):
print rep
makedirs(rep)
template = jinja_env.get_template('slide')
env = {'title': '', 'subtitle': ''}
rendered_template = template.render(**env)
with open(dest, 'w') as rendered_file:
rendered_file.write(rendered_template.encode('utf-8'))
jinja_loader = FileSystemLoader('./templates') jinja_loader = FileSystemLoader('./templates')
jinja_env = Environment(loader=jinja_loader, jinja_env = Environment(loader=jinja_loader,
block_start_string='((*', block_start_string='((*',
@ -43,50 +116,20 @@ def main():
trim_blocks=True) trim_blocks=True)
jinja_env.filters['escape_tex'] = escape_tex jinja_env.filters['escape_tex'] = escape_tex
root = '../' parser = argparse.ArgumentParser(description="Préparation des fichiers tex")
if args.directory: subparsers = parser.add_subparsers(help='Aide des sous-commandes')
root = root + re.sub(r'[\w]+/?', '../', args.directory) parser_init = subparsers.add_parser('init', help='Initialisation du fichier maître')
else: parser_init.add_argument('-n', '--name', help="Nom du fichier à créer", required=True)
root = '../' parser_init.add_argument('-a', '--author', help="Auteur de la formation")
parser_init.add_argument('-t', '--title', help="Titre de la formation")
parser_init.add_argument('-d', '--directory', help="Sous-répertoires où créer le fichier", required=True)
parser_init.set_defaults(func=init)
parser_update = subparsers.add_parser('update', help='Mise à jour des fichiers inclus')
parser_update.add_argument('-m', '--master', help="Emplacement du fichier maître", required=True)
parser_update.set_defaults(func=update)
args = parser.parse_args()
args.func(args)
name = args.name
if name:
if path.splitext(name)[1] == '':
name = name.decode('utf-8') + u'.tex'
else:
name = get_unique_name('formation')
title = args.title
if not title:
title = u'FIXME'
else:
title = title.decode('utf-8')
author = args.author
if not author:
author = u'Cadoles'
else:
author = author.decode('utf-8')
directory = args.directory
if not directory:
directory = ''
env = {'root': root,
'title': title,
'author': author}
resources = [(path.join('presentations', directory), 'main.tex'),]
#('slides', 'license-cc-by-sa-2.0.tex'),
#('slides', 'preambule.tex'),
#('slides', 'title.tex')]
for directory, template_file in resources:
template = jinja_env.get_template(template_file)
rendered_template = template.render(**env)
if not path.exists(directory):
makedirs(directory)
with open(path.join(directory, name), 'w') as rendered_file:
rendered_file.write(rendered_template.encode('utf-8'))

View File

@ -41,7 +41,7 @@
% Corps de la formation % Corps de la formation
%\skbinput[from=slides]{} %\skbinput[from=slides]{}
\skbinput[]{styles/license-cc-by-sa-2.0.tex} \skbinput[from=rep]{licences/(((license)))}
\end{document} \end{document}