]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Allow to reconfigure the default for truncate leeway
authorArmin Ronacher <armin.ronacher@active-4.com>
Tue, 10 Jan 2017 08:21:14 +0000 (09:21 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Tue, 10 Jan 2017 08:21:15 +0000 (09:21 +0100)
Refs #610

CHANGES
docs/api.rst
jinja2/defaults.py
jinja2/filters.py

diff --git a/CHANGES b/CHANGES
index d2803654e87b679692e1371d3a1817f4eda09f24..f872f350f0aa447820c5e636767511526e708033 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -12,6 +12,8 @@ Version 2.9.4
 - Resolved an issue where top-level output silencing after known extend
   blocks could generate invalid code when blocks where contained in if
   statements.  (#651)
+- Made the `truncate.leeway` default configurable to improve compatibility
+  with older templates.
 
 Version 2.9.3
 -------------
index de4d2e442be323f67972cd7749536796dc8d5270..443d0535730ff96493613ccc83050dad04a887e6 100644 (file)
@@ -584,6 +584,12 @@ Example::
     If this is set to False then all strings are stored as unicode
     internally.
 
+``truncate.leeway``:
+    Configures the leeway default for the `truncate` filter.  Leeway as
+    introduced in 2.9 but to restore compatibility with older templates
+    it can be configured to `0` to get the old behavior back.  The default
+    is `5`.
+
 ``urlize.rel``:
     A string that defines the items for the `rel` attribute of generated
     links with the `urlize` filter.  These items are always added.  The
index e29ce565135b37a4b1dba5ececbe39ee596dea3a..35903883cd3ccda829d62fc61424521a7411ec72 100644 (file)
@@ -44,6 +44,7 @@ DEFAULT_POLICIES = {
     'compiler.ascii_str':   True,
     'urlize.rel':           'noopener',
     'urlize.target':        None,
+    'truncate.leeway':      5,
     'json.dumps_function':  None,
     'json.dumps_kwargs':    {'sort_keys': True},
 }
index e13bc623ae9b7fed86cd93b57bec3ca1ed753e5e..f4cd7968886bf8d1b641de7ab4e588ad41e32ade 100644 (file)
@@ -463,7 +463,8 @@ def do_indent(s, width=4, indentfirst=False):
     return rv
 
 
-def do_truncate(s, length=255, killwords=False, end='...', leeway=5):
+@environmentfilter
+def do_truncate(env, s, length=255, killwords=False, end='...', leeway=None):
     """Return a truncated copy of the string. The length is specified
     with the first parameter which defaults to ``255``. If the second
     parameter is ``true`` the filter will cut the text at length. Otherwise
@@ -484,7 +485,11 @@ def do_truncate(s, length=255, killwords=False, end='...', leeway=5):
         {{ "foo bar baz qux"|truncate(11, False, '...', 0) }}
             -> "foo bar..."
 
+    The default leeway on newer Jinja2 versions is 5 and was 0 before but
+    can be reconfigured globally.
     """
+    if leeway is None:
+        leeway = env.policies['truncate.leeway']
     assert length >= len(end), 'expected length >= %s, got %s' % (len(end), length)
     assert leeway >= 0, 'expected leeway >= 0, got %s' % leeway
     if len(s) <= length + leeway: