@ -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 = ' ( % c omment % ) '
os . path . join ( datadir , ' templates ' )
texenv . comment_end_string = ' ( % e ndcomment % ) '
) ,
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 = ' ( % c omment % ) '
- output : a file object to write the result
self . texenv . comment_end_string = ' ( % e ndcomment % ) '
- 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 )
)