diff --git a/songbook_core/build.py b/songbook_core/build.py index 03f0f557..e16703af 100755 --- a/songbook_core/build.py +++ b/songbook_core/build.py @@ -16,7 +16,7 @@ from songbook_core import errors from songbook_core.files import recursive_find from songbook_core.index import process_sxd from songbook_core.songs import Song, SongsList -from songbook_core.templates import render_tex +from songbook_core.templates import TexRenderer EOL = "\n" DEFAULT_AUTHWORDS = { @@ -129,17 +129,19 @@ class Songbook(object): Arguments: - output: a file object, in which the file will be written. """ - context = parse_template(os.path.join( - self.config['datadir'], - 'templates', - self.config['template'] - )) + renderer = TexRenderer( + self.config['template'], + self.config['datadir'], + ) + + context = parse_template(renderer.file_template()) context.update(self.config) context['titleprefixkeys'] = ["after", "sep", "ignore"] context['songlist'] = self.songslist context['filename'] = output.name[:-4] - render_tex(output, context, self.config['datadir']) + + renderer.render_tex(output, context) def clean(basename): diff --git a/songbook_core/songs.py b/songbook_core/songs.py index f0c8811b..ed047e8c 100644 --- a/songbook_core/songs.py +++ b/songbook_core/songs.py @@ -128,4 +128,8 @@ class SongsList(object): def languages(self): """Renvoie la liste des langues utilisées par les chansons""" - return set().union(*[set(song.languages) for song in self.songs]) + languages = set().union(*[set(song.languages) for song in self.songs]) + if languages: + return languages + else: + return set(['english']) diff --git a/songbook_core/templates.py b/songbook_core/templates.py index 0af2afc2..94107cb1 100644 --- a/songbook_core/templates.py +++ b/songbook_core/templates.py @@ -23,38 +23,50 @@ def _escape_tex(value): newval = pattern.sub(replacement, newval) return newval +class TexRenderer(object): + """Render a template to a LaTeX file.""" -def _init_tex_env(datadir=''): - '''Start a new jinja2 environment for .tex creation''' - loader = ChoiceLoader([ - PackageLoader('songbook_core', 'data/templates'), - FileSystemLoader(os.path.join(datadir, 'templates')), - ]) - texenv = Environment(loader=loader) - texenv.block_start_string = '(*' - texenv.block_end_string = '*)' - texenv.variable_start_string = '((' - texenv.variable_end_string = '))' - texenv.comment_start_string = '(% comment %)' - texenv.comment_end_string = '(% endcomment %)' - texenv.filters['escape_tex'] = _escape_tex - texenv.trim_blocks = True - texenv.lstrip_blocks = True - return texenv - - -def render_tex(output, context, datadir=''): - '''Render a template into a .tex file - - Arguments: - - output: a file object to write the result - - context: all the data to populate the template - - datadir: location of the user-defined templates - ''' - env = _init_tex_env(datadir=datadir) - template = env.get_template(context['template']) - - content = template.render(**context) #pylint: disable=star-args - output.write(content) - - return None + def __init__(self, template, datadir=''): + '''Start a new jinja2 environment for .tex creation. + + Arguments: + - datadir: location of the user-defined templates + ''' + self.template = template + + self.texenv = Environment( + loader=ChoiceLoader([ + FileSystemLoader( + os.path.join(datadir, 'templates') + ), + PackageLoader( + 'songbook_core', os.path.join('data', 'templates') + ), + ]) + ) + self.texenv.block_start_string = '(*' + self.texenv.block_end_string = '*)' + self.texenv.variable_start_string = '((' + self.texenv.variable_end_string = '))' + self.texenv.comment_start_string = '(% comment %)' + self.texenv.comment_end_string = '(% endcomment %)' + self.texenv.filters['escape_tex'] = _escape_tex + self.texenv.trim_blocks = True + self.texenv.lstrip_blocks = True + + def file_template(self): + """Return the filename of the selected template.""" + return self.texenv.get_template(self.template).filename + + def render_tex(self, output, context): + '''Render a template into a .tex file + + Arguments: + - output: a file object to write the result + - context: all the data to populate the template + ''' + + #pylint: disable=star-args + output.write( + self.texenv.get_template(self.template).render(**context) + )