comparison mercurial/templatefilters.py @ 49494:c96ed4029fda

templates: add filter to reverse list The filter supports only lists because for lists, it’s straightforward to implement. Reversing text doesn’t seem very useful and is hard to implement. Reversing the bytes would break multi-bytes encodings. Reversing the code points would break characters consisting of multiple code points. Reversing graphemes is non-trivial without using a library not included in the standard library.
author Manuel Jacob <me@manueljacob.de>
date Thu, 15 Sep 2022 01:48:38 +0200
parents d44e3c45f0e4
children 046b9cce5850
comparison
equal deleted inserted replaced
49493:4367c46a89ee 49494:c96ed4029fda
388 interpreting it as per RFC 5322. 388 interpreting it as per RFC 5322.
389 """ 389 """
390 return stringutil.person(author) 390 return stringutil.person(author)
391 391
392 392
393 @templatefilter(b'reverse')
394 def reverse(list_):
395 """List. Reverses the order of list items."""
396 if isinstance(list_, list):
397 return templateutil.hybridlist(list_[::-1], name=b'item')
398 raise error.ParseError(_(b'not reversible'))
399
400
393 @templatefilter(b'revescape', intype=bytes) 401 @templatefilter(b'revescape', intype=bytes)
394 def revescape(text): 402 def revescape(text):
395 """Any text. Escapes all "special" characters, except @. 403 """Any text. Escapes all "special" characters, except @.
396 Forward slashes are escaped twice to prevent web servers from prematurely 404 Forward slashes are escaped twice to prevent web servers from prematurely
397 unescaping them. For example, "@foo bar/baz" becomes "@foo%20bar%252Fbaz". 405 unescaping them. For example, "@foo bar/baz" becomes "@foo%20bar%252Fbaz".