Browse Source

Améliorations du moteur de template (#9)

- résolution d'un bug si le template choisi n'est pas dans datadir.
- création d'une classe templateRenderer
- correction de l'ordre de séléction des template (d'abord datadir, puis
  par défaut)
- chargement d'un langage par défaut pour que la compilation ne plante
  pas, même avec un carnet de chants vide
pull/15/head
Louis 11 years ago
parent
commit
6a61bb1a86
  1. 14
      songbook_core/build.py
  2. 6
      songbook_core/songs.py
  3. 62
      songbook_core/templates.py

14
songbook_core/build.py

@ -16,7 +16,7 @@ from songbook_core import errors
from songbook_core.files import recursive_find from songbook_core.files import recursive_find
from songbook_core.index import process_sxd from songbook_core.index import process_sxd
from songbook_core.songs import Song, SongsList from songbook_core.songs import Song, SongsList
from songbook_core.templates import render_tex from songbook_core.templates import TexRenderer
EOL = "\n" EOL = "\n"
DEFAULT_AUTHWORDS = { DEFAULT_AUTHWORDS = {
@ -129,17 +129,19 @@ class Songbook(object):
Arguments: Arguments:
- output: a file object, in which the file will be written. - output: a file object, in which the file will be written.
""" """
context = parse_template(os.path.join( renderer = TexRenderer(
self.config['template'],
self.config['datadir'], self.config['datadir'],
'templates', )
self.config['template']
)) context = parse_template(renderer.file_template())
context.update(self.config) context.update(self.config)
context['titleprefixkeys'] = ["after", "sep", "ignore"] context['titleprefixkeys'] = ["after", "sep", "ignore"]
context['songlist'] = self.songslist context['songlist'] = self.songslist
context['filename'] = output.name[:-4] context['filename'] = output.name[:-4]
render_tex(output, context, self.config['datadir'])
renderer.render_tex(output, context)
def clean(basename): def clean(basename):

6
songbook_core/songs.py

@ -128,4 +128,8 @@ class SongsList(object):
def languages(self): def languages(self):
"""Renvoie la liste des langues utilisées par les chansons""" """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'])

62
songbook_core/templates.py

@ -23,38 +23,50 @@ def _escape_tex(value):
newval = pattern.sub(replacement, newval) newval = pattern.sub(replacement, newval)
return newval return newval
class TexRenderer(object):
"""Render a template to a LaTeX file."""
def _init_tex_env(datadir=''): def __init__(self, template, datadir=''):
'''Start a new jinja2 environment for .tex creation''' '''Start a new jinja2 environment for .tex creation.
Arguments:
- datadir: location of the user-defined templates
'''
self.template = template
self.texenv = Environment(
loader=ChoiceLoader([ loader=ChoiceLoader([
PackageLoader('songbook_core', 'data/templates'), FileSystemLoader(
FileSystemLoader(os.path.join(datadir, 'templates')), os.path.join(datadir, 'templates')
),
PackageLoader(
'songbook_core', os.path.join('data', 'templates')
),
]) ])
texenv = Environment(loader=loader) )
texenv.block_start_string = '(*' self.texenv.block_start_string = '(*'
texenv.block_end_string = '*)' self.texenv.block_end_string = '*)'
texenv.variable_start_string = '((' self.texenv.variable_start_string = '(('
texenv.variable_end_string = '))' self.texenv.variable_end_string = '))'
texenv.comment_start_string = '(% comment %)' self.texenv.comment_start_string = '(% comment %)'
texenv.comment_end_string = '(% endcomment %)' self.texenv.comment_end_string = '(% endcomment %)'
texenv.filters['escape_tex'] = _escape_tex self.texenv.filters['escape_tex'] = _escape_tex
texenv.trim_blocks = True self.texenv.trim_blocks = True
texenv.lstrip_blocks = True self.texenv.lstrip_blocks = True
return texenv
def file_template(self):
"""Return the filename of the selected template."""
def render_tex(output, context, datadir=''): return self.texenv.get_template(self.template).filename
def render_tex(self, output, context):
'''Render a template into a .tex file '''Render a template into a .tex file
Arguments: Arguments:
- output: a file object to write the result - output: a file object to write the result
- context: all the data to populate the template - 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 #pylint: disable=star-args
output.write(content) output.write(
self.texenv.get_template(self.template).render(**context)
return None )

Loading…
Cancel
Save