import re
import math
import random
+import warnings
from itertools import groupby, chain
from collections import namedtuple
return rv
-def do_indent(s, width=4, indentfirst=False):
+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
{{ mytext|indent(2, true) }}
indent by two spaces and indent the first line too.
"""
+ 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
indention = u' ' * width
- rv = (u'\n' + indention).join(s.splitlines())
- if indentfirst:
+ if indent_blank_lines:
+ 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 = indention + rv
return rv
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', ''])
+ 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 '
def test_int(self, env):
class IntIsh(object):