Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore empty lines in indent filter #681 #685

Merged
merged 2 commits into from Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -29,13 +29,15 @@ 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
.. _#478: https://github.com/pallets/jinja/pull/478
.. _#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
-------------
Expand Down
44 changes: 34 additions & 10 deletions jinja2/filters.py
Expand Up @@ -11,6 +11,7 @@
import re
import math
import random
import warnings

from itertools import groupby, chain
from collections import namedtuple
Expand Down Expand Up @@ -525,21 +526,44 @@ def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False,
return rv


def do_indent(s, width=4, indentfirst=False):
"""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.

.. versionchanged:: 2.10
Blank lines are not indented by default.

{{ mytext|indent(2, true) }}
indent by two spaces and indent the first line too.
Rename the ``indentfirst`` argument to ``first``.
"""
if indentfirst is not None:
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
rv = (u'\n' + indention).join(s.splitlines())
if indentfirst:

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 first:
rv = indention + rv

return rv


Expand Down
25 changes: 20 additions & 5 deletions tests/test_filters.py
Expand Up @@ -136,11 +136,26 @@ def test_format(self, env):
assert out == 'a|b'

def test_indent(self, env):
tmpl = env.from_string('{{ foo|indent(2) }}|{{ foo|indent(2, true) }}')
text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)
out = tmpl.render(foo=text)
assert out == ('foo bar foo bar\n foo bar foo bar| '
'foo bar foo bar\n foo bar foo bar')
text = '\n'.join(['', 'foo bar', ''])
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):
Expand Down