[PATCH 1 of 4 V2] templatefilter: add splitlines function

Ryan McElroy ryanmce at gmail.com
Mon Jun 30 03:16:19 CDT 2014


# HG changeset patch
# User Ryan McElroy <rmcelroy at fb.com>
# Date 1402620341 25200
#      Thu Jun 12 17:45:41 2014 -0700
# Node ID 45d7b890bf0da308b743180101acd80e1d147620
# Parent  cd3c79392056a0d965236e4986a7a4b5d580f3e5
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.

diff -r cd3c79392056 -r 45d7b890bf0d mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Wed Jun 18 20:59:36 2014 -0500
+++ 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"
diff -r cd3c79392056 -r 45d7b890bf0d mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Wed Jun 18 20:59:36 2014 -0500
+++ 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,
diff -r cd3c79392056 -r 45d7b890bf0d tests/test-command-template.t
--- a/tests/test-command-template.t	Wed Jun 18 20:59:36 2014 -0500
+++ 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


More information about the Mercurial-devel mailing list