mercurial/templater.py
changeset 3637 e7639888bb2f
parent 3636 dfbbb33f3fa3
child 3638 7b064d8bac5e
equal deleted inserted replaced
3636:dfbbb33f3fa3 3637:e7639888bb2f
    76                     self.map[key] = os.path.join(self.base, val)
    76                     self.map[key] = os.path.join(self.base, val)
    77             else:
    77             else:
    78                 raise SyntaxError(_("%s:%s: parse error") % (mapfile, i))
    78                 raise SyntaxError(_("%s:%s: parse error") % (mapfile, i))
    79 
    79 
    80     def __contains__(self, key):
    80     def __contains__(self, key):
    81         return key in self.cache
    81         return key in self.cache or key in self.map
    82 
    82 
    83     def __call__(self, t, **map):
    83     def __call__(self, t, **map):
    84         '''perform expansion.
    84         '''perform expansion.
    85         t is name of map element to expand.
    85         t is name of map element to expand.
    86         map is added elements to use during expansion.'''
    86         map is added elements to use during expansion.'''
    87         m = self.defaults.copy()
    87         m = self.defaults.copy()
    88         m.update(map)
    88         m.update(map)
    89         try:
    89         if not self.cache.has_key(t):
    90             tmpl = self.cache[t]
       
    91         except KeyError:
       
    92             try:
    90             try:
    93                 tmpl = self.cache[t] = file(self.map[t]).read()
    91                 self.cache[t] = file(self.map[t]).read()
    94             except IOError, inst:
    92             except IOError, inst:
    95                 raise IOError(inst.args[0], _('template file %s: %s') %
    93                 raise IOError(inst.args[0], _('template file %s: %s') %
    96                               (self.map[t], inst.args[1]))
    94                               (self.map[t], inst.args[1]))
    97         return self.template(tmpl, self.filters, **m)
    95         return self.template(self.cache[t], **m)
    98 
    96 
    99     template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
    97     template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
   100                              r"(\w+)((%\w+)*)((\|\w+)*)[#}]")
    98                              r"(\w+)((%\w+)*)((\|\w+)*)[#}]")
   101 
    99 
   102     def template(self, tmpl, filters={}, **map):
   100     def template(self, tmpl, **map):
   103         while tmpl:
   101         while tmpl:
   104             m = self.template_re.search(tmpl)
   102             m = self.template_re.search(tmpl)
   105             if m:
   103             if m:
   106                 start, end = m.span(0)
   104                 start, end = m.span(0)
   107                 key = m.group(1)
   105                 key = m.group(1)
   126 
   124 
   127                     v = ""
   125                     v = ""
   128 
   126 
   129                 elif fl:
   127                 elif fl:
   130                     for f in fl.split("|")[1:]:
   128                     for f in fl.split("|")[1:]:
   131                         v = filters[f](v)
   129                         v = self.filters[f](v)
   132 
   130 
   133                 yield v
   131                 yield v
   134                 tmpl = tmpl[end:]
   132                 tmpl = tmpl[end:]
   135             else:
   133             else:
   136                 yield tmpl
   134                 yield tmpl