##// END OF EJS Templates
templater: make the templating engine pluggable to some extent
Dirkjan Ochtman -
r8361:d8c5a7f2 default
parent child Browse files
Show More
@@ -0,0 +1,35 b''
1 #!/bin/sh
2
3 cat > engine.py << EOF
4
5 from mercurial import templater
6
7 class mytemplater(object):
8 def __init__(self, loader, filters, defaults):
9 self.loader = loader
10
11 def process(self, t, map):
12 tmpl = self.loader(t)
13 for k, v in map.iteritems():
14 v = templater.stringify(v)
15 tmpl = tmpl.replace('{{%s}}' % k, v)
16 yield tmpl
17
18 templater.engines['my'] = mytemplater
19 EOF
20
21 hg init test
22 echo '[extensions]' > test/.hg/hgrc
23 echo "engine = $PWD/engine.py" >> test/.hg/hgrc
24
25 cd test
26 cat > mymap << EOF
27 changeset = my:changeset.txt
28 EOF
29
30 cat > changeset.txt << EOF
31 {{rev}} {{node}} {{author}}
32 EOF
33
34 hg ci -Ama
35 hg log --style=./mymap
@@ -0,0 +1,3 b''
1 adding changeset.txt
2 adding mymap
3 0 97e5f848f0936960273bbf75be6388cd0350a32b test
@@ -105,6 +105,8 b' class engine(object):'
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={},
@@ -121,6 +123,7 b' class templater(object):'
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
@@ -138,7 +141,10 b' class templater(object):'
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
@@ -147,14 +153,19 b' class templater(object):'
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,
General Comments 0
You need to be logged in to leave comments. Login now