formatter: add support for parts map of [templates] section
authorYuya Nishihara <yuya@tcha.org>
Sat, 22 Apr 2017 21:29:00 +0900
changeset 32952 61b60b28c381
parent 32951 050efe9a1644
child 32953 6d79e9109908
formatter: add support for parts map of [templates] section Unlike a mapfile whose template is looked up by spec -> mapfile -> topic, [templates] section is global. We use :sub-section syntax to define parts per template.
mercurial/formatter.py
mercurial/help/templates.txt
tests/test-branches.t
--- a/mercurial/formatter.py	Sat Jun 17 16:32:20 2017 +0900
+++ b/mercurial/formatter.py	Sat Apr 22 21:29:00 2017 +0900
@@ -453,6 +453,11 @@
     partsmap = {spec.ref: spec.ref}  # initial ref must exist in t
     if spec.mapfile:
         partsmap.update((p, p) for p in partnames if p in t)
+    elif spec.ref:
+        for part in partnames:
+            ref = '%s:%s' % (spec.ref, part)  # select config sub-section
+            if ref in t:
+                partsmap[part] = ref
     return partsmap
 
 def loadtemplater(ui, spec, cache=None):
--- a/mercurial/help/templates.txt	Sat Jun 17 16:32:20 2017 +0900
+++ b/mercurial/help/templates.txt	Sat Apr 22 21:29:00 2017 +0900
@@ -117,6 +117,15 @@
 but be aware that the keywords cannot be overridden by templates. For example,
 a template defined as ``templates.rev`` cannot be referenced as ``{rev}``.
 
+A template defined in ``templates`` section may have sub templates which
+are inserted before/after/between items::
+
+  [templates]
+  myjson = ' {dict(rev, node|short)|json}'
+  myjson:docheader = '\{\n'
+  myjson:docfooter = '\n}\n'
+  myjson:separator = ',\n'
+
 Examples
 ========
 
--- a/tests/test-branches.t	Sat Jun 17 16:32:20 2017 +0900
+++ b/tests/test-branches.t	Sat Apr 22 21:29:00 2017 +0900
@@ -544,6 +544,31 @@
    {"branch": "default", "node": "19709c5a4e75"}
   }
 
+  $ cat <<'EOF' >> .hg/hgrc
+  > [templates]
+  > myjson = ' {dict(branch, node|short)|json}'
+  > myjson:docheader = '\{\n'
+  > myjson:docfooter = '\n}\n'
+  > myjson:separator = ',\n'
+  > EOF
+  $ hg branches -T myjson
+  {
+   {"branch": "b", "node": "e23b5505d1ad"},
+   {"branch": "a branch *", "node": "10ff5895aa57"}, (glob)
+   {"branch": "a", "node": "d8cbc61dbaa6"},
+   {"branch": "default", "node": "19709c5a4e75"}
+  }
+
+  $ cat <<'EOF' >> .hg/hgrc
+  > [templates]
+  > :docheader = 'should not be selected as a docheader for literal templates\n'
+  > EOF
+  $ hg branches -T '{branch}\n'
+  b
+  a branch name much longer than the default justification used by branches
+  a
+  default
+
 Tests of revision branch name caching
 
 We rev branch cache is updated automatically. In these tests we use a trick to