Merge pull request #685 from jkfran/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Ignore empty lines in indent filter #681
authored by David Lord · committed by GitHub · parents 6f4d03c, baa18e0 · Browse files · 3 files·+56 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+56 added−15 removed
- tests:+20 added−5 removed
- other:+36 added−10 removed
Provenance
- Commit
e276843- Tree
50cea78- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
6f4d03c- Parent 2
baa18e0
3 files changed+56 added−15 removed
modified:jinja2/filters.py+34 added-10 removed
9 unmodified lines10 """11 import re12 import math13 import random14+import warnings15 16 from itertools import groupby, chain17 from collections import namedtuple18 from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \506 unmodified lines525 rv = Markup(rv)526 return rv527 528 528-def do_indent(s, width=4, indentfirst=False):529- """Return a copy of the passed string, each line indented by530- 4 spaces. The first line is not indented. If you want to531- change the number of spaces or indent the first line too532- you can pass additional parameters to the filter:529+def do_indent(530+ s, width=4, first=False, blank=False, indentfirst=None531+):532+ """Return a copy of the string with each line indented by 4 spaces. The533+ first line and blank lines are not indented by default.534 534- .. sourcecode:: jinja535+ :param width: Number of spaces to indent by.536+ :param first: Don't skip indenting the first line.537+ :param blank: Don't skip indenting empty lines.538 536- {{ mytext|indent(2, true) }}537- indent by two spaces and indent the first line too.539+ .. versionchanged:: 2.10540+ Blank lines are not indented by default.541+542+ Rename the ``indentfirst`` argument to ``first``.543 """544+ if indentfirst is not None:545+ warnings.warn(DeprecationWarning(546+ 'The "indentfirst" argument is renamed to "first".'547+ ), stacklevel=2)548+ first = indentfirst549+550+ s += u'\n' # this quirk is necessary for splitlines method551 indention = u' ' * width540- rv = (u'\n' + indention).join(s.splitlines())541- if indentfirst:552+553+ if blank:554+ rv = (u'\n' + indention).join(s.splitlines())555+ else:556+ lines = s.splitlines()557+ rv = lines.pop(0)558+559+ if lines:560+ rv += u'\n' + u'\n'.join(561+ indention + line if line else line for line in lines562+ )563+564+ if first:565 rv = indention + rv566+567 return rv568 569 570 @environmentfilter613 unmodified linesmodified:tests/test_filters.py+20 added-5 removed
134 unmodified lines135 out = tmpl.render()136 assert out == 'a|b'137 138 def test_indent(self, env):139- tmpl = env.from_string('{{ foo|indent(2) }}|{{ foo|indent(2, true) }}')140- text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)141- out = tmpl.render(foo=text)142- assert out == ('foo bar foo bar\n foo bar foo bar| '143- 'foo bar foo bar\n foo bar foo bar')139+ text = '\n'.join(['', 'foo bar', ''])140+ t = env.from_string('{{ foo|indent(2, false, false) }}')141+ assert t.render(foo=text) == '\n foo bar\n'142+ t = env.from_string('{{ foo|indent(2, false, true) }}')143+ assert t.render(foo=text) == '\n foo bar\n '144+ t = env.from_string('{{ foo|indent(2, true, false) }}')145+ assert t.render(foo=text) == ' \n foo bar\n'146+ t = env.from_string('{{ foo|indent(2, true, true) }}')147+ assert t.render(foo=text) == ' \n foo bar\n '148 149+ t = env.from_string('{{ "jinja"|indent }}')150+ assert t.render() == 'jinja'151+ t = env.from_string('{{ "jinja"|indent(first=true) }}')152+ assert t.render() == ' jinja'153+ t = env.from_string('{{ "jinja"|indent(blank=true) }}')154+ assert t.render() == 'jinja'155+156+ def test_indentfirst_deprecated(self, env):157+ with pytest.warns(DeprecationWarning):158+ env.from_string('{{ "jinja"|indent(indentfirst=true) }}').render()159+160 def test_int(self, env):161 class IntIsh(object):162 def __int__(self):163 return 42488 unmodified linesmodified:CHANGES+2 added
27 unmodified lines28 - Add ``min`` and ``max`` filters. (`#475`_)29 - Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``,30 ``gt``, ``ge``. (`#665`_)31 - ``import`` statement cannot end with a trailing comma. (`#617`_, `#618`_)32+- ``indent`` filter will not indent blank lines by default. (`#685`_)33 34 .. _#469: https://github.com/pallets/jinja/pull/46935 .. _#475: https://github.com/pallets/jinja/pull/47536 .. _#478: https://github.com/pallets/jinja/pull/47837 .. _#617: https://github.com/pallets/jinja/pull/61738 .. _#618: https://github.com/pallets/jinja/pull/61839 .. _#665: https://github.com/pallets/jinja/pull/66540+.. _#685: https://github.com/pallets/jinja/pull/68541 42 Version 2.9.643 -------------44 (bugfix release, released on April 3rd 2017)504 unmodified lines