template: Bugfix newline handling.

If a rendering ended with a zeroed out split, the output would contain an
extraneous trailing newline.  Rearrange the code to print a newline
preceding each content line.
This commit is contained in:
Brett Smith 2017-12-27 11:36:25 -05:00
parent e758db31ad
commit 9f720527f1

View file

@ -165,7 +165,7 @@ class AccountSplitter:
account_s = account.format_map(template_vars) account_s = account.format_map(template_vars)
amount_s = babel.numbers.format_currency(amount, template_vars['currency'], amt_fmt) amount_s = babel.numbers.format_currency(amount, template_vars['currency'], amt_fmt)
sep_len = max(2, self.TARGET_ACCTLINE_LEN - len(account_s) - len(amount_s)) sep_len = max(2, self.TARGET_ACCTLINE_LEN - len(account_s) - len(amount_s))
yield ' {}{}{}\n{}'.format( yield '\n {}{}{}{}'.format(
account_s, ' ' * sep_len, amount_s, account_s, ' ' * sep_len, amount_s,
metadata.format_map(template_vars), metadata.format_map(template_vars),
) )
@ -214,14 +214,12 @@ class Template:
amount_expr = line[match.end():] amount_expr = line[match.end():]
self.splitter.add(account, amount_expr) self.splitter.add(account, amount_expr)
self.format_funcs.append(self.splitter.render_next) self.format_funcs.append(self.splitter.render_next)
if metadata:
self._add_str_func(metadata) self._add_str_func(metadata)
if not metadata[-1].endswith('\n'):
self.format_funcs.append('\n'.format_map) self.format_funcs.append('\n'.format_map)
def _nonblank_lines(self, s): def _nonblank_lines(self, s):
for line in s.splitlines(True): for line in s.splitlines(True):
line = line.lstrip() line = line.strip()
if line: if line:
yield line yield line
@ -234,12 +232,12 @@ class Template:
if self.PAYEE_LINE_RE.match(line1): if self.PAYEE_LINE_RE.match(line1):
yield '\n' + line1 yield '\n' + line1
else: else:
yield '\n{date} {payee}\n' yield '\n{date} {payee}'
yield line1 yield line1
yield from lines yield from lines
def _add_str_func(self, str_seq): def _add_str_func(self, str_seq):
str_flat = ''.join(' ' + s for s in str_seq) str_flat = ''.join('\n ' + s for s in str_seq)
if not str_flat: if not str_flat:
pass pass
elif self.splitter.is_empty(): elif self.splitter.is_empty():