##// END OF EJS Templates
templater: keep default resources per template engine (API)...
Yuya Nishihara -
r35484:32c278eb default
parent child Browse files
Show More
@@ -1886,7 +1886,6 b' class changeset_templater(changeset_prin'
1886 '''show a single changeset or file revision'''
1886 '''show a single changeset or file revision'''
1887 props = props.copy()
1887 props = props.copy()
1888 props.update(templatekw.keywords)
1888 props.update(templatekw.keywords)
1889 props['templ'] = self.t
1890 props['ctx'] = ctx
1889 props['ctx'] = ctx
1891 props['repo'] = self.repo
1890 props['repo'] = self.repo
1892 props['ui'] = self.repo.ui
1891 props['ui'] = self.repo.ui
@@ -2663,7 +2662,6 b' def _graphnodeformatter(ui, displayer):'
2663 if isinstance(displayer, changeset_templater):
2662 if isinstance(displayer, changeset_templater):
2664 cache = displayer.cache # reuse cache of slow templates
2663 cache = displayer.cache # reuse cache of slow templates
2665 props = templatekw.keywords.copy()
2664 props = templatekw.keywords.copy()
2666 props['templ'] = templ
2667 props['cache'] = cache
2665 props['cache'] = cache
2668 def formatnode(repo, ctx):
2666 def formatnode(repo, ctx):
2669 props['ctx'] = ctx
2667 props['ctx'] = ctx
@@ -392,7 +392,6 b' class templateformatter(baseformatter):'
392 props.update(item)
392 props.update(item)
393 if 'ctx' in item:
393 if 'ctx' in item:
394 # but template resources must be always available
394 # but template resources must be always available
395 props['templ'] = self._t
396 props['repo'] = props['ctx'].repo()
395 props['repo'] = props['ctx'].repo()
397 props['revcache'] = {}
396 props['revcache'] = {}
398 props = pycompat.strkwargs(props)
397 props = pycompat.strkwargs(props)
@@ -468,18 +467,19 b' def templatepartsmap(spec, t, partnames)'
468 partsmap[part] = ref
467 partsmap[part] = ref
469 return partsmap
468 return partsmap
470
469
471 def loadtemplater(ui, spec, cache=None):
470 def loadtemplater(ui, spec, resources=None, cache=None):
472 """Create a templater from either a literal template or loading from
471 """Create a templater from either a literal template or loading from
473 a map file"""
472 a map file"""
474 assert not (spec.tmpl and spec.mapfile)
473 assert not (spec.tmpl and spec.mapfile)
475 if spec.mapfile:
474 if spec.mapfile:
476 return templater.templater.frommapfile(spec.mapfile, cache=cache)
475 frommapfile = templater.templater.frommapfile
477 return maketemplater(ui, spec.tmpl, cache=cache)
476 return frommapfile(spec.mapfile, resources=resources, cache=cache)
477 return maketemplater(ui, spec.tmpl, resources=resources, cache=cache)
478
478
479 def maketemplater(ui, tmpl, cache=None):
479 def maketemplater(ui, tmpl, resources=None, cache=None):
480 """Create a templater from a string template 'tmpl'"""
480 """Create a templater from a string template 'tmpl'"""
481 aliases = ui.configitems('templatealias')
481 aliases = ui.configitems('templatealias')
482 t = templater.templater(cache=cache, aliases=aliases)
482 t = templater.templater(resources=resources, cache=cache, aliases=aliases)
483 t.cache.update((k, templater.unquotestring(v))
483 t.cache.update((k, templater.unquotestring(v))
484 for k, v in ui.configitems('templates'))
484 for k, v in ui.configitems('templates'))
485 if tmpl:
485 if tmpl:
@@ -393,7 +393,11 b' def runsymbol(context, mapping, key, def'
393 except TemplateNotFound:
393 except TemplateNotFound:
394 v = default
394 v = default
395 if callable(v):
395 if callable(v):
396 return v(**pycompat.strkwargs(mapping))
396 # TODO: templatekw functions will be updated to take (context, mapping)
397 # pair instead of **props
398 props = context._resources.copy()
399 props.update(mapping)
400 return v(**props)
397 return v
401 return v
398
402
399 def buildtemplate(exp, context):
403 def buildtemplate(exp, context):
@@ -657,7 +661,10 b' def files(context, mapping, args):'
657 ctx = context.resource(mapping, 'ctx')
661 ctx = context.resource(mapping, 'ctx')
658 m = ctx.match([raw])
662 m = ctx.match([raw])
659 files = list(ctx.matches(m))
663 files = list(ctx.matches(m))
660 return templatekw.showlist("file", files, mapping)
664 # TODO: pass (context, mapping) pair to keyword function
665 props = context._resources.copy()
666 props.update(mapping)
667 return templatekw.showlist("file", files, props)
661
668
662 @templatefunc('fill(text[, width[, initialident[, hangindent]]])')
669 @templatefunc('fill(text[, width[, initialident[, hangindent]]])')
663 def fill(context, mapping, args):
670 def fill(context, mapping, args):
@@ -878,7 +885,10 b' def latesttag(context, mapping, args):'
878 if len(args) == 1:
885 if len(args) == 1:
879 pattern = evalstring(context, mapping, args[0])
886 pattern = evalstring(context, mapping, args[0])
880
887
881 return templatekw.showlatesttags(pattern, **pycompat.strkwargs(mapping))
888 # TODO: pass (context, mapping) pair to keyword function
889 props = context._resources.copy()
890 props.update(mapping)
891 return templatekw.showlatesttags(pattern, **pycompat.strkwargs(props))
882
892
883 @templatefunc('localdate(date[, tz])')
893 @templatefunc('localdate(date[, tz])')
884 def localdate(context, mapping, args):
894 def localdate(context, mapping, args):
@@ -1062,8 +1072,11 b' def revset(context, mapping, args):'
1062 revs = list(revs)
1072 revs = list(revs)
1063 revsetcache[raw] = revs
1073 revsetcache[raw] = revs
1064
1074
1075 # TODO: pass (context, mapping) pair to keyword function
1076 props = context._resources.copy()
1077 props.update(mapping)
1065 return templatekw.showrevslist("revision", revs,
1078 return templatekw.showrevslist("revision", revs,
1066 **pycompat.strkwargs(mapping))
1079 **pycompat.strkwargs(props))
1067
1080
1068 @templatefunc('rstdoc(text, style)')
1081 @templatefunc('rstdoc(text, style)')
1069 def rstdoc(context, mapping, args):
1082 def rstdoc(context, mapping, args):
@@ -1290,14 +1303,18 b' class engine(object):'
1290 filter uses function to transform value. syntax is
1303 filter uses function to transform value. syntax is
1291 {key|filter1|filter2|...}.'''
1304 {key|filter1|filter2|...}.'''
1292
1305
1293 def __init__(self, loader, filters=None, defaults=None, aliases=()):
1306 def __init__(self, loader, filters=None, defaults=None, resources=None,
1307 aliases=()):
1294 self._loader = loader
1308 self._loader = loader
1295 if filters is None:
1309 if filters is None:
1296 filters = {}
1310 filters = {}
1297 self._filters = filters
1311 self._filters = filters
1298 if defaults is None:
1312 if defaults is None:
1299 defaults = {}
1313 defaults = {}
1314 if resources is None:
1315 resources = {}
1300 self._defaults = defaults
1316 self._defaults = defaults
1317 self._resources = resources
1301 self._aliasmap = _aliasrules.buildmap(aliases)
1318 self._aliasmap = _aliasrules.buildmap(aliases)
1302 self._cache = {} # key: (func, data)
1319 self._cache = {} # key: (func, data)
1303
1320
@@ -1311,7 +1328,12 b' class engine(object):'
1311 def resource(self, mapping, key):
1328 def resource(self, mapping, key):
1312 """Return internal data (e.g. cache) used for keyword/function
1329 """Return internal data (e.g. cache) used for keyword/function
1313 evaluation"""
1330 evaluation"""
1314 return mapping[key]
1331 v = mapping.get(key)
1332 if v is None:
1333 v = self._resources.get(key)
1334 if v is None:
1335 raise KeyError
1336 return v
1315
1337
1316 def _load(self, t):
1338 def _load(self, t):
1317 '''load, parse, and cache a template'''
1339 '''load, parse, and cache a template'''
@@ -1406,17 +1428,21 b' class TemplateNotFound(error.Abort):'
1406
1428
1407 class templater(object):
1429 class templater(object):
1408
1430
1409 def __init__(self, filters=None, defaults=None, cache=None, aliases=(),
1431 def __init__(self, filters=None, defaults=None, resources=None,
1410 minchunk=1024, maxchunk=65536):
1432 cache=None, aliases=(), minchunk=1024, maxchunk=65536):
1411 '''set up template engine.
1433 '''set up template engine.
1412 filters is dict of functions. each transforms a value into another.
1434 filters is dict of functions. each transforms a value into another.
1413 defaults is dict of default map definitions.
1435 defaults is dict of default map definitions.
1436 resources is dict of internal data (e.g. cache), which are inaccessible
1437 from user template.
1414 aliases is list of alias (name, replacement) pairs.
1438 aliases is list of alias (name, replacement) pairs.
1415 '''
1439 '''
1416 if filters is None:
1440 if filters is None:
1417 filters = {}
1441 filters = {}
1418 if defaults is None:
1442 if defaults is None:
1419 defaults = {}
1443 defaults = {}
1444 if resources is None:
1445 resources = {}
1420 if cache is None:
1446 if cache is None:
1421 cache = {}
1447 cache = {}
1422 self.cache = cache.copy()
1448 self.cache = cache.copy()
@@ -1424,15 +1450,17 b' class templater(object):'
1424 self.filters = templatefilters.filters.copy()
1450 self.filters = templatefilters.filters.copy()
1425 self.filters.update(filters)
1451 self.filters.update(filters)
1426 self.defaults = defaults
1452 self.defaults = defaults
1453 self._resources = {'templ': self}
1454 self._resources.update(resources)
1427 self._aliases = aliases
1455 self._aliases = aliases
1428 self.minchunk, self.maxchunk = minchunk, maxchunk
1456 self.minchunk, self.maxchunk = minchunk, maxchunk
1429 self.ecache = {}
1457 self.ecache = {}
1430
1458
1431 @classmethod
1459 @classmethod
1432 def frommapfile(cls, mapfile, filters=None, defaults=None, cache=None,
1460 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None,
1433 minchunk=1024, maxchunk=65536):
1461 cache=None, minchunk=1024, maxchunk=65536):
1434 """Create templater from the specified map file"""
1462 """Create templater from the specified map file"""
1435 t = cls(filters, defaults, cache, [], minchunk, maxchunk)
1463 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk)
1436 cache, tmap, aliases = _readmapfile(mapfile)
1464 cache, tmap, aliases = _readmapfile(mapfile)
1437 t.cache.update(cache)
1465 t.cache.update(cache)
1438 t.map = tmap
1466 t.map = tmap
@@ -1469,7 +1497,7 b' class templater(object):'
1469 except KeyError:
1497 except KeyError:
1470 raise error.Abort(_('invalid template engine: %s') % ttype)
1498 raise error.Abort(_('invalid template engine: %s') % ttype)
1471 self.ecache[ttype] = ecls(self.load, self.filters, self.defaults,
1499 self.ecache[ttype] = ecls(self.load, self.filters, self.defaults,
1472 self._aliases)
1500 self._resources, self._aliases)
1473 proc = self.ecache[ttype]
1501 proc = self.ecache[ttype]
1474
1502
1475 stream = proc.process(t, mapping)
1503 stream = proc.process(t, mapping)
@@ -4,8 +4,9 b''
4 > from mercurial import templater
4 > from mercurial import templater
5 >
5 >
6 > class mytemplater(object):
6 > class mytemplater(object):
7 > def __init__(self, loader, filters, defaults, aliases):
7 > def __init__(self, loader, filters, defaults, resources, aliases):
8 > self.loader = loader
8 > self.loader = loader
9 > self._resources = resources
9 >
10 >
10 > def process(self, t, map):
11 > def process(self, t, map):
11 > tmpl = self.loader(t)
12 > tmpl = self.loader(t)
@@ -13,7 +14,9 b''
13 > if k in ('templ', 'ctx', 'repo', 'revcache', 'cache', 'troubles'):
14 > if k in ('templ', 'ctx', 'repo', 'revcache', 'cache', 'troubles'):
14 > continue
15 > continue
15 > if hasattr(v, '__call__'):
16 > if hasattr(v, '__call__'):
16 > v = v(**map)
17 > props = self._resources.copy()
18 > props.update(map)
19 > v = v(**props)
17 > v = templater.stringify(v)
20 > v = templater.stringify(v)
18 > tmpl = tmpl.replace('{{%s}}' % k, v)
21 > tmpl = tmpl.replace('{{%s}}' % k, v)
19 > yield tmpl
22 > yield tmpl
General Comments 0
You need to be logged in to leave comments. Login now