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. 16
      songbook_core/build.py
  2. 6
      songbook_core/songs.py
  3. 80
      songbook_core/templates.py

16
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['datadir'], self.config['template'],
'templates', self.config['datadir'],
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'])

80
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.
loader = ChoiceLoader([
PackageLoader('songbook_core', 'data/templates'), Arguments:
FileSystemLoader(os.path.join(datadir, 'templates')), - datadir: location of the user-defined templates
]) '''
texenv = Environment(loader=loader) self.template = template
texenv.block_start_string = '(*'
texenv.block_end_string = '*)' self.texenv = Environment(
texenv.variable_start_string = '((' loader=ChoiceLoader([
texenv.variable_end_string = '))' FileSystemLoader(
texenv.comment_start_string = '(% comment %)' os.path.join(datadir, 'templates')
texenv.comment_end_string = '(% endcomment %)' ),
texenv.filters['escape_tex'] = _escape_tex PackageLoader(
texenv.trim_blocks = True 'songbook_core', os.path.join('data', 'templates')
texenv.lstrip_blocks = True ),
return texenv ])
)
self.texenv.block_start_string = '(*'
def render_tex(output, context, datadir=''): self.texenv.block_end_string = '*)'
'''Render a template into a .tex file self.texenv.variable_start_string = '(('
self.texenv.variable_end_string = '))'
Arguments: self.texenv.comment_start_string = '(% comment %)'
- output: a file object to write the result self.texenv.comment_end_string = '(% endcomment %)'
- context: all the data to populate the template self.texenv.filters['escape_tex'] = _escape_tex
- datadir: location of the user-defined templates self.texenv.trim_blocks = True
''' self.texenv.lstrip_blocks = True
env = _init_tex_env(datadir=datadir)
template = env.get_template(context['template']) def file_template(self):
"""Return the filename of the selected template."""
content = template.render(**context) #pylint: disable=star-args return self.texenv.get_template(self.template).filename
output.write(content)
def render_tex(self, output, context):
return None '''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)
)

Loading…
Cancel
Save