Merge pull request #610 from jab/jab-do_truncate
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix and improve do_truncate
authored by Armin Ronacher · committed by GitHub · parents 5453db1, 9f9606b · Browse files · 2 files·+18 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+18 added−14 removed
- tests:+4 added−4 removed
- other:+14 added−10 removed
Provenance
- Commit
49c382a- Tree
51026ad- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
GitHub <noreply@github.com>- Parent 1
5453db1- Parent 2
9f9606b
2 files changed+18 added−14 removed
modified:jinja2/filters.py+14 added-10 removed
461 unmodified lines462 rv = indention + rv463 return rv464 465 466-def do_truncate(s, length=255, killwords=False, end='...'):466+def do_truncate(s, length=255, killwords=False, end='...', leeway=5):467 """Return a truncated copy of the string. The length is specified468 with the first parameter which defaults to ``255``. If the second469 parameter is ``true`` the filter will cut the text at length. Otherwise470 it will discard the last word. If the text was in fact471 truncated it will append an ellipsis sign (``"..."``). If you want a472 different ellipsis sign than ``"..."`` you can specify it using the473- third parameter.473+ third parameter. Strings that only exceed the length by the tolerance474+ margin given in the fourth parameter will not be truncated.475 476 .. sourcecode:: jinja477 477- {{ "foo bar baz"|truncate(9) }}478- -> "foo ..."479- {{ "foo bar baz"|truncate(9, True) }}478+ {{ "foo bar baz qux"|truncate(9) }}479+ -> "foo..."480+ {{ "foo bar baz qux"|truncate(9, True) }}481 -> "foo ba..."482+ {{ "foo bar baz qux"|truncate(11) }}483+ -> "foo bar baz qux"484+ {{ "foo bar baz qux"|truncate(11, False, '...', 0) }}485+ -> "foo bar..."486 487 """488+ assert length >= len(end), 'expected length >= %s, got %s' % (len(end), length)489+ assert leeway >= 0, 'expected leeway >= 0, got %s' % leeway483- if len(s) <= length:490+ if len(s) <= length + leeway:491 return s485- elif killwords:492+ if killwords:493 return s[:length - len(end)] + end487-494 result = s[:length - len(end)].rsplit(' ', 1)[0]489- if len(result) < length:490- result += ' '495 return result + end496 497 498 @environmentfilter526 unmodified linesmodified:tests/test_filters.py+4 added-4 removed
237 unmodified lines238 )239 out = tmpl.render(data='foobar baz bar' * 1000,240 smalldata='foobar baz bar')241 msg = 'Current output: %s' % out242- assert out == 'foobar baz b>>>|foobar baz >>>|foobar baz bar', msg242+ assert out == 'foobar baz b>>>|foobar baz>>>|foobar baz bar', msg243 244 def test_truncate_very_short(self, env):245 tmpl = env.from_string(246 '{{ "foo bar baz"|truncate(9) }}|'247 '{{ "foo bar baz"|truncate(9, true) }}'248 )249 out = tmpl.render()250- assert out == 'foo ...|foo ba...', out250+ assert out == 'foo bar baz|foo bar baz', out251 252 def test_truncate_end_length(self, env):253- tmpl = env.from_string('{{ "Joel is a slug"|truncate(9, true) }}')253+ tmpl = env.from_string('{{ "Joel is a slug"|truncate(7, true) }}')254 out = tmpl.render()255- assert out == 'Joel i...', 'Current output: %s' % out255+ assert out == 'Joel...', 'Current output: %s' % out256 257 def test_upper(self, env):258 tmpl = env.from_string('{{ "foo"|upper }}')259 assert tmpl.render() == 'FOO'319 unmodified lines