changeset 19770:0361163efbaf

templater: support using templates with non-standard names from map file Allow to add arbitrarily-named entries to a template map file and then reference them, to make it possible to deduplicate and simplify templates code.
author Alexander Plavin <alexander@plav.in>
date Sun, 22 Sep 2013 13:52:18 +0400
parents 186f54d40fdd
children 3bc675361206
files mercurial/templater.py tests/test-command-template.t
diffstat 2 files changed, 33 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Fri Sep 06 13:30:56 2013 +0400
+++ b/mercurial/templater.py	Sun Sep 22 13:52:18 2013 +0400
@@ -139,7 +139,12 @@
 def runsymbol(context, mapping, key):
     v = mapping.get(key)
     if v is None:
-        v = context._defaults.get(key, '')
+        v = context._defaults.get(key)
+    if v is None:
+        try:
+            v = context.process(key, mapping)
+        except TemplateNotFound:
+            v = ''
     if util.safehasattr(v, '__call__'):
         return v(**mapping)
     if isinstance(v, types.GeneratorType):
@@ -449,6 +454,9 @@
             stylelist.append(split[1])
     return ", ".join(sorted(stylelist))
 
+class TemplateNotFound(util.Abort):
+    pass
+
 class templater(object):
 
     def __init__(self, mapfile, filters={}, defaults={}, cache={},
@@ -500,7 +508,8 @@
             try:
                 self.cache[t] = util.readfile(self.map[t][1])
             except KeyError, inst:
-                raise util.Abort(_('"%s" not in template map') % inst.args[0])
+                raise TemplateNotFound(_('"%s" not in template map') %
+                                       inst.args[0])
             except IOError, inst:
                 raise IOError(inst.args[0], _('template file %s: %s') %
                               (self.map[t][1], inst.args[1]))
--- a/tests/test-command-template.t	Fri Sep 06 13:30:56 2013 +0400
+++ b/tests/test-command-template.t	Sun Sep 22 13:52:18 2013 +0400
@@ -500,6 +500,28 @@
   1
   0
 
+Missing non-standard names give no error (backward compatibility):
+
+  $ echo "changeset = '{c}'" > t
+  $ hg log --style ./t
+
+Defining non-standard name works:
+
+  $ cat <<EOF > t
+  > changeset = '{c}'
+  > c = q
+  > EOF
+  $ hg log --style ./t
+  8
+  7
+  6
+  5
+  4
+  3
+  2
+  1
+  0
+
 ui.style works:
 
   $ echo '[ui]' > .hg/hgrc