tools (common/template): ScriptsHandler fix/improvments:
ScriptsHandler was not used so far, this commit fix it to be usable:
- script can be imported with "include"
- generate_scripts is used to generate the <script> elements generation, in base page
- paths use root_path, so they are adapted to context
- generate_scripts return a Markup string, so it's not escaped
This commit is contained in:
parent
9ae18d4d28
commit
92825fc674
1 changed files with 23 additions and 14 deletions
|
|
@ -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 <script> are generated there.
|
||||
If called several time with the same library, it will be imported once.
|
||||
@param library_name(unicode): name of the library to import
|
||||
"""
|
||||
if library_name.endswith('.js'):
|
||||
library_name = library_name[:-3]
|
||||
if library_name not in self.scripts:
|
||||
self.scripts[library_name] = {}
|
||||
self.scripts.append(library_name)
|
||||
return u''
|
||||
|
||||
def generate_scripts(self):
|
||||
"""Generate the <script> elements
|
||||
|
||||
def generate(self):
|
||||
"""Generate the <scripts> elements
|
||||
@return (unicode): <scripts> HTML tags
|
||||
"""
|
||||
scripts = []
|
||||
tpl = u'<script src="{src}"></script>'
|
||||
for library,data in self.scripts:
|
||||
tpl = u'<script src={src}></script>'
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue