|
@ -66,28 +66,28 @@ def _escape_tex(value): |
|
|
return newval |
|
|
return newval |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TexRenderer: |
|
|
class Renderer: |
|
|
"""Render a template to a LaTeX file.""" |
|
|
"""Render a template to a LaTeX file.""" |
|
|
# pylint: disable=too-few-public-methods |
|
|
# pylint: disable=too-few-public-methods |
|
|
|
|
|
|
|
|
def __init__(self, template, texenv, encoding=None): |
|
|
def __init__(self, template, jinjaenv, encoding=None): |
|
|
self.encoding = encoding |
|
|
self.encoding = encoding |
|
|
self.texenv = texenv |
|
|
self.jinjaenv = jinjaenv |
|
|
self.texenv.block_start_string = '(*' |
|
|
self.jinjaenv.block_start_string = '(*' |
|
|
self.texenv.block_end_string = '*)' |
|
|
self.jinjaenv.block_end_string = '*)' |
|
|
self.texenv.variable_start_string = '((' |
|
|
self.jinjaenv.variable_start_string = '((' |
|
|
self.texenv.variable_end_string = '))' |
|
|
self.jinjaenv.variable_end_string = '))' |
|
|
self.texenv.comment_start_string = '(% comment %)' |
|
|
self.jinjaenv.comment_start_string = '(% comment %)' |
|
|
self.texenv.comment_end_string = '(% endcomment %)' |
|
|
self.jinjaenv.comment_end_string = '(% endcomment %)' |
|
|
self.texenv.line_comment_prefix = '%!' |
|
|
self.jinjaenv.line_comment_prefix = '%!' |
|
|
self.texenv.filters['escape_tex'] = _escape_tex |
|
|
self.jinjaenv.filters['escape_tex'] = _escape_tex |
|
|
self.texenv.trim_blocks = True |
|
|
self.jinjaenv.trim_blocks = True |
|
|
self.texenv.lstrip_blocks = True |
|
|
self.jinjaenv.lstrip_blocks = True |
|
|
self.texenv.globals["path2posix"] = files.path2posix |
|
|
self.jinjaenv.globals["path2posix"] = files.path2posix |
|
|
self.template = self.texenv.get_template(template) |
|
|
self.template = self.jinjaenv.get_template(template) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TexBookRenderer(TexRenderer): |
|
|
class TexBookRenderer(Renderer): |
|
|
"""Tex renderer for the whole songbook""" |
|
|
"""Tex renderer for the whole songbook""" |
|
|
|
|
|
|
|
|
def __init__(self, template, datadirs, lang, encoding=None): |
|
|
def __init__(self, template, datadirs, lang, encoding=None): |
|
@ -106,17 +106,17 @@ class TexBookRenderer(TexRenderer): |
|
|
FileSystemLoader(os.path.join(datadir, 'templates')) |
|
|
FileSystemLoader(os.path.join(datadir, 'templates')) |
|
|
for datadir in datadirs |
|
|
for datadir in datadirs |
|
|
] |
|
|
] |
|
|
texenv = Environment( |
|
|
jinjaenv = Environment( |
|
|
loader=ChoiceLoader(loaders), |
|
|
loader=ChoiceLoader(loaders), |
|
|
extensions=[VariablesExtension], |
|
|
extensions=[VariablesExtension], |
|
|
) |
|
|
) |
|
|
try: |
|
|
try: |
|
|
super().__init__(template, texenv, encoding) |
|
|
super().__init__(template, jinjaenv, encoding) |
|
|
except TemplateNotFound as exception: |
|
|
except TemplateNotFound as exception: |
|
|
# Only works if all loaders are FileSystemLoader(). |
|
|
# Only works if all loaders are FileSystemLoader(). |
|
|
paths = [ |
|
|
paths = [ |
|
|
item |
|
|
item |
|
|
for loader in self.texenv.loader.loaders |
|
|
for loader in self.jinjaenv.loader.loaders |
|
|
for item in loader.searchpath |
|
|
for item in loader.searchpath |
|
|
] |
|
|
] |
|
|
raise errors.TemplateError( |
|
|
raise errors.TemplateError( |
|
@ -199,13 +199,13 @@ class TexBookRenderer(TexRenderer): |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
subvariables = {} |
|
|
subvariables = {} |
|
|
templatename = self.texenv.get_template(template).filename |
|
|
templatename = self.jinjaenv.get_template(template).filename |
|
|
with patacrep.encoding.open_read( |
|
|
with patacrep.encoding.open_read( |
|
|
templatename, |
|
|
templatename, |
|
|
encoding=self.encoding |
|
|
encoding=self.encoding |
|
|
) as template_file: |
|
|
) as template_file: |
|
|
content = template_file.read() |
|
|
content = template_file.read() |
|
|
subtemplates = list(find_templates(self.texenv.parse(content))) |
|
|
subtemplates = list(find_templates(self.jinjaenv.parse(content))) |
|
|
match = re.findall(_VARIABLE_REGEXP, content) |
|
|
match = re.findall(_VARIABLE_REGEXP, content) |
|
|
if match: |
|
|
if match: |
|
|
for var in match: |
|
|
for var in match: |
|
|