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