diff --git a/src/tools/common/template.py b/src/tools/common/template.py index f3a0eb3c..b3aec1e5 100644 --- a/src/tools/common/template.py +++ b/src/tools/common/template.py @@ -25,7 +25,6 @@ from sat.core import exceptions from sat.core.log import getLogger log = getLogger(__name__) import os.path -from collections import OrderedDict from xml.sax.saxutils import quoteattr import time from babel import support @@ -43,6 +42,8 @@ try: except: raise exceptions.MissingModule(u'Missing module jinja2, please install it from http://jinja.pocoo.org or with pip install jinja2') +from jinja2 import Markup as safe + HTML_EXT = ('html', 'xhtml') DEFAULT_LOCALE = u'en' # TODO: handle external path (an additional search path for templates should be settable by user @@ -136,34 +137,42 @@ class Indexer(object): class ScriptsHandler(object): - # TODO: this class is not finished/used yet, and add_script is referenced in default/script/base.html - # but doesn't exist yet here - def __init__(self, renderer, template_path, template_root_dir): + def __init__(self, renderer, template_path, template_root_dir, root_path): self.renderer = renderer self.template_root_dir = template_root_dir - self.scripts = OrderedDict + self.root_path = root_path + self.scripts = [] # we don't use a set because order may be important dummy, self.theme, self.is_default_theme = renderer.getThemeData(template_path) - def import_script(self, library_name): + def include(self, library_name): + """Mark that a script need to be imported. + + Must be used before base.html is extended, as ' - for library,data in self.scripts: + tpl = u'' + for library in self.scripts: path = self.renderer.getStaticPath(library, self.template_root_dir, self.theme, self.is_default_theme, '.js') if path is None: log.warning(_(u"Can't find {}.js javascript library").format(library)) continue + path = os.path.join(self.root_path, path) scripts.append(tpl.format(src=quoteattr(path))) - return u'\n'.join(scripts) + return safe(u'\n'.join(scripts)) class Renderer(object): @@ -357,8 +366,8 @@ class Renderer(object): if css_contents: kwargs['css_content'] = '\n'.join(css_contents) - scripts_handler = ScriptsHandler(self, template_path, template_root_dir) + scripts_handler = ScriptsHandler(self, template_path, template_root_dir, root_path) self.setLocale(locale) # XXX: theme used in template arguments is the requested theme, which may differ from actual theme # if the template doesn't exist in the requested theme. - return template_source.render(theme=theme, root_path=root_path, css_files=css_files, locale=locale, gidx=Indexer(), scripts_handler=scripts_handler, **kwargs) + return template_source.render(theme=theme, root_path=root_path, css_files=css_files, locale=self._locale, gidx=Indexer(), script=scripts_handler, **kwargs)