Skip to content
Myr

Fix and improve do_truncate

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Ensure that the requested max length be at least as long as the requested `end` value (e.g. 3 in the case of '...'). Fixes #539 - Add `leeway` parameter so that strings that just barely miss the requested max length cutoff can still be spared from unwanted truncation. Default value is 5. - No longer append a space before appending `end` under any circumstances. Adding whitespace before ellipsis punctuation in English is grammatically incorrect.

authored by jab · committed by jab · parent 390c3ce · 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

2 files+18 added−14 removed

modified:jinja2/filters.py+14 added-10 removed
453 unmodified lines
454 rv = indention + rv455 return rv456457458def do_truncate(s, length=255, killwords=False, end='...'):458def do_truncate(s, length=255, killwords=False, end='...', leeway=5):459 """Return a truncated copy of the string. The length is specified460 with the first parameter which defaults to ``255``. If the second461 parameter is ``true`` the filter will cut the text at length. Otherwise462 it will discard the last word. If the text was in fact463 truncated it will append an ellipsis sign (``"..."``). If you want a464 different ellipsis sign than ``"..."`` you can specify it using the465 third parameter.465 third parameter. Strings that only exceed the length by the tolerance466 margin given in the fourth parameter will not be truncated.467468 .. sourcecode:: jinja469469 {{ "foo bar baz"|truncate(9) }}470 -> "foo ..."471 {{ "foo bar baz"|truncate(9, True) }}470 {{ "foo bar baz qux"|truncate(9) }}471 -> "foo..."472 {{ "foo bar baz qux"|truncate(9, True) }}473 -> "foo ba..."474 {{ "foo bar baz qux"|truncate(11) }}475 -> "foo bar baz qux"476 {{ "foo bar baz qux"|truncate(11, False, '...', 0) }}477 -> "foo bar..."478479 """480 assert length >= len(end), 'expected length >= %s, got %s' % (len(end), length)481 assert leeway >= 0, 'expected leeway >= 0, got %s' % leeway475 if len(s) <= length:482 if len(s) <= length + leeway:483 return s477 elif killwords:484 if killwords:485 return s[:length - len(end)] + end479486 result = s[:length - len(end)].rsplit(' ', 1)[0]481 if len(result) < length:482 result += ' '487 return result + end488489490@environmentfilter
516 unmodified lines
modified:tests/test_filters.py+4 added-4 removed
237 unmodified lines
238 )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', msg243244 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', out251252 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' % out256257 def test_upper(self, env):258 tmpl = env.from_string('{{ "foo"|upper }}')259 assert tmpl.render() == 'FOO'
312 unmodified lines