formations/setup_main_tex_file.py

96 lines
2.9 KiB
Python
Executable File

#!/usr/bin/python
# -*- coding:utf-8 -*-
import argparse
import re
import random
import time
from os import path, makedirs
from jinja2 import Environment, FileSystemLoader
def get_unique_name(base):
now = time.localtime()
year = str(now[0])
month = str(now[1]).rjust(2, '0')
day = str(now[2]).rjust(2, '0')
rand = str(randint(0, 100)).rjust(2, '0')
return '-'.join([base, year, month, day, rand]).decode('utf-8')
def escape_tex(value):
newval = value
for pattern, replacement in LATEX_SUBS:
newval = pattern.sub(replacement, newval)
return newval
def main():
parser = argparse.ArgumentParser(description="Préparation du fichier maître tex")
parser.add_argument('-n', '--name', help="Nom du fichier à créer")
parser.add_argument('-a', '--author', help="Auteur de la formation")
parser.add_argument('-t', '--title', help="Titre de la formation")
parser.add_argument('-d', '--directory', help="Sous-répertoires où créer le fichier")
args = parser.parse_args()
jinja_loader = FileSystemLoader('./templates')
jinja_env = Environment(loader=jinja_loader,
block_start_string='((*',
block_end_string='*))',
variable_start_string='(((',
variable_end_string=')))',
comment_start_string='((=',
comment_end_string='=))',
trim_blocks=True)
jinja_env.filters['escape_tex'] = escape_tex
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'))
if __name__ == '__main__':
main()