Mercurial > hg
comparison mercurial/templater.py @ 8361:d8c5a7f25a40
templater: make the templating engine pluggable to some extent
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Tue, 12 May 2009 12:05:19 +0200 |
parents | acc202b71619 |
children | 0bf0045000b5 |
comparison
equal
deleted
inserted
replaced
8360:acc202b71619 | 8361:d8c5a7f25a40 |
---|---|
103 if fl: | 103 if fl: |
104 for f in fl.split("|")[1:]: | 104 for f in fl.split("|")[1:]: |
105 v = self.filters[f](v) | 105 v = self.filters[f](v) |
106 yield v | 106 yield v |
107 | 107 |
108 engines = {'default': engine} | |
109 | |
108 class templater(object): | 110 class templater(object): |
109 | 111 |
110 def __init__(self, mapfile, filters={}, defaults={}, cache={}, | 112 def __init__(self, mapfile, filters={}, defaults={}, cache={}, |
111 minchunk=1024, maxchunk=65536): | 113 minchunk=1024, maxchunk=65536): |
112 '''set up template engine. | 114 '''set up template engine. |
119 self.base = (mapfile and os.path.dirname(mapfile)) or '' | 121 self.base = (mapfile and os.path.dirname(mapfile)) or '' |
120 self.filters = templatefilters.filters.copy() | 122 self.filters = templatefilters.filters.copy() |
121 self.filters.update(filters) | 123 self.filters.update(filters) |
122 self.defaults = defaults | 124 self.defaults = defaults |
123 self.minchunk, self.maxchunk = minchunk, maxchunk | 125 self.minchunk, self.maxchunk = minchunk, maxchunk |
126 self.engines = {} | |
124 | 127 |
125 if not mapfile: | 128 if not mapfile: |
126 return | 129 return |
127 if not os.path.exists(mapfile): | 130 if not os.path.exists(mapfile): |
128 raise util.Abort(_('style not found: %s') % mapfile) | 131 raise util.Abort(_('style not found: %s') % mapfile) |
136 self.cache[key] = parsestring(val) | 139 self.cache[key] = parsestring(val) |
137 except SyntaxError, inst: | 140 except SyntaxError, inst: |
138 raise SyntaxError('%s: %s' % | 141 raise SyntaxError('%s: %s' % |
139 (conf.source('', key), inst.args[0])) | 142 (conf.source('', key), inst.args[0])) |
140 else: | 143 else: |
141 self.map[key] = os.path.join(self.base, val) | 144 val = 'default', val |
145 if ':' in val[1]: | |
146 val = val[1].split(':', 1) | |
147 self.map[key] = val[0], os.path.join(self.base, val[1]) | |
142 | 148 |
143 def __contains__(self, key): | 149 def __contains__(self, key): |
144 return key in self.cache or key in self.map | 150 return key in self.cache or key in self.map |
145 | 151 |
146 def load(self, t): | 152 def load(self, t): |
147 '''Get the template for the given template name. Use a local cache.''' | 153 '''Get the template for the given template name. Use a local cache.''' |
148 if not t in self.cache: | 154 if not t in self.cache: |
149 try: | 155 try: |
150 self.cache[t] = file(self.map[t]).read() | 156 self.cache[t] = open(self.map[t][1]).read() |
151 except IOError, inst: | 157 except IOError, inst: |
152 raise IOError(inst.args[0], _('template file %s: %s') % | 158 raise IOError(inst.args[0], _('template file %s: %s') % |
153 (self.map[t], inst.args[1])) | 159 (self.map[t][1], inst.args[1])) |
154 return self.cache[t] | 160 return self.cache[t] |
155 | 161 |
156 def __call__(self, t, **map): | 162 def __call__(self, t, **map): |
157 proc = engine(self.load, self.filters, self.defaults) | 163 ttype = t in self.map and self.map[t][0] or 'default' |
164 proc = self.engines.get(ttype) | |
165 if proc is None: | |
166 proc = engines[ttype](self.load, self.filters, self.defaults) | |
167 self.engines[ttype] = proc | |
168 | |
158 stream = proc.process(t, map) | 169 stream = proc.process(t, map) |
159 if self.minchunk: | 170 if self.minchunk: |
160 stream = util.increasingchunks(stream, min=self.minchunk, | 171 stream = util.increasingchunks(stream, min=self.minchunk, |
161 max=self.maxchunk) | 172 max=self.maxchunk) |
162 return stream | 173 return stream |