Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added linebreak option for indent filter. #2031

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,13 @@ def do_urlize(
return rv


@pass_environment
def do_indent(
s: str, width: t.Union[int, str] = 4, first: bool = False, blank: bool = False
environment: "Environment",
s: str,
width: t.Union[int, str] = 4,
first: bool = False,
blank: 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.
Expand All @@ -832,7 +837,7 @@ def do_indent(
else:
indention = " " * width

newline = "\n"
newline = str(environment.newline_sequence)

if isinstance(s, Markup):
indention = Markup(indention)
Expand Down
11 changes: 6 additions & 5 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ def test_format(self, env):

@staticmethod
def _test_indent_multiline_template(env, markup=False):
text = "\n".join(["", "foo bar", '"baz"', ""])
newline = env.newline_sequence
text = newline.join(["", "foo bar", '"baz"', ""])
if markup:
text = Markup(text)
t = env.from_string("{{ foo|indent(2, false, false) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n'
assert t.render(foo=text) == f'{newline} foo bar{newline} "baz"{newline}'
t = env.from_string("{{ foo|indent(2, false, true) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n '
assert t.render(foo=text) == f'{newline} foo bar{newline} "baz"{newline} '
t = env.from_string("{{ foo|indent(2, true, false) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n'
assert t.render(foo=text) == f' {newline} foo bar{newline} "baz"{newline}'
t = env.from_string("{{ foo|indent(2, true, true) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n '
assert t.render(foo=text) == f' {newline} foo bar{newline} "baz"{newline} '

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