changeset 21820:cce404b0c918

templatefilter: add splitlines function This is useful for applying changes to each line, and it's especially powerful when used in conjunction with conditionals to modify lines based on content.
author Ryan McElroy <rmcelroy@fb.com>
date Thu, 12 Jun 2014 17:45:41 -0700
parents 816754e75f2f
children 4a445dc5abff
files mercurial/help/templates.txt mercurial/templatefilters.py tests/test-command-template.t
diffstat 3 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help/templates.txt	Thu May 01 19:57:25 2014 +0900
+++ b/mercurial/help/templates.txt	Thu Jun 12 17:45:41 2014 -0700
@@ -84,6 +84,10 @@
 
    $ hg log -r 0 --template "files: {join(files, ', ')}\n"
 
+- Modify each line of a commit description::
+
+   $ hg log --template "{splitlines(desc) % '**** {line}\n'}"
+
 - Format date::
 
    $ hg log -r 0 --template "{date(date, '%Y')}\n"
--- a/mercurial/templatefilters.py	Thu May 01 19:57:25 2014 +0900
+++ b/mercurial/templatefilters.py	Thu Jun 12 17:45:41 2014 -0700
@@ -8,6 +8,7 @@
 import cgi, re, os, time, urllib
 import encoding, node, util
 import hbisect
+import templatekw
 
 def addbreaks(text):
     """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of
@@ -302,6 +303,10 @@
     """:shortdate: Date. Returns a date like "2006-09-18"."""
     return util.shortdate(text)
 
+def splitlines(text):
+    """:splitlines: Any text. Split text into a list of lines."""
+    return templatekw.showlist('line', text.splitlines(), 'lines')
+
 def stringescape(text):
     return text.encode('string_escape')
 
@@ -384,6 +389,7 @@
     "short": short,
     "shortbisect": shortbisect,
     "shortdate": shortdate,
+    "splitlines": splitlines,
     "stringescape": stringescape,
     "stringify": stringify,
     "strip": strip,
--- a/tests/test-command-template.t	Thu May 01 19:57:25 2014 +0900
+++ b/tests/test-command-template.t	Thu Jun 12 17:45:41 2014 -0700
@@ -1859,3 +1859,28 @@
   $ hg log -R a -r 8 --template '{strip(if("1", if("1", "-abc-")), if("1", if("1", "-")))}\n'
   abc
 
+Test splitlines
+
+  $ hg log -Gv -R a --template "{splitlines(desc) % 'foo {line}\n'}"
+  @  foo future
+  |
+  o  foo third
+  |
+  o  foo second
+  
+  o    foo merge
+  |\
+  | o  foo new head
+  | |
+  o |  foo new branch
+  |/
+  o  foo no user, no domain
+  |
+  o  foo no person
+  |
+  o  foo other 1
+  |  foo other 2
+  |  foo
+  |  foo other 3
+  o  foo line 1
+     foo line 2