or ``@contextfunction``. :issue:`842`, :pr:`1248`
- Support ``pgettext`` and ``npgettext`` (message contexts) in i18n
extension. :issue:`441`
+- The ``|indent`` filter's ``width`` argument can be a string to
+ indent by. :pr:`1167`
Version 2.11.3
return rv
-def do_indent(s: str, width: int = 4, first: bool = False, blank: bool = False) -> str:
+def do_indent(
+ 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.
- :param width: Number of spaces to indent by.
+ :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.
+ .. versionchanged:: 3.0
+ ``width`` can be a string.
+
.. versionchanged:: 2.10
Blank lines are not indented by default.
Rename the ``indentfirst`` argument to ``first``.
"""
- indention = " " * width
+ if isinstance(width, str):
+ indention = width
+ else:
+ indention = " " * width
+
newline = "\n"
if isinstance(s, Markup):
"""
self._test_indent_multiline_template(env, markup=True)
+ def test_indent_width_string(self, env):
+ t = env.from_string("{{ 'jinja\nflask'|indent(width='>>> ', first=True) }}")
+ assert t.render() == ">>> jinja\n>>> flask"
+
@pytest.mark.parametrize(
("value", "expect"),
(