Mercurial > hg
comparison mercurial/templater.py @ 8474:bccc90b56d02
templater: clean up the process method, separating code paths
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Sun, 17 May 2009 16:05:50 +0200 |
parents | 0bf0045000b5 |
children | caaf1e779b37 |
comparison
equal
deleted
inserted
replaced
8473:69f51fd9fb48 | 8474:bccc90b56d02 |
---|---|
68 elif hasattr(item, '__iter__'): | 68 elif hasattr(item, '__iter__'): |
69 iters.insert(0, iter(item)) | 69 iters.insert(0, iter(item)) |
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: |
78 yield tmpl | 102 yield tmpl |
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 |
110 class templater(object): | 121 class templater(object): |
111 | 122 |