Skip to content

Commit

Permalink
Merge pull request #685 from jkfran/master
Browse files Browse the repository at this point in the history
Ignore empty lines in indent filter #681
  • Loading branch information
davidism committed Jul 7, 2017
2 parents 6f4d03c + baa18e0 commit e276843
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
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

0 comments on commit e276843

Please sign in to comment.