mercurial/templatekw.py
changeset 10053 5c5c6295533d
child 10054 1a85861f59af
equal deleted inserted replaced
10052:dfc3ed37d58d 10053:5c5c6295533d
       
     1 # templatekw.py - common changeset template keywords
       
     2 #
       
     3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com>
       
     4 #
       
     5 # This software may be used and distributed according to the terms of the
       
     6 # GNU General Public License version 2, incorporated herein by reference.
       
     7 
       
     8 
       
     9 def showlist(templ, name, values, plural=None, **args):
       
    10     '''expand set of values.
       
    11     name is name of key in template map.
       
    12     values is list of strings or dicts.
       
    13     plural is plural of name, if not simply name + 's'.
       
    14 
       
    15     expansion works like this, given name 'foo'.
       
    16 
       
    17     if values is empty, expand 'no_foos'.
       
    18 
       
    19     if 'foo' not in template map, return values as a string,
       
    20     joined by space.
       
    21 
       
    22     expand 'start_foos'.
       
    23 
       
    24     for each value, expand 'foo'. if 'last_foo' in template
       
    25     map, expand it instead of 'foo' for last key.
       
    26 
       
    27     expand 'end_foos'.
       
    28     '''
       
    29     if plural: names = plural
       
    30     else: names = name + 's'
       
    31     if not values:
       
    32         noname = 'no_' + names
       
    33         if noname in templ:
       
    34             yield templ(noname, **args)
       
    35         return
       
    36     if name not in templ:
       
    37         if isinstance(values[0], str):
       
    38             yield ' '.join(values)
       
    39         else:
       
    40             for v in values:
       
    41                 yield dict(v, **args)
       
    42         return
       
    43     startname = 'start_' + names
       
    44     if startname in templ:
       
    45         yield templ(startname, **args)
       
    46     vargs = args.copy()
       
    47     def one(v, tag=name):
       
    48         try:
       
    49             vargs.update(v)
       
    50         except (AttributeError, ValueError):
       
    51             try:
       
    52                 for a, b in v:
       
    53                     vargs[a] = b
       
    54             except ValueError:
       
    55                 vargs[name] = v
       
    56         return templ(tag, **vargs)
       
    57     lastname = 'last_' + name
       
    58     if lastname in templ:
       
    59         last = values.pop()
       
    60     else:
       
    61         last = None
       
    62     for v in values:
       
    63         yield one(v)
       
    64     if last is not None:
       
    65         yield one(last, tag=lastname)
       
    66     endname = 'end_' + names
       
    67     if endname in templ:
       
    68         yield templ(endname, **args)
       
    69