#!/usr/bin/env python # -*- coding: utf-8 -*- """Template for .tex generation settings and utilities""" from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PackageLoader, TemplateNotFound from jinja2.meta import find_referenced_templates as find_templates import os import re import json from songbook_core import errors _LATEX_SUBS = ( (re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'([{}_#%&$])'), r'\\\1'), (re.compile(r'~'), r'\~{}'), (re.compile(r'\^'), r'\^{}'), (re.compile(r'"'), r"''"), (re.compile(r'\.\.\.+'), r'\\ldots'), ) def _escape_tex(value): '''Escape TeX special characters''' newval = value for pattern, replacement in _LATEX_SUBS: newval = pattern.sub(replacement, newval) return newval class TexRenderer(object): """Render a template to a LaTeX file.""" def __init__(self, template, datadir, lang): '''Start a new jinja2 environment for .tex creation. Arguments: - template: name of the template to use. - datadir: location of the data directory (which max contain file /templates/