D7432: templates: make {indent("", " ")} be empty
martinvonz (Martin von Zweigbergk)
phabricator at mercurial-scm.org
Mon Nov 18 00:59:37 EST 2019
martinvonz updated this revision to Diff 18208.
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D7432?vs=18165&id=18208
BRANCH
default
CHANGES SINCE LAST ACTION
https://phab.mercurial-scm.org/D7432/new/
REVISION DETAIL
https://phab.mercurial-scm.org/D7432
AFFECTED FILES
mercurial/templatefilters.py
mercurial/templatefuncs.py
relnotes/next
tests/test-template-functions.t
CHANGE DETAILS
diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -1507,16 +1507,16 @@
Test indent with empty first line
$ hg version -T "{indent('', '>> ')}\n"
- >>
+
$ hg version -T "{indent('
> second', '>> ')}\n"
- >>
+
>> second
$ hg version -T "{indent('
> second', '>> ', ' > ')}\n"
- >
+
>> second
Test with non-strings like dates
diff --git a/relnotes/next b/relnotes/next
--- a/relnotes/next
+++ b/relnotes/next
@@ -6,6 +6,9 @@
== Bug Fixes ==
+ * The `indent()` template function was documented to not indent empty lines,
+ but it still indented the first line even if it was empty. It no longer does
+ that.
== Backwards Compatibility Changes ==
diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -310,13 +310,11 @@
text = evalstring(context, mapping, args[0])
indent = evalstring(context, mapping, args[1])
+ firstline = indent
if len(args) == 3:
firstline = evalstring(context, mapping, args[2])
- else:
- firstline = indent
- # the indent function doesn't indent the first line, so we do it here
- return templatefilters.indent(firstline + text, indent)
+ return templatefilters.indent(text, indent, firstline=firstline)
@templatefunc(b'get(dict, key)')
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -299,7 +299,7 @@
return dateutil.datestr(text, b'%Y-%m-%d %H:%M:%S %1%2')
-def indent(text, prefix):
+def indent(text, prefix, firstline=b''):
'''indent each non-empty line of text after first with prefix.'''
lines = text.splitlines()
num_lines = len(lines)
@@ -308,8 +308,8 @@
def indenter():
for i in pycompat.xrange(num_lines):
l = lines[i]
- if i and l.strip():
- yield prefix
+ if l.strip():
+ yield prefix if i else firstline
yield l
if i < num_lines - 1 or endswithnewline:
yield b'\n'
To: martinvonz, #hg-reviewers
Cc: mercurial-devel
More information about the Mercurial-devel
mailing list