comparison mercurial/templater.py @ 29085:df838803c1d4

templater: add separate() template function A pretty common pattern in templates is adding conditional separators like so: {node}{if(bookmarks, " {bookmarks}")}{if(tags, " {tags}")} With this patch, the above can be simplified to: {separate(" ", node, bookmarks, tags)} The function is similar to the already existing join(), but with a few differences: * separate() skips empty arguments * join() expects a single list argument, while separate() expects each item as a separate argument * separate() takes the separator first in order to allow a variable number of arguments after it
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 03 May 2016 09:49:54 -0700
parents d813132ea361
children 84ef4517de03
comparison
equal deleted inserted replaced
29084:052c9318e464 29085:df838803c1d4
722 text = evalstring(context, mapping, args[0]) 722 text = evalstring(context, mapping, args[0])
723 style = evalstring(context, mapping, args[1]) 723 style = evalstring(context, mapping, args[1])
724 724
725 return minirst.format(text, style=style, keep=['verbose']) 725 return minirst.format(text, style=style, keep=['verbose'])
726 726
727 @templatefunc('separate(sep, args)')
728 def separate(context, mapping, args):
729 """Add a separator between non-empty arguments."""
730 if not args:
731 # i18n: "separate" is a keyword
732 raise error.ParseError(_("separate expects at least one argument"))
733
734 sep = evalstring(context, mapping, args[0])
735 first = True
736 for arg in args[1:]:
737 argstr = evalstring(context, mapping, arg)
738 if not argstr:
739 continue
740 if first:
741 first = False
742 else:
743 yield sep
744 yield argstr
745
727 @templatefunc('shortest(node, minlength=4)') 746 @templatefunc('shortest(node, minlength=4)')
728 def shortest(context, mapping, args): 747 def shortest(context, mapping, args):
729 """Obtain the shortest representation of 748 """Obtain the shortest representation of
730 a node.""" 749 a node."""
731 if not (1 <= len(args) <= 2): 750 if not (1 <= len(args) <= 2):