comparison mercurial/templatefilters.py @ 43695:fa246ada356b

templates: make {indent("", " ")} be empty indent() is documented to indent all non-empty lines, but it made an exception for the first line, which always got indented. I also made indent() not indent the first line even if an indent override was given for the first line. I think that is what one would usually want. Differential Revision: https://phab.mercurial-scm.org/D7432
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 15 Nov 2019 10:16:27 -0800
parents 91c746a77fa3
children e3e44e6e7245
comparison
equal deleted inserted replaced
43694:0fd9e7a1cf36 43695:fa246ada356b
297 filter. 297 filter.
298 """ 298 """
299 return dateutil.datestr(text, b'%Y-%m-%d %H:%M:%S %1%2') 299 return dateutil.datestr(text, b'%Y-%m-%d %H:%M:%S %1%2')
300 300
301 301
302 def indent(text, prefix): 302 def indent(text, prefix, firstline=b''):
303 '''indent each non-empty line of text after first with prefix.''' 303 '''indent each non-empty line of text after first with prefix.'''
304 lines = text.splitlines() 304 lines = text.splitlines()
305 num_lines = len(lines) 305 num_lines = len(lines)
306 endswithnewline = text[-1:] == b'\n' 306 endswithnewline = text[-1:] == b'\n'
307 307
308 def indenter(): 308 def indenter():
309 for i in pycompat.xrange(num_lines): 309 for i in pycompat.xrange(num_lines):
310 l = lines[i] 310 l = lines[i]
311 if i and l.strip(): 311 if l.strip():
312 yield prefix 312 yield prefix if i else firstline
313 yield l 313 yield l
314 if i < num_lines - 1 or endswithnewline: 314 if i < num_lines - 1 or endswithnewline:
315 yield b'\n' 315 yield b'\n'
316 316
317 return b"".join(indenter()) 317 return b"".join(indenter())