##// END OF EJS Templates
templater: clean up the process method, separating code paths
Dirkjan Ochtman -
r8474:bccc90b5 default
parent child Browse files
Show More
@@ -70,8 +70,32 b' class engine(object):'
70 else:
70 else:
71 yield str(item)
71 yield str(item)
72
72
73 def _format(self, key, format, get, map):
74 v = get(key)
75 if not hasattr(v, '__iter__'):
76 raise SyntaxError(_("Error expanding '%s%%%s'") % (key, format))
77 lm = map.copy()
78 for i in v:
79 lm.update(i)
80 yield self.process(format, lm)
81
82 def _filter(self, key, filters, get, map):
83 v = get(key)
84 for f in filters.split('|')[1:]:
85 v = self.filters[f](v)
86 return v
87
73 def _process(self, tmpl, map):
88 def _process(self, tmpl, map):
74 '''Render a template. Returns a generator.'''
89 '''Render a template. Returns a generator.'''
90
91 def get(key):
92 v = map.get(key)
93 if v is None:
94 v = self.defaults.get(key, '')
95 if hasattr(v, '__call__'):
96 v = v(**map)
97 return v
98
75 while tmpl:
99 while tmpl:
76 m = self.template_re.search(tmpl)
100 m = self.template_re.search(tmpl)
77 if not m:
101 if not m:
@@ -79,31 +103,18 b' class engine(object):'
79 break
103 break
80
104
81 start, end = m.span(0)
105 start, end = m.span(0)
82 key, format, fl = m.groups()
106 key, fmt, fl = m.groups()
83
107
84 if start:
108 if start:
85 yield tmpl[:start]
109 yield tmpl[:start]
86 tmpl = tmpl[end:]
110 tmpl = tmpl[end:]
87
111
88 if key in map:
112 if fmt:
89 v = map[key]
113 yield self._format(key, fmt, get, map)
114 elif fl:
115 yield self._filter(key, fl, get, map)
90 else:
116 else:
91 v = self.defaults.get(key, "")
117 yield get(key)
92 if hasattr(v, '__call__'):
93 v = v(**map)
94 if format:
95 if not hasattr(v, '__iter__'):
96 raise SyntaxError(_("Error expanding '%s%%%s'")
97 % (key, format))
98 lm = map.copy()
99 for i in v:
100 lm.update(i)
101 yield self.process(format, lm)
102 else:
103 if fl:
104 for f in fl.split("|")[1:]:
105 v = self.filters[f](v)
106 yield v
107
118
108 engines = {'default': engine}
119 engines = {'default': engine}
109
120
General Comments 0
You need to be logged in to leave comments. Login now