- 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
.. _#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
-------------
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
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):