##// END OF EJS Templates
templater: separate function to create templater from map file (API)...
Yuya Nishihara -
r28954:f97a0bcf default
parent child Browse files
Show More
@@ -1478,6 +1478,7 b' class changeset_templater(changeset_prin'
1478 def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered):
1478 def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered):
1479 changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
1479 changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered)
1480 formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12])
1480 formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12])
1481 filters = {'formatnode': formatnode}
1481 defaulttempl = {
1482 defaulttempl = {
1482 'parent': '{rev}:{node|formatnode} ',
1483 'parent': '{rev}:{node|formatnode} ',
1483 'manifest': '{rev}:{node|formatnode}',
1484 'manifest': '{rev}:{node|formatnode}',
@@ -1486,10 +1487,14 b' class changeset_templater(changeset_prin'
1486 }
1487 }
1487 # filecopy is preserved for compatibility reasons
1488 # filecopy is preserved for compatibility reasons
1488 defaulttempl['filecopy'] = defaulttempl['file_copy']
1489 defaulttempl['filecopy'] = defaulttempl['file_copy']
1489 self.t = templater.templater(mapfile, {'formatnode': formatnode},
1490 assert not (tmpl and mapfile)
1490 cache=defaulttempl)
1491 if mapfile:
1491 if tmpl:
1492 self.t = templater.templater.frommapfile(mapfile, filters=filters,
1492 self.t.cache['changeset'] = tmpl
1493 cache=defaulttempl)
1494 else:
1495 self.t = templater.templater(filters=filters, cache=defaulttempl)
1496 if tmpl:
1497 self.t.cache['changeset'] = tmpl
1493
1498
1494 self.cache = {}
1499 self.cache = {}
1495
1500
@@ -2770,7 +2770,7 b' def debuginstall(ui, **opts):'
2770 # template found, check if it is working
2770 # template found, check if it is working
2771 err = None
2771 err = None
2772 try:
2772 try:
2773 templater.templater(m)
2773 templater.templater.frommapfile(m)
2774 except Exception as inst:
2774 except Exception as inst:
2775 err = inst
2775 err = inst
2776 p = None
2776 p = None
@@ -3681,7 +3681,7 b' def debugtemplate(ui, repo, tmpl, **opts'
3681 mapfile = None
3681 mapfile = None
3682 if revs is None:
3682 if revs is None:
3683 k = 'debugtemplate'
3683 k = 'debugtemplate'
3684 t = templater.templater(mapfile)
3684 t = templater.templater()
3685 t.cache[k] = tmpl
3685 t.cache[k] = tmpl
3686 ui.write(templater.stringify(t(k, **props)))
3686 ui.write(templater.stringify(t(k, **props)))
3687 else:
3687 else:
@@ -526,7 +526,7 b' def _formatlabels(repo, fcd, fco, fca, l'
526
526
527 ui = repo.ui
527 ui = repo.ui
528 template = ui.config('ui', 'mergemarkertemplate', _defaultconflictmarker)
528 template = ui.config('ui', 'mergemarkertemplate', _defaultconflictmarker)
529 tmpl = templater.templater(None, cache={'conflictmarker': template})
529 tmpl = templater.templater(cache={'conflictmarker': template})
530
530
531 pad = max(len(l) for l in labels)
531 pad = max(len(l) for l in labels)
532
532
@@ -190,7 +190,10 b' def lookuptemplate(ui, topic, tmpl):'
190
190
191 def gettemplater(ui, topic, spec):
191 def gettemplater(ui, topic, spec):
192 tmpl, mapfile = lookuptemplate(ui, topic, spec)
192 tmpl, mapfile = lookuptemplate(ui, topic, spec)
193 t = templater.templater(mapfile, {})
193 assert not (tmpl and mapfile)
194 if mapfile:
195 return templater.templater.frommapfile(mapfile)
196 t = templater.templater()
194 if tmpl:
197 if tmpl:
195 t.cache[topic] = tmpl
198 t.cache[topic] = tmpl
196 return t
199 return t
@@ -188,20 +188,22 b' class requestcontext(object):'
188
188
189 # create the templater
189 # create the templater
190
190
191 tmpl = templater.templater(mapfile,
191 defaults = {
192 filters={'websub': websubfilter},
192 'url': req.url,
193 defaults={'url': req.url,
193 'logourl': logourl,
194 'logourl': logourl,
194 'logoimg': logoimg,
195 'logoimg': logoimg,
195 'staticurl': staticurl,
196 'staticurl': staticurl,
196 'urlbase': urlbase,
197 'urlbase': urlbase,
197 'repo': self.reponame,
198 'repo': self.reponame,
198 'encoding': encoding.encoding,
199 'encoding': encoding.encoding,
199 'motd': motd,
200 'motd': motd,
200 'sessionvars': sessionvars,
201 'sessionvars': sessionvars,
201 'pathdef': makebreadcrumb(req.url),
202 'pathdef': makebreadcrumb(req.url),
202 'style': style,
203 'style': style,
203 }
204 })
204 tmpl = templater.templater.frommapfile(mapfile,
205 filters={'websub': websubfilter},
206 defaults=defaults)
205 return tmpl
207 return tmpl
206
208
207
209
@@ -491,16 +491,17 b' class hgwebdir(object):'
491 if not staticurl.endswith('/'):
491 if not staticurl.endswith('/'):
492 staticurl += '/'
492 staticurl += '/'
493
493
494 tmpl = templater.templater(mapfile,
494 defaults = {
495 defaults={"encoding": encoding.encoding,
495 "encoding": encoding.encoding,
496 "motd": motd,
496 "motd": motd,
497 "url": url,
497 "url": url,
498 "logourl": logourl,
498 "logourl": logourl,
499 "logoimg": logoimg,
499 "logoimg": logoimg,
500 "staticurl": staticurl,
500 "staticurl": staticurl,
501 "sessionvars": sessionvars,
501 "sessionvars": sessionvars,
502 "style": style,
502 "style": style,
503 })
503 }
504 tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
504 return tmpl
505 return tmpl
505
506
506 def updatereqenv(self, env):
507 def updatereqenv(self, env):
@@ -1016,10 +1016,9 b' class TemplateNotFound(error.Abort):'
1016
1016
1017 class templater(object):
1017 class templater(object):
1018
1018
1019 def __init__(self, mapfile, filters=None, defaults=None, cache=None,
1019 def __init__(self, filters=None, defaults=None, cache=None,
1020 minchunk=1024, maxchunk=65536):
1020 minchunk=1024, maxchunk=65536):
1021 '''set up template engine.
1021 '''set up template engine.
1022 mapfile is name of file to read map definitions from.
1023 filters is dict of functions. each transforms a value into another.
1022 filters is dict of functions. each transforms a value into another.
1024 defaults is dict of default map definitions.'''
1023 defaults is dict of default map definitions.'''
1025 if filters is None:
1024 if filters is None:
@@ -1036,11 +1035,15 b' class templater(object):'
1036 self.minchunk, self.maxchunk = minchunk, maxchunk
1035 self.minchunk, self.maxchunk = minchunk, maxchunk
1037 self.ecache = {}
1036 self.ecache = {}
1038
1037
1039 if not mapfile:
1038 @classmethod
1040 return
1039 def frommapfile(cls, mapfile, filters=None, defaults=None, cache=None,
1040 minchunk=1024, maxchunk=65536):
1041 """Create templater from the specified map file"""
1042 t = cls(filters, defaults, cache, minchunk, maxchunk)
1041 cache, tmap = _readmapfile(mapfile)
1043 cache, tmap = _readmapfile(mapfile)
1042 self.cache.update(cache)
1044 t.cache.update(cache)
1043 self.map = tmap
1045 t.map = tmap
1046 return t
1044
1047
1045 def __contains__(self, key):
1048 def __contains__(self, key):
1046 return key in self.cache or key in self.map
1049 return key in self.cache or key in self.map
General Comments 0
You need to be logged in to leave comments. Login now