##// END OF EJS Templates
templater: factor out generator of join()-ed items...
Yuya Nishihara -
r37341:41a5d815 default
parent child Browse files
Show More
@@ -324,14 +324,8 b' def join(context, mapping, args):'
324 joiner = " "
324 joiner = " "
325 if len(args) > 1:
325 if len(args) > 1:
326 joiner = evalstring(context, mapping, args[1])
326 joiner = evalstring(context, mapping, args[1])
327
327 itemiter = (joinfmt(x) for x in pycompat.maybebytestr(joinset))
328 first = True
328 return templateutil.joinitems(itemiter, joiner)
329 for x in pycompat.maybebytestr(joinset):
330 if first:
331 first = False
332 else:
333 yield joiner
334 yield joinfmt(x)
335
329
336 @templatefunc('label(label, expr)')
330 @templatefunc('label(label, expr)')
337 def label(context, mapping, args):
331 def label(context, mapping, args):
@@ -75,19 +75,12 b' class hybrid(wrapped):'
75 """
75 """
76
76
77 def __init__(self, gen, values, makemap, joinfmt, keytype=None):
77 def __init__(self, gen, values, makemap, joinfmt, keytype=None):
78 if gen is not None:
79 self._gen = gen # generator or function returning generator
78 self._gen = gen # generator or function returning generator
80 self._values = values
79 self._values = values
81 self._makemap = makemap
80 self._makemap = makemap
82 self.joinfmt = joinfmt
81 self.joinfmt = joinfmt
83 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
82 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
84
83
85 def _gen(self):
86 """Default generator to stringify this as {join(self, ' ')}"""
87 for i, x in enumerate(self._values):
88 if i > 0:
89 yield ' '
90 yield self.joinfmt(x)
91 def itermaps(self, context):
84 def itermaps(self, context):
92 makemap = self._makemap
85 makemap = self._makemap
93 for x in self._values:
86 for x in self._values:
@@ -96,6 +89,8 b' class hybrid(wrapped):'
96 def show(self, context, mapping):
89 def show(self, context, mapping):
97 # TODO: switch gen to (context, mapping) API?
90 # TODO: switch gen to (context, mapping) API?
98 gen = self._gen
91 gen = self._gen
92 if gen is None:
93 return joinitems((self.joinfmt(x) for x in self._values), ' ')
99 if callable(gen):
94 if callable(gen):
100 return gen()
95 return gen()
101 return gen
96 return gen
@@ -556,3 +551,13 b' def getdictitem(dictarg, key):'
556 if val is None:
551 if val is None:
557 return
552 return
558 return wraphybridvalue(dictarg, key, val)
553 return wraphybridvalue(dictarg, key, val)
554
555 def joinitems(itemiter, sep):
556 """Join items with the separator; Returns generator of bytes"""
557 first = True
558 for x in itemiter:
559 if first:
560 first = False
561 else:
562 yield sep
563 yield x
General Comments 0
You need to be logged in to leave comments. Login now