]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
shorten arguments 685/head
authorDavid Lord <davidism@gmail.com>
Fri, 7 Jul 2017 18:46:19 +0000 (11:46 -0700)
committerDavid Lord <davidism@gmail.com>
Fri, 7 Jul 2017 18:46:19 +0000 (11:46 -0700)
add test for single line
update changelog

CHANGES
jinja2/filters.py
tests/test_filters.py

diff --git a/CHANGES b/CHANGES
index aaae3ad85a624f882cd8c7e2eada2bbf147c0c45..4240a63dc1eab9ef4334ec3f5aa5383e39773425 100644 (file)
--- 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
 -------------
index aec4843c31d7884654fdccf0b93ea028a3e99f1e..da15a1a759ac8803efb0b3b6cd6f2b01355dab22 100644 (file)
@@ -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
 
 
index 233b27febc78475400d8f72e6328c7322b2d1e56..2ef3249ea67f8df2ea9c7a332aa781872aaee4d7 100644 (file)
@@ -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):