Allow to reconfigure the default for truncate leeway
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Refs #610
authored by Armin Ronacher · committed by Armin Ronacher · parent e73c574 · Browse files · 4 files·+15 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+15 added−1 removed
- other:+15 added−1 removed
Provenance
- Commit
fb47dfa- Tree
0dafbf9- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e73c574
4 files changed+15 added−1 removed
modified:docs/api.rst+6 added
582 unmodified lines583 Now however sometimes the inverse is true (for instance str.format).584 If this is set to False then all strings are stored as unicode585 internally.586 587+``truncate.leeway``:588+ Configures the leeway default for the `truncate` filter. Leeway as589+ introduced in 2.9 but to restore compatibility with older templates590+ it can be configured to `0` to get the old behavior back. The default591+ is `5`.592+593 ``urlize.rel``:594 A string that defines the items for the `rel` attribute of generated595 links with the `urlize` filter. These items are always added. The596 default is `noopener`.327 unmodified linesmodified:jinja2/defaults.py+1 added
42 unmodified lines43 DEFAULT_POLICIES = {44 'compiler.ascii_str': True,45 'urlize.rel': 'noopener',46 'urlize.target': None,47+ 'truncate.leeway': 5,48 'json.dumps_function': None,49 'json.dumps_kwargs': {'sort_keys': True},50 }51 3 unmodified linesmodified:jinja2/filters.py+6 added-1 removed
461 unmodified lines462 rv = indention + rv463 return rv464 465 466-def do_truncate(s, length=255, killwords=False, end='...', leeway=5):466+@environmentfilter467+def do_truncate(env, s, length=255, killwords=False, end='...', leeway=None):468 """Return a truncated copy of the string. The length is specified469 with the first parameter which defaults to ``255``. If the second470 parameter is ``true`` the filter will cut the text at length. Otherwise471 it will discard the last word. If the text was in fact12 unmodified lines484 -> "foo bar baz qux"485 {{ "foo bar baz qux"|truncate(11, False, '...', 0) }}486 -> "foo bar..."487 488+ The default leeway on newer Jinja2 versions is 5 and was 0 before but489+ can be reconfigured globally.490 """491+ if leeway is None:492+ leeway = env.policies['truncate.leeway']493 assert length >= len(end), 'expected length >= %s, got %s' % (len(end), length)494 assert leeway >= 0, 'expected leeway >= 0, got %s' % leeway495 if len(s) <= length + leeway:496 return s570 unmodified linesmodified:CHANGES+2 added
10 unmodified lines11 - Corrected bad code generation and scoping for filtered loops. (#649)12 - Resolved an issue where top-level output silencing after known extend13 blocks could generate invalid code when blocks where contained in if14 statements. (#651)15+- Made the `truncate.leeway` default configurable to improve compatibility16+ with older templates.17 18 Version 2.9.319 -------------20 (bugfix release, released on January 8th 2017)466 unmodified lines