comparison mercurial/templatefilters.py @ 38008: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 fb7140f1d09d
children 08e042f0a67c
comparison
equal deleted inserted replaced
38007:307ee8883975 38008:54355c243042
352 352
353 @templatefilter('stringescape') 353 @templatefilter('stringescape')
354 def stringescape(text): 354 def stringescape(text):
355 return stringutil.escapestr(text) 355 return stringutil.escapestr(text)
356 356
357 @templatefilter('stringify') 357 @templatefilter('stringify', intype=bytes)
358 def stringify(thing): 358 def stringify(thing):
359 """Any type. Turns the value into text by converting values into 359 """Any type. Turns the value into text by converting values into
360 text and concatenating them. 360 text and concatenating them.
361 """ 361 """
362 return templateutil.stringify(thing) 362 return thing # coerced by the intype
363 363
364 @templatefilter('stripdir') 364 @templatefilter('stripdir')
365 def stripdir(text): 365 def stripdir(text):
366 """Treat the text as path and strip a directory level, if 366 """Treat the text as path and strip a directory level, if
367 possible. For example, "foo" and "foo/bar" becomes "foo". 367 possible. For example, "foo" and "foo/bar" becomes "foo".