1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix preserve_trailing_newlines (broken by 7f5080f64a )

Fix for one half of hte bug reported in #12198
This commit is contained in:
Toshio Kuratomi 2015-09-02 08:45:58 -07:00
parent a7231c2203
commit 7ed746ad45

View file

@ -316,6 +316,10 @@ class Templar:
def _do_template(self, data, preserve_trailing_newlines=False, fail_on_undefined=None, overrides=None): def _do_template(self, data, preserve_trailing_newlines=False, fail_on_undefined=None, overrides=None):
# For preserving the number of input newlines in the output (used
# later in this method)
data_newlines = self._count_newlines_from_end(data)
if fail_on_undefined is None: if fail_on_undefined is None:
fail_on_undefined = self._fail_on_undefined_errors fail_on_undefined = self._fail_on_undefined_errors
@ -377,8 +381,15 @@ class Templar:
# characters at the end of the input data, so we use the # characters at the end of the input data, so we use the
# calculate the difference in newlines and append them # calculate the difference in newlines and append them
# to the resulting output for parity # to the resulting output for parity
#
# jinja2 added a keep_trailing_newline option in 2.7 when
# creating an Environment. That would let us make this code
# better (remove a single newline if
# preserve_trailing_newlines is False). Once we can depend on
# that version being present, modify our code to set that when
# initializing self.environment and remove a single trailing
# newline here if preserve_newlines is False.
res_newlines = self._count_newlines_from_end(res) res_newlines = self._count_newlines_from_end(res)
data_newlines = self._count_newlines_from_end(data)
if data_newlines > res_newlines: if data_newlines > res_newlines:
res += '\n' * (data_newlines - res_newlines) res += '\n' * (data_newlines - res_newlines)