add jinja2 templating
This commit is contained in:
@ -66,11 +66,11 @@
|
||||
<!ATTLIST file group CDATA "root">
|
||||
<!ATTLIST file filelist CDATA #IMPLIED>
|
||||
<!ATTLIST file redefine (True|False) "False">
|
||||
<!ATTLIST file engine (none|creole) #IMPLIED>
|
||||
<!ATTLIST file engine (none|creole|jinja2) #IMPLIED>
|
||||
|
||||
<!ELEMENT override EMPTY>
|
||||
<!ATTLIST override source CDATA #IMPLIED>
|
||||
<!ATTLIST override engine (none|creole) #IMPLIED>
|
||||
<!ATTLIST override engine (none|creole|jinja2) #IMPLIED>
|
||||
|
||||
<!ELEMENT variables ((variable*|family*)*)>
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
from . import none, creole
|
||||
from . import none, creole, jinja2
|
||||
|
||||
|
||||
__all__ = ('none', 'creole')
|
||||
__all__ = ('none', 'creole', 'jinja2')
|
||||
|
69
src/rougail/template/engine/jinja2.py
Normal file
69
src/rougail/template/engine/jinja2.py
Normal file
@ -0,0 +1,69 @@
|
||||
"""Jinja2 engine
|
||||
|
||||
Created by:
|
||||
EOLE (http://eole.orion.education.fr)
|
||||
Copyright (C) 2005-2018
|
||||
|
||||
Forked by:
|
||||
Cadoles (http://www.cadoles.com)
|
||||
Copyright (C) 2021
|
||||
|
||||
distribued with GPL-2 or later license
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
"""
|
||||
from typing import Any, Dict
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from jinja2.exceptions import UndefinedError
|
||||
|
||||
from ...i18n import _
|
||||
from ...utils import normalize_family
|
||||
from ...error import TemplateError
|
||||
|
||||
|
||||
def process(filename: str,
|
||||
source: str,
|
||||
true_destfilename: str,
|
||||
destfilename: str,
|
||||
variable: Any,
|
||||
rougail_variables_dict: Dict,
|
||||
eosfunc: Dict,
|
||||
):
|
||||
"""Process a cheetah template
|
||||
"""
|
||||
# full path of the destination file
|
||||
dir_name, template_name = filename.rsplit('/', 1)
|
||||
if source is not None: # pragma: no cover
|
||||
raise TemplateError(_('source is not supported for jinja2'))
|
||||
rougail_variables_dict['rougail_variable'] = variable
|
||||
if variable is not None:
|
||||
var = {'rougail_variable': variable}
|
||||
else:
|
||||
var = {}
|
||||
try:
|
||||
# extra_context = {'normalize_family': normalize_family,
|
||||
# eosfunc
|
||||
env = Environment(loader=FileSystemLoader(dir_name))
|
||||
template = env.get_template(template_name)
|
||||
data = template.render(**rougail_variables_dict, rougail_filename=true_destfilename, **var)
|
||||
except UndefinedError as err: # pragma: no cover
|
||||
varname = err
|
||||
msg = f"Error: unknown variable used in template {filename} to {destfilename}: {varname}"
|
||||
raise TemplateError(_(msg)) from err
|
||||
|
||||
if not data.endswith('\n'):
|
||||
data = data + '\n'
|
||||
with open(destfilename, 'w') as file_h:
|
||||
file_h.write(data)
|
Reference in New Issue
Block a user