From: David Lord Date: Fri, 7 Jul 2017 18:46:19 +0000 (-0700) Subject: shorten arguments X-Git-Tag: 2.10~13^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=baa18e09a0785c9c59b6453d3a1d6d248d04dce5;p=thirdparty%2Fjinja.git shorten arguments add test for single line update changelog --- diff --git a/CHANGES b/CHANGES index aaae3ad8..4240a63d 100644 --- a/CHANGES +++ b/CHANGES @@ -29,6 +29,7 @@ Version 2.10 - Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``, ``gt``, ``ge``. (`#665`_) - ``import`` statement cannot end with a trailing comma. (`#617`_, `#618`_) +- ``indent`` filter will not indent blank lines by default. (`#685`_) .. _#469: https://github.com/pallets/jinja/pull/469 .. _#475: https://github.com/pallets/jinja/pull/475 @@ -36,6 +37,7 @@ Version 2.10 .. _#617: https://github.com/pallets/jinja/pull/617 .. _#618: https://github.com/pallets/jinja/pull/618 .. _#665: https://github.com/pallets/jinja/pull/665 +.. _#685: https://github.com/pallets/jinja/pull/685 Version 2.9.6 ------------- diff --git a/jinja2/filters.py b/jinja2/filters.py index aec4843c..da15a1a7 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -526,32 +526,44 @@ def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False, return rv -def do_indent(s, width=4, indent_first=False, indent_blank_lines=False, indentfirst=None): - """Return a copy of the passed string, each line indented by - 4 spaces. The first line is not indented. If you want to - change the number of spaces or indent the first line too - you can pass additional parameters to the filter: +def do_indent( + s, width=4, first=False, blank=False, indentfirst=None +): + """Return a copy of the string with each line indented by 4 spaces. The + first line and blank lines are not indented by default. - .. sourcecode:: jinja + :param width: Number of spaces to indent by. + :param first: Don't skip indenting the first line. + :param blank: Don't skip indenting empty lines. - {{ mytext|indent(2, true) }} - indent by two spaces and indent the first line too. + .. versionchanged:: 2.10 + Blank lines are not indented by default. + + Rename the ``indentfirst`` argument to ``first``. """ if indentfirst is not None: - warnings.warn('The use of indentfirst is obsolete. ' - 'You should use indent_first instead.', DeprecationWarning) - indent_first = indentfirst - s += '\n' # this quirk is necessary for splitlines method + warnings.warn(DeprecationWarning( + 'The "indentfirst" argument is renamed to "first".' + ), stacklevel=2) + first = indentfirst + + s += u'\n' # this quirk is necessary for splitlines method indention = u' ' * width - if indent_blank_lines: + + if blank: rv = (u'\n' + indention).join(s.splitlines()) else: lines = s.splitlines() rv = lines.pop(0) + if lines: - rv += u'\n' + u'\n'.join(indention + line if line else line for line in lines) - if indent_first: + rv += u'\n' + u'\n'.join( + indention + line if line else line for line in lines + ) + + if first: rv = indention + rv + return rv diff --git a/tests/test_filters.py b/tests/test_filters.py index 233b27fe..2ef3249e 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -137,14 +137,25 @@ class TestFilter(object): def test_indent(self, env): text = '\n'.join(['', 'foo bar', '']) - tmpl = env.from_string('{{ foo|indent(2, false, false) }}') - assert tmpl.render(foo=text) == '\n foo bar\n' - tmpl = env.from_string('{{ foo|indent(1, false, true) }}') - assert tmpl.render(foo=text) == '\n foo bar\n ' - tmpl = env.from_string('{{ foo|indent(1, true, false) }}') - assert tmpl.render(foo=text) == ' \n foo bar\n' - tmpl = env.from_string('{{ foo|indent(1, true, true) }}') - assert tmpl.render(foo=text) == ' \n foo bar\n ' + t = env.from_string('{{ foo|indent(2, false, false) }}') + assert t.render(foo=text) == '\n foo bar\n' + t = env.from_string('{{ foo|indent(2, false, true) }}') + assert t.render(foo=text) == '\n foo bar\n ' + t = env.from_string('{{ foo|indent(2, true, false) }}') + assert t.render(foo=text) == ' \n foo bar\n' + t = env.from_string('{{ foo|indent(2, true, true) }}') + assert t.render(foo=text) == ' \n foo bar\n ' + + t = env.from_string('{{ "jinja"|indent }}') + assert t.render() == 'jinja' + t = env.from_string('{{ "jinja"|indent(first=true) }}') + assert t.render() == ' jinja' + t = env.from_string('{{ "jinja"|indent(blank=true) }}') + assert t.render() == 'jinja' + + def test_indentfirst_deprecated(self, env): + with pytest.warns(DeprecationWarning): + env.from_string('{{ "jinja"|indent(indentfirst=true) }}').render() def test_int(self, env): class IntIsh(object):