##// END OF EJS Templates
templater: simplify cache and remove filter argument in __call__
Matt Mackall -
r3637:e7639888 default
parent child Browse files
Show More
@@ -78,7 +78,7 b' class templater(object):'
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.
@@ -86,20 +86,18 b' class templater(object):'
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:
@@ -128,7 +126,7 b' class templater(object):'
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:]
General Comments 0
You need to be logged in to leave comments. Login now