comparison src/tools/common/template.py @ 2465:bb0bbcc80fc8

template: boolean attribute can now be specified when importing a script, and default to "defer"
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 00:23:36 +0100
parents 06ff33052354
children d2e16a7466a0
comparison
equal deleted inserted replaced
2464:8788c217a49e 2465:bb0bbcc80fc8
153 self.template_root_dir = template_root_dir 153 self.template_root_dir = template_root_dir
154 self.root_path = root_path 154 self.root_path = root_path
155 self.scripts = [] # we don't use a set because order may be important 155 self.scripts = [] # we don't use a set because order may be important
156 dummy, self.theme, self.is_default_theme = renderer.getThemeData(template_path) 156 dummy, self.theme, self.is_default_theme = renderer.getThemeData(template_path)
157 157
158 def include(self, library_name): 158 def include(self, library_name, attribute='defer'):
159 """Mark that a script need to be imported. 159 """Mark that a script need to be imported.
160 160
161 Must be used before base.html is extended, as <script> are generated there. 161 Must be used before base.html is extended, as <script> are generated there.
162 If called several time with the same library, it will be imported once. 162 If called several time with the same library, it will be imported once.
163 @param library_name(unicode): name of the library to import 163 @param library_name(unicode): name of the library to import
164 """ 164 @param loading:
165 """
166 if attribute not in ('defer', 'async', ''):
167 raise exceptions.DataError(_(u'Invalid attribute, please use one of "defer", "async" or ""'))
165 if library_name.endswith('.js'): 168 if library_name.endswith('.js'):
166 library_name = library_name[:-3] 169 library_name = library_name[:-3]
167 if library_name not in self.scripts: 170 if library_name not in self.scripts:
168 self.scripts.append(library_name) 171 self.scripts.append((library_name, attribute))
169 return u'' 172 return u''
170 173
171 def generate_scripts(self): 174 def generate_scripts(self):
172 """Generate the <script> elements 175 """Generate the <script> elements
173 176
174 @return (unicode): <scripts> HTML tags 177 @return (unicode): <scripts> HTML tags
175 """ 178 """
176 scripts = [] 179 scripts = []
177 tpl = u'<script src={src}></script>' 180 tpl = u'<script src={src} {attribute}></script>'
178 for library in self.scripts: 181 for library, attribute in self.scripts:
179 path = self.renderer.getStaticPath(library, self.template_root_dir, self.theme, self.is_default_theme, '.js') 182 path = self.renderer.getStaticPath(library, self.template_root_dir, self.theme, self.is_default_theme, '.js')
180 if path is None: 183 if path is None:
181 log.warning(_(u"Can't find {}.js javascript library").format(library)) 184 log.warning(_(u"Can't find {}.js javascript library").format(library))
182 continue 185 continue
183 path = os.path.join(self.root_path, path) 186 path = os.path.join(self.root_path, path)
184 scripts.append(tpl.format(src=quoteattr(path))) 187 scripts.append(tpl.format(
188 src = quoteattr(path),
189 attribute = attribute,
190 ))
185 return safe(u'\n'.join(scripts)) 191 return safe(u'\n'.join(scripts))
186 192
187 193
188 class Renderer(object): 194 class Renderer(object):
189 195