Mercurial > hg
comparison mercurial/templatefuncs.py @ 37327:ebf139cbd4a1
templater: abstract away from joinfmt
Future patches will add a wrapper for a list of template mappings, which
will implement a custom join() something like {join(mappings % template)}.
The original join() function is broken down as follows:
if hasattr(joinset, 'joinfmt'):
# hybrid.join() where values must be a list or a dict
joinitems((joinfmt(x) for x in values), sep)
elif isinstance(joinset, templateutil.wrapped):
# mappable.join()
show()
else:
# a plain list, a generator, or a byte string; joinfmt was identity()
joinset = templateutil.unwrapvalue(context, joinset)
joinitems(pycompat.maybebytestr(joinset), joiner)
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 17 Mar 2018 22:06:31 +0900 |
parents | 41a5d815d2c1 |
children | f83cb91b052e |
comparison
equal
deleted
inserted
replaced
37326:9cd88dd3bf64 | 37327:ebf139cbd4a1 |
---|---|
314 """Join items in a list with a delimiter.""" | 314 """Join items in a list with a delimiter.""" |
315 if not (1 <= len(args) <= 2): | 315 if not (1 <= len(args) <= 2): |
316 # i18n: "join" is a keyword | 316 # i18n: "join" is a keyword |
317 raise error.ParseError(_("join expects one or two arguments")) | 317 raise error.ParseError(_("join expects one or two arguments")) |
318 | 318 |
319 # TODO: perhaps this should be evalfuncarg(), but it can't because hgweb | |
320 # abuses generator as a keyword that returns a list of dicts. | |
321 joinset = evalrawexp(context, mapping, args[0]) | 319 joinset = evalrawexp(context, mapping, args[0]) |
322 joinset = templateutil.unwrapvalue(context, mapping, joinset) | |
323 joinfmt = getattr(joinset, 'joinfmt', pycompat.identity) | |
324 joiner = " " | 320 joiner = " " |
325 if len(args) > 1: | 321 if len(args) > 1: |
326 joiner = evalstring(context, mapping, args[1]) | 322 joiner = evalstring(context, mapping, args[1]) |
327 itemiter = (joinfmt(x) for x in pycompat.maybebytestr(joinset)) | 323 if isinstance(joinset, templateutil.wrapped): |
328 return templateutil.joinitems(itemiter, joiner) | 324 return joinset.join(context, mapping, joiner) |
325 # TODO: perhaps a generator should be stringify()-ed here, but we can't | |
326 # because hgweb abuses it as a keyword that returns a list of dicts. | |
327 joinset = templateutil.unwrapvalue(context, mapping, joinset) | |
328 return templateutil.joinitems(pycompat.maybebytestr(joinset), joiner) | |
329 | 329 |
330 @templatefunc('label(label, expr)') | 330 @templatefunc('label(label, expr)') |
331 def label(context, mapping, args): | 331 def label(context, mapping, args): |
332 """Apply a label to generated content. Content with | 332 """Apply a label to generated content. Content with |
333 a label applied can result in additional post-processing, such as | 333 a label applied can result in additional post-processing, such as |