Skip to content
Myr

Ignore empty lines in indent filter

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Francisco Jiménez Cabrera · committed by David Lord · parent 6f4d03c · Browse files · 2 files+24 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−8 removed

  • tests:+9 added−5 removed
  • other:+15 added−3 removed

2 files+24 added−8 removed

modified:jinja2/filters.py+15 added-3 removed
9 unmodified lines
10"""11import re12import math13import random14import warnings1516from itertools import groupby, chain17from collections import namedtuple18from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \
506 unmodified lines
525 rv = Markup(rv)526 return rv527528528def do_indent(s, width=4, indentfirst=False):529def do_indent(s, width=4, indent_first=False, indent_blank_lines=False, indentfirst=None):530 """Return a copy of the passed string, each line indented by531 4 spaces. The first line is not indented. If you want to532 change the number of spaces or indent the first line too533 you can pass additional parameters to the filter:
2 unmodified lines
536537 {{ mytext|indent(2, true) }}538 indent by two spaces and indent the first line too.539 """540 if indentfirst is not None:541 warnings.warn('The use of indentfirst is obsolete. '542 'You should use indent_first instead.', DeprecationWarning)543 indent_first = indentfirst544 s += '\n' # this quirk is necessary for splitlines method545 indention = u' ' * width546 if indent_blank_lines:540 rv = (u'\n' + indention).join(s.splitlines())541 if indentfirst:547 rv = (u'\n' + indention).join(s.splitlines())548 else:549 lines = s.splitlines()550 rv = lines.pop(0)551 if lines:552 rv += u'\n' + u'\n'.join(indention + line if line else line for line in lines)553 if indent_first:554 rv = indention + rv555 return rv556557
614 unmodified lines
modified:tests/test_filters.py+9 added-5 removed
134 unmodified lines
135 out = tmpl.render()136 assert out == 'a|b'137138 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 tmpl = env.from_string('{{ foo|indent(2, false, false) }}')141 assert tmpl.render(foo=text) == '\n foo bar\n'142 tmpl = env.from_string('{{ foo|indent(1, false, true) }}')143 assert tmpl.render(foo=text) == '\n foo bar\n '144 tmpl = env.from_string('{{ foo|indent(1, true, false) }}')145 assert tmpl.render(foo=text) == ' \n foo bar\n'146 tmpl = env.from_string('{{ foo|indent(1, true, true) }}')147 assert tmpl.render(foo=text) == ' \n foo bar\n '148149 def test_int(self, env):150 class IntIsh(object):151 def __int__(self):
489 unmodified lines