comparison src/tools/common/template.py @ 2385:39d30cf722cb

template: gidx methods improvment: gidx methods now use the filtered name to return global indexes. For instance, if "widget" is filtered name, first index will be "widget", then "widget_1", "widget_2" and so one. If an other name is used, it restart (e.g. for "label": "label", "label_1", "label_2", etc.).
author Goffi <goffi@goffi.org>
date Mon, 16 Oct 2017 07:48:09 +0200
parents d14c1a3e3244
children 07e1543d6992
comparison
equal deleted inserted replaced
2384:d14c1a3e3244 2385:39d30cf722cb
124 124
125 class Indexer(object): 125 class Indexer(object):
126 """Index global to a page""" 126 """Index global to a page"""
127 127
128 def __init__(self): 128 def __init__(self):
129 self._idx = 0 129 self._indexes = {}
130 130
131 def next(self): 131 def next(self, value):
132 self._idx+=1 132 if value not in self._indexes:
133 return self._idx 133 self._indexes[value] = 0
134 134 return 0
135 def current(self): 135 self._indexes[value] += 1
136 return self._idx 136 return self._indexes[value]
137
138 def current(self, value):
139 return self._indexes.get(value)
137 140
138 141
139 class ScriptsHandler(object): 142 class ScriptsHandler(object):
140 143
141 def __init__(self, renderer, template_path, template_root_dir, root_path): 144 def __init__(self, renderer, template_path, template_root_dir, root_path):
317 return css_files 320 return css_files
318 321
319 @jinja2.contextfilter 322 @jinja2.contextfilter
320 def _next_gidx(self, ctx, value): 323 def _next_gidx(self, ctx, value):
321 """Use next current global index as suffix""" 324 """Use next current global index as suffix"""
322 return u"{}_{}".format(value, ctx['gidx'].next()) 325 next_ = ctx['gidx'].next(value)
326 return value if next_ == 0 else u"{}_{}".format(value, next_)
323 327
324 @jinja2.contextfilter 328 @jinja2.contextfilter
325 def _cur_gidx(self, ctx, value): 329 def _cur_gidx(self, ctx, value):
326 """Use current current global index as suffix""" 330 """Use current current global index as suffix"""
327 return u"{}_{}".format(value, ctx['gidx'].current()) 331 current = ctx['gidx'].current(value)
332 return value if not current else u"{}_{}".format(value, current)
328 333
329 def _date_days(self, timestamp): 334 def _date_days(self, timestamp):
330 return int(time.time() - int(timestamp))/(3600*24) 335 return int(time.time() - int(timestamp))/(3600*24)
331 336
332 def attr_escape(self, text): 337 def attr_escape(self, text):