add new engine creole_legacy
This commit is contained in:
parent
f4538004c7
commit
651d89dd04
4 changed files with 136 additions and 19 deletions
|
@ -67,7 +67,7 @@
|
||||||
<!ATTLIST file group CDATA "root">
|
<!ATTLIST file group CDATA "root">
|
||||||
<!ATTLIST file filelist CDATA #IMPLIED>
|
<!ATTLIST file filelist CDATA #IMPLIED>
|
||||||
<!ATTLIST file redefine (True|False) "False">
|
<!ATTLIST file redefine (True|False) "False">
|
||||||
<!ATTLIST file engine (none|creole|jinja2) #IMPLIED>
|
<!ATTLIST file engine (none|creole|jinja2|creole_legacy) #IMPLIED>
|
||||||
<!ATTLIST file included (no|name|content) "no">
|
<!ATTLIST file included (no|name|content) "no">
|
||||||
|
|
||||||
<!ELEMENT override EMPTY>
|
<!ELEMENT override EMPTY>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from . import none, creole, jinja2
|
from . import none, creole, jinja2, creole_legacy
|
||||||
|
|
||||||
|
|
||||||
__all__ = ('none', 'creole', 'jinja2')
|
__all__ = ('none', 'creole', 'jinja2', 'creole_legacy')
|
||||||
|
|
|
@ -34,25 +34,16 @@ from ...utils import normalize_family
|
||||||
from ...error import TemplateError
|
from ...error import TemplateError
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def cl_compile(kls, *args, **kwargs):
|
|
||||||
"""Rewrite compile methode to force some settings
|
|
||||||
"""
|
|
||||||
kwargs['compilerSettings'] = {'directiveStartToken' : '%',
|
|
||||||
'cheetahVarStartToken' : '%%',
|
|
||||||
'EOLSlurpToken' : '%',
|
|
||||||
'commentStartToken' : '#',
|
|
||||||
'multiLineCommentStartToken' : '#*',
|
|
||||||
'multiLineCommentEndToken' : '*#',
|
|
||||||
}
|
|
||||||
return kls.old_compile(*args, **kwargs) # pylint: disable=E1101
|
|
||||||
Template.old_compile = Template.compile
|
|
||||||
Template.compile = cl_compile
|
|
||||||
|
|
||||||
|
|
||||||
class CheetahTemplate(Template): # pylint: disable=W0223
|
class CheetahTemplate(Template): # pylint: disable=W0223
|
||||||
"""Construct a cheetah templating object
|
"""Construct a cheetah templating object
|
||||||
"""
|
"""
|
||||||
|
compilerSettings = {'directiveStartToken' : '%',
|
||||||
|
'cheetahVarStartToken' : '%%',
|
||||||
|
'EOLSlurpToken' : '%',
|
||||||
|
'commentStartToken' : '#',
|
||||||
|
'multiLineCommentStartToken' : '#*',
|
||||||
|
'multiLineCommentEndToken' : '*#',
|
||||||
|
}
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
filename: str,
|
filename: str,
|
||||||
source: str,
|
source: str,
|
||||||
|
@ -66,11 +57,13 @@ class CheetahTemplate(Template): # pylint: disable=W0223
|
||||||
Template.__init__(self,
|
Template.__init__(self,
|
||||||
file=filename,
|
file=filename,
|
||||||
searchList=[context, eosfunc, extra_context],
|
searchList=[context, eosfunc, extra_context],
|
||||||
|
compilerSettings=self.compilerSettings,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
Template.__init__(self,
|
Template.__init__(self,
|
||||||
source=source,
|
source=source,
|
||||||
searchList=[context, eosfunc, extra_context],
|
searchList=[context, eosfunc, extra_context],
|
||||||
|
compilerSettings=self.compilerSettings,
|
||||||
)
|
)
|
||||||
|
|
||||||
# FORK of Cheetah function, do not replace '\\' by '/'
|
# FORK of Cheetah function, do not replace '\\' by '/'
|
||||||
|
@ -92,6 +85,7 @@ class CheetahTemplate(Template): # pylint: disable=W0223
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# Sync to creole_legacy.py
|
||||||
def process(filename: str,
|
def process(filename: str,
|
||||||
source: str,
|
source: str,
|
||||||
true_destfilename: str,
|
true_destfilename: str,
|
||||||
|
|
123
src/rougail/template/engine/creole_legacy.py
Normal file
123
src/rougail/template/engine/creole_legacy.py
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
"""Legacy Creole 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 Dict, Any
|
||||||
|
from Cheetah.NameMapper import NotFound
|
||||||
|
|
||||||
|
from .creole import CheetahTemplate as oriCheetahTemplate
|
||||||
|
from ...i18n import _
|
||||||
|
from ...utils import normalize_family
|
||||||
|
from ...error import TemplateError
|
||||||
|
|
||||||
|
|
||||||
|
class IsDefined:
|
||||||
|
"""
|
||||||
|
filtre permettant de ne pas lever d'exception au cas où
|
||||||
|
la variable Creole n'est pas définie
|
||||||
|
"""
|
||||||
|
def __init__(self, context):
|
||||||
|
self.context = context
|
||||||
|
|
||||||
|
def __call__(self, varname):
|
||||||
|
if '.' in varname:
|
||||||
|
splitted_var = varname.split('.')
|
||||||
|
if len(splitted_var) != 2:
|
||||||
|
msg = u"Group variables must be of type master.slave"
|
||||||
|
raise KeyError(msg)
|
||||||
|
master, slave = splitted_var
|
||||||
|
if master in self.context:
|
||||||
|
return slave in self.context[master].slave.keys()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return varname in self.context
|
||||||
|
|
||||||
|
|
||||||
|
class CheetahTemplate(oriCheetahTemplate):
|
||||||
|
compilerSettings = {'directiveStartToken' : '%',
|
||||||
|
'cheetahVarStartToken' : '%%',
|
||||||
|
'EOLSlurpToken' : '%',
|
||||||
|
'PSPStartToken' : 'µ' * 10,
|
||||||
|
'PSPEndToken' : 'µ' * 10,
|
||||||
|
'commentStartToken' : 'µ' * 10,
|
||||||
|
'commentEndToken' : 'µ' * 10,
|
||||||
|
'multiLineCommentStartToken' : 'µ' * 10,
|
||||||
|
'multiLineCommentEndToken' : 'µ' * 10}
|
||||||
|
def __init__(self,
|
||||||
|
filename: str,
|
||||||
|
source: str,
|
||||||
|
context,
|
||||||
|
eosfunc: Dict,
|
||||||
|
extra_context: Dict,
|
||||||
|
):
|
||||||
|
extra_context['is_defined'] = IsDefined(context)
|
||||||
|
super().__init__(filename, source, context, eosfunc, extra_context)
|
||||||
|
|
||||||
|
|
||||||
|
# Sync to creole.py
|
||||||
|
def process(filename: str,
|
||||||
|
source: str,
|
||||||
|
true_destfilename: str,
|
||||||
|
destfilename: str,
|
||||||
|
destdir: str,
|
||||||
|
variable: Any,
|
||||||
|
rougail_variables_dict: Dict,
|
||||||
|
eosfunc: Dict,
|
||||||
|
):
|
||||||
|
"""Process a cheetah template
|
||||||
|
"""
|
||||||
|
# full path of the destination file
|
||||||
|
try:
|
||||||
|
extra_context = {'normalize_family': normalize_family,
|
||||||
|
'rougail_filename': true_destfilename,
|
||||||
|
'rougail_destination_dir': destdir,
|
||||||
|
}
|
||||||
|
if variable is not None:
|
||||||
|
extra_context['rougail_variable'] = variable
|
||||||
|
cheetah_template = CheetahTemplate(filename,
|
||||||
|
source,
|
||||||
|
rougail_variables_dict,
|
||||||
|
eosfunc,
|
||||||
|
extra_context,
|
||||||
|
)
|
||||||
|
data = str(cheetah_template)
|
||||||
|
except NotFound as err: # pragma: no cover
|
||||||
|
varname = err.args[0][13:-1]
|
||||||
|
if filename:
|
||||||
|
msg = f"Error: unknown variable used in template {filename} to {destfilename}: {varname}"
|
||||||
|
else:
|
||||||
|
msg = f"Error: unknown variable used in file {destfilename}: {varname}"
|
||||||
|
raise TemplateError(_(msg)) from err
|
||||||
|
except Exception as err: # pragma: no cover
|
||||||
|
if filename:
|
||||||
|
msg = _(f"Error while instantiating template {filename} to {destfilename}: {err}")
|
||||||
|
else:
|
||||||
|
msg = _(f"Error while instantiating filename {destfilename}: {err}")
|
||||||
|
raise TemplateError(msg) from err
|
||||||
|
|
||||||
|
with open(destfilename, 'w') as file_h:
|
||||||
|
file_h.write(data)
|
Loading…
Reference in a new issue