mercurial/templater.py
changeset 38353 48289eafb37d
parent 38352 e9cb13c54d63
child 38354 e637dc0b3b1f
equal deleted inserted replaced
38352:e9cb13c54d63 38353:48289eafb37d
   716         if extramapping:
   716         if extramapping:
   717             extramapping.update(mapping)
   717             extramapping.update(mapping)
   718             mapping = extramapping
   718             mapping = extramapping
   719         return templateutil.flatten(self, mapping, func(self, mapping, data))
   719         return templateutil.flatten(self, mapping, func(self, mapping, data))
   720 
   720 
   721 engines = {'default': engine}
       
   722 
       
   723 def stylelist():
   721 def stylelist():
   724     paths = templatepaths()
   722     paths = templatepaths()
   725     if not paths:
   723     if not paths:
   726         return _('no templates found, try `hg debuginstall` for more info')
   724         return _('no templates found, try `hg debuginstall` for more info')
   727     dirlist = os.listdir(paths[0])
   725     dirlist = os.listdir(paths[0])
   775             if val[0] != val[-1]:
   773             if val[0] != val[-1]:
   776                 raise error.ParseError(_('unmatched quotes'),
   774                 raise error.ParseError(_('unmatched quotes'),
   777                                        conf.source('templates', key))
   775                                        conf.source('templates', key))
   778             cache[key] = unquotestring(val)
   776             cache[key] = unquotestring(val)
   779         elif key != '__base__':
   777         elif key != '__base__':
   780             val = 'default', val
   778             tmap[key] = os.path.join(base, val)
   781             if ':' in val[1]:
       
   782                 val = val[1].split(':', 1)
       
   783             tmap[key] = val[0], os.path.join(base, val[1])
       
   784     aliases.extend(conf['templatealias'].items())
   779     aliases.extend(conf['templatealias'].items())
   785     return cache, tmap, aliases
   780     return cache, tmap, aliases
   786 
   781 
   787 class templater(object):
   782 class templater(object):
   788 
   783 
   813         self._filters.update(filters)
   808         self._filters.update(filters)
   814         self.defaults = defaults
   809         self.defaults = defaults
   815         self._resources = resources
   810         self._resources = resources
   816         self._aliases = aliases
   811         self._aliases = aliases
   817         self._minchunk, self._maxchunk = minchunk, maxchunk
   812         self._minchunk, self._maxchunk = minchunk, maxchunk
   818         self._ecache = {}
       
   819 
   813 
   820     @classmethod
   814     @classmethod
   821     def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None,
   815     def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None,
   822                     cache=None, minchunk=1024, maxchunk=65536):
   816                     cache=None, minchunk=1024, maxchunk=65536):
   823         """Create templater from the specified map file"""
   817         """Create templater from the specified map file"""
   833 
   827 
   834     def load(self, t):
   828     def load(self, t):
   835         '''Get the template for the given template name. Use a local cache.'''
   829         '''Get the template for the given template name. Use a local cache.'''
   836         if t not in self.cache:
   830         if t not in self.cache:
   837             try:
   831             try:
   838                 self.cache[t] = util.readfile(self._map[t][1])
   832                 self.cache[t] = util.readfile(self._map[t])
   839             except KeyError as inst:
   833             except KeyError as inst:
   840                 raise templateutil.TemplateNotFound(
   834                 raise templateutil.TemplateNotFound(
   841                     _('"%s" not in template map') % inst.args[0])
   835                     _('"%s" not in template map') % inst.args[0])
   842             except IOError as inst:
   836             except IOError as inst:
   843                 reason = (_('template file %s: %s')
   837                 reason = (_('template file %s: %s')
   844                           % (self._map[t][1],
   838                           % (self._map[t],
   845                              stringutil.forcebytestr(inst.args[1])))
   839                              stringutil.forcebytestr(inst.args[1])))
   846                 raise IOError(inst.args[0], encoding.strfromlocal(reason))
   840                 raise IOError(inst.args[0], encoding.strfromlocal(reason))
   847         return self.cache[t]
   841         return self.cache[t]
   848 
   842 
   849     def renderdefault(self, mapping):
   843     def renderdefault(self, mapping):
   855         return b''.join(self.generate(t, mapping))
   849         return b''.join(self.generate(t, mapping))
   856 
   850 
   857     def generate(self, t, mapping):
   851     def generate(self, t, mapping):
   858         """Return a generator that renders the specified named template and
   852         """Return a generator that renders the specified named template and
   859         yields chunks"""
   853         yields chunks"""
   860         ttype = t in self._map and self._map[t][0] or 'default'
   854         proc = engine(self.load, self._filters, self.defaults, self._resources,
   861         if ttype not in self._ecache:
   855                       self._aliases)
   862             try:
       
   863                 ecls = engines[ttype]
       
   864             except KeyError:
       
   865                 raise error.Abort(_('invalid template engine: %s') % ttype)
       
   866             self._ecache[ttype] = ecls(self.load, self._filters, self.defaults,
       
   867                                        self._resources, self._aliases)
       
   868         proc = self._ecache[ttype]
       
   869 
       
   870         stream = proc.process(t, mapping)
   856         stream = proc.process(t, mapping)
   871         if self._minchunk:
   857         if self._minchunk:
   872             stream = util.increasingchunks(stream, min=self._minchunk,
   858             stream = util.increasingchunks(stream, min=self._minchunk,
   873                                            max=self._maxchunk)
   859                                            max=self._maxchunk)
   874         return stream
   860         return stream