|
@ -9,7 +9,8 @@ import os |
|
|
import re |
|
|
import re |
|
|
import json |
|
|
import json |
|
|
|
|
|
|
|
|
from patacrep import encoding, errors, files |
|
|
from patacrep import errors, files |
|
|
|
|
|
import patacrep.encoding |
|
|
|
|
|
|
|
|
_LATEX_SUBS = ( |
|
|
_LATEX_SUBS = ( |
|
|
(re.compile(r'\\'), r'\\textbackslash'), |
|
|
(re.compile(r'\\'), r'\\textbackslash'), |
|
@ -67,7 +68,7 @@ def _escape_tex(value): |
|
|
class TexRenderer(object): |
|
|
class TexRenderer(object): |
|
|
"""Render a template to a LaTeX file.""" |
|
|
"""Render a template to a LaTeX file.""" |
|
|
|
|
|
|
|
|
def __init__(self, template, datadirs, lang): |
|
|
def __init__(self, template, datadirs, lang, encoding=None): |
|
|
'''Start a new jinja2 environment for .tex creation. |
|
|
'''Start a new jinja2 environment for .tex creation. |
|
|
|
|
|
|
|
|
Arguments: |
|
|
Arguments: |
|
@ -75,8 +76,10 @@ class TexRenderer(object): |
|
|
- datadirs: list of locations of the data directory |
|
|
- datadirs: list of locations of the data directory |
|
|
(which may contain file <datadir>/templates/<template>). |
|
|
(which may contain file <datadir>/templates/<template>). |
|
|
- lang: main language of songbook. |
|
|
- lang: main language of songbook. |
|
|
|
|
|
- encoding: if set, encoding of the template. |
|
|
''' |
|
|
''' |
|
|
self.lang = lang |
|
|
self.lang = lang |
|
|
|
|
|
self.encoding = encoding |
|
|
# Load templates in filesystem ... |
|
|
# Load templates in filesystem ... |
|
|
loaders = [FileSystemLoader(os.path.join(datadir, 'templates')) |
|
|
loaders = [FileSystemLoader(os.path.join(datadir, 'templates')) |
|
|
for datadir in datadirs] |
|
|
for datadir in datadirs] |
|
@ -186,10 +189,11 @@ class TexRenderer(object): |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
subvariables = {} |
|
|
subvariables = {} |
|
|
template_file = None |
|
|
|
|
|
templatename = self.texenv.get_template(template).filename |
|
|
templatename = self.texenv.get_template(template).filename |
|
|
try: |
|
|
with patacrep.encoding.open_read( |
|
|
template_file = encoding.open_read(templatename, 'r') |
|
|
templatename, |
|
|
|
|
|
encoding=self.encoding |
|
|
|
|
|
) as template_file: |
|
|
content = template_file.read() |
|
|
content = template_file.read() |
|
|
subtemplates = list(find_templates(self.texenv.parse(content))) |
|
|
subtemplates = list(find_templates(self.texenv.parse(content))) |
|
|
match = re.findall(_VARIABLE_REGEXP, content) |
|
|
match = re.findall(_VARIABLE_REGEXP, content) |
|
@ -209,9 +213,6 @@ class TexRenderer(object): |
|
|
jsonstring=var, |
|
|
jsonstring=var, |
|
|
) |
|
|
) |
|
|
) |
|
|
) |
|
|
finally: |
|
|
|
|
|
if template_file: |
|
|
|
|
|
template_file.close() |
|
|
|
|
|
|
|
|
|
|
|
return (subvariables, subtemplates) |
|
|
return (subvariables, subtemplates) |
|
|
|
|
|
|
|
|