Mercurial > hg
comparison mercurial/registrar.py @ 37222:54355c243042
templatefilters: allow declaration of input data type
Currently filters take an unwrapped value, which should have no hybrid magic
but actually it does because stringify() relies on it. The 'intype' allows
us to pre-process the magic by .e.g. evalstring() keeping filter functions
as simple as they are now.
stringify() is ported as an example. More follow.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 18 Mar 2018 15:14:58 +0900 |
parents | 521f6c7e1756 |
children | 9bcf096a2da2 |
comparison
equal
deleted
inserted
replaced
37221:307ee8883975 | 37222:54355c243042 |
---|---|
322 | 322 |
323 Usage:: | 323 Usage:: |
324 | 324 |
325 templatefilter = registrar.templatefilter() | 325 templatefilter = registrar.templatefilter() |
326 | 326 |
327 @templatefilter('myfilter') | 327 @templatefilter('myfilter', intype=bytes) |
328 def myfilterfunc(text): | 328 def myfilterfunc(text): |
329 '''Explanation of this template filter .... | 329 '''Explanation of this template filter .... |
330 ''' | 330 ''' |
331 pass | 331 pass |
332 | 332 |
333 The first string argument is used also in online help. | 333 The first string argument is used also in online help. |
334 | 334 |
335 Optional argument 'intype' defines the type of the input argument, | |
336 which should be (bytes, int, or None for any.) | |
337 | |
335 'templatefilter' instance in example above can be used to | 338 'templatefilter' instance in example above can be used to |
336 decorate multiple functions. | 339 decorate multiple functions. |
337 | 340 |
338 Decorated functions are registered automatically at loading | 341 Decorated functions are registered automatically at loading |
339 extension, if an instance named as 'templatefilter' is used for | 342 extension, if an instance named as 'templatefilter' is used for |
340 decorating in extension. | 343 decorating in extension. |
341 | 344 |
342 Otherwise, explicit 'templatefilters.loadkeyword()' is needed. | 345 Otherwise, explicit 'templatefilters.loadkeyword()' is needed. |
343 """ | 346 """ |
347 | |
348 def _extrasetup(self, name, func, intype=None): | |
349 func._intype = intype | |
344 | 350 |
345 class templatefunc(_templateregistrarbase): | 351 class templatefunc(_templateregistrarbase): |
346 """Decorator to register template function | 352 """Decorator to register template function |
347 | 353 |
348 Usage:: | 354 Usage:: |