Skip to content

Commit

Permalink
Added linebreak option for indent filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeromin committed Oct 2, 2024
1 parent eb0df04 commit 578c381
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,14 +810,19 @@ def do_urlize(


def do_indent(
s: str, width: t.Union[int, str] = 4, first: bool = False, blank: bool = False
s: str,
width: t.Union[int, str] = 4,
first: bool = False,
blank: bool = False,
CRLF: bool = False,
) -> str:
"""Return a copy of the string with each line indented by 4 spaces. The
first line and blank lines are not indented by default.
:param width: Number of spaces, or a string, to indent by.
:param first: Don't skip indenting the first line.
:param blank: Don't skip indenting empty lines.
:param CRLF: Use CRLF line breaks. The default is LF line breaks.
.. versionchanged:: 3.0
``width`` can be a string.
Expand All @@ -831,8 +836,10 @@ def do_indent(
indention = width
else:
indention = " " * width

newline = "\n"
if CRLF:
newline = "\r\n"
else:
newline = "\n"

if isinstance(s, Markup):
indention = Markup(indention)
Expand Down
16 changes: 12 additions & 4 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,22 @@ def _test_indent_multiline_template(env, markup=False):
text = "\n".join(["", "foo bar", '"baz"', ""])
if markup:
text = Markup(text)
t = env.from_string("{{ foo|indent(2, false, false) }}")
t = env.from_string("{{ foo|indent(2, false, false, false) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n'
t = env.from_string("{{ foo|indent(2, false, true) }}")
t = env.from_string("{{ foo|indent(2, false, false, true) }}")
assert t.render(foo=text) == '\r\n foo bar\r\n "baz"\r\n'
t = env.from_string("{{ foo|indent(2, false, true, false) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n '
t = env.from_string("{{ foo|indent(2, true, false) }}")
t = env.from_string("{{ foo|indent(2, false, true, true) }}")
assert t.render(foo=text) == '\r\n foo bar\r\n "baz"\r\n '
t = env.from_string("{{ foo|indent(2, true, false, false) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n'
t = env.from_string("{{ foo|indent(2, true, true) }}")
t = env.from_string("{{ foo|indent(2, true, false, true) }}")
assert t.render(foo=text) == ' \r\n foo bar\r\n "baz"\r\n'
t = env.from_string("{{ foo|indent(2, true, true, false) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n '
t = env.from_string("{{ foo|indent(2, true, true, true) }}")
assert t.render(foo=text) == ' \r\n foo bar\r\n "baz"\r\n '

def test_indent(self, env):
self._test_indent_multiline_template(env)
Expand Down

0 comments on commit 578c381

Please sign in to comment.