comparison mercurial/formatter.py @ 33090:04b3743c1d7c

formatter: proxy fm.context() through converter Otherwise nested template formatter would not see the context objects. It's just a boolean flag now. We might want to change it to 'ctxs -> items' function so changectx attributes are populated automatically in JSON, but I'm not sure.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 26 Jun 2017 09:33:01 +0900
parents 61b60b28c381
children 0fa781320203
comparison
equal deleted inserted replaced
33089:9e0d222f5687 33090:04b3743c1d7c
125 125
126 pickle = util.pickle 126 pickle = util.pickle
127 127
128 class _nullconverter(object): 128 class _nullconverter(object):
129 '''convert non-primitive data types to be processed by formatter''' 129 '''convert non-primitive data types to be processed by formatter'''
130
131 # set to True if context object should be stored as item
132 storecontext = False
133
130 @staticmethod 134 @staticmethod
131 def formatdate(date, fmt): 135 def formatdate(date, fmt):
132 '''convert date tuple to appropriate format''' 136 '''convert date tuple to appropriate format'''
133 return date 137 return date
134 @staticmethod 138 @staticmethod
176 # name is mandatory argument for now, but it could be optional if 180 # name is mandatory argument for now, but it could be optional if
177 # we have default template keyword, e.g. {item} 181 # we have default template keyword, e.g. {item}
178 return self._converter.formatlist(data, name, fmt, sep) 182 return self._converter.formatlist(data, name, fmt, sep)
179 def context(self, **ctxs): 183 def context(self, **ctxs):
180 '''insert context objects to be used to render template keywords''' 184 '''insert context objects to be used to render template keywords'''
181 pass 185 ctxs = pycompat.byteskwargs(ctxs)
186 assert all(k == 'ctx' for k in ctxs)
187 if self._converter.storecontext:
188 self._item.update(ctxs)
182 def data(self, **data): 189 def data(self, **data):
183 '''insert data into item that's not shown in default output''' 190 '''insert data into item that's not shown in default output'''
184 data = pycompat.byteskwargs(data) 191 data = pycompat.byteskwargs(data)
185 self._item.update(data) 192 self._item.update(data)
186 def write(self, fields, deftext, *fielddata, **opts): 193 def write(self, fields, deftext, *fielddata, **opts):
226 return sorted(data.iteritems()) 233 return sorted(data.iteritems())
227 return data 234 return data
228 235
229 class _plainconverter(object): 236 class _plainconverter(object):
230 '''convert non-primitive data types to text''' 237 '''convert non-primitive data types to text'''
238
239 storecontext = False
240
231 @staticmethod 241 @staticmethod
232 def formatdate(date, fmt): 242 def formatdate(date, fmt):
233 '''stringify date tuple in the given format''' 243 '''stringify date tuple in the given format'''
234 return util.datestr(date, fmt) 244 return util.datestr(date, fmt)
235 @staticmethod 245 @staticmethod
321 baseformatter.end(self) 331 baseformatter.end(self)
322 self._out.write("\n]\n") 332 self._out.write("\n]\n")
323 333
324 class _templateconverter(object): 334 class _templateconverter(object):
325 '''convert non-primitive data types to be processed by templater''' 335 '''convert non-primitive data types to be processed by templater'''
336
337 storecontext = True
338
326 @staticmethod 339 @staticmethod
327 def formatdate(date, fmt): 340 def formatdate(date, fmt):
328 '''return date tuple''' 341 '''return date tuple'''
329 return date 342 return date
330 @staticmethod 343 @staticmethod
353 self._parts = templatepartsmap(spec, self._t, 366 self._parts = templatepartsmap(spec, self._t,
354 ['docheader', 'docfooter', 'separator']) 367 ['docheader', 'docfooter', 'separator'])
355 self._counter = itertools.count() 368 self._counter = itertools.count()
356 self._cache = {} # for templatekw/funcs to store reusable data 369 self._cache = {} # for templatekw/funcs to store reusable data
357 self._renderitem('docheader', {}) 370 self._renderitem('docheader', {})
358
359 def context(self, **ctxs):
360 '''insert context objects to be used to render template keywords'''
361 ctxs = pycompat.byteskwargs(ctxs)
362 assert all(k == 'ctx' for k in ctxs)
363 self._item.update(ctxs)
364 371
365 def _showitem(self): 372 def _showitem(self):
366 item = self._item.copy() 373 item = self._item.copy()
367 item['index'] = index = next(self._counter) 374 item['index'] = index = next(self._counter)
368 if index > 0: 375 if index > 0: