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.
This commit is contained in:
Ryan McElroy 2014-06-12 17:45:41 -07:00
parent 35e80dcc92
commit 9f5399cbcd
3 changed files with 35 additions and 0 deletions

View File

@ -84,6 +84,10 @@ Some sample command line templates:
$ 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"

View File

@ -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 @@ def shortdate(text):
""":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 @@ filters = {
"short": short,
"shortbisect": shortbisect,
"shortdate": shortdate,
"splitlines": splitlines,
"stringescape": stringescape,
"stringify": stringify,
"strip": strip,

View File

@ -1859,3 +1859,28 @@ Test stringify on sub expressions
$ 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