42 {key|filter1|filter2|...}.''' |
42 {key|filter1|filter2|...}.''' |
43 |
43 |
44 template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
44 template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
45 r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") |
45 r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") |
46 |
46 |
47 def __init__(self, mapfile, filters={}, defaults={}, cache={}): |
47 def __init__(self, mapfile, filters={}, defaults={}, cache={}, |
|
48 minchunk=1024, maxchunk=65536): |
48 '''set up template engine. |
49 '''set up template engine. |
49 mapfile is name of file to read map definitions from. |
50 mapfile is name of file to read map definitions from. |
50 filters is dict of functions. each transforms a value into another. |
51 filters is dict of functions. each transforms a value into another. |
51 defaults is dict of default map definitions.''' |
52 defaults is dict of default map definitions.''' |
52 self.mapfile = mapfile or 'template' |
53 self.mapfile = mapfile or 'template' |
53 self.cache = cache.copy() |
54 self.cache = cache.copy() |
54 self.map = {} |
55 self.map = {} |
55 self.base = (mapfile and os.path.dirname(mapfile)) or '' |
56 self.base = (mapfile and os.path.dirname(mapfile)) or '' |
56 self.filters = filters |
57 self.filters = filters |
57 self.defaults = defaults |
58 self.defaults = defaults |
|
59 self.minchunk, self.maxchunk = minchunk, maxchunk |
58 |
60 |
59 if not mapfile: |
61 if not mapfile: |
60 return |
62 return |
61 if not os.path.exists(mapfile): |
63 if not os.path.exists(mapfile): |
62 raise util.Abort(_('style not found: %s') % mapfile) |
64 raise util.Abort(_('style not found: %s') % mapfile) |
128 for f in fl.split("|")[1:]: |
130 for f in fl.split("|")[1:]: |
129 v = self.filters[f](v) |
131 v = self.filters[f](v) |
130 yield v |
132 yield v |
131 |
133 |
132 def __call__(self, t, **map): |
134 def __call__(self, t, **map): |
|
135 stream = self.expand(t, **map) |
|
136 if self.minchunk: |
|
137 stream = util.increasingchunks(stream, min=self.minchunk, |
|
138 max=self.maxchunk) |
|
139 return stream |
|
140 |
|
141 def expand(self, t, **map): |
133 '''Perform expansion. t is name of map element to expand. map contains |
142 '''Perform expansion. t is name of map element to expand. map contains |
134 added elements for use during expansion. Is a generator.''' |
143 added elements for use during expansion. Is a generator.''' |
135 tmpl = self._template(t) |
144 tmpl = self._template(t) |
136 iters = [self._process(tmpl, map)] |
145 iters = [self._process(tmpl, map)] |
137 while iters: |
146 while iters: |