Fixed #1919 -- filter truncatewords is inefficient and destroys white space #1175

Closed
wants to merge 1 commit into
from

Projects

None yet

2 participants

@qris

Used @arien's patch and @SmileyChris' comments, integrated with current code, hopefully made even more efficient for long strings by not splitting and rejoining them.

https://code.djangoproject.com/ticket/1919

@qris qris Fixed #1919 -- filter truncatewords destroys white space
Used @arien's patch and @SmileyChris' comments, integrated with current
code, hopefully made even more efficient for long strings by not
splitting and rejoining them.

https://code.djangoproject.com/ticket/1919
d637d5d
@timgraham
Django member

buildbot, test this please.

@timgraham timgraham commented on the diff Aug 1, 2014
django/utils/text.py
"""
- words = self._wrapped.split()
- if len(words) > length:
- words = words[:length]
- return self.add_truncation_text(' '.join(words), truncate)
- return ' '.join(words)
+ if max_words < 1:
+ return u''
@timgraham
timgraham Aug 1, 2014

remove u'' prefix (syntax error on Python 3.2 and unnecessary since this file has from __future__ import unicode_literals).

@timgraham timgraham commented on the diff Aug 4, 2014
django/utils/text.py
"""
Truncates a string after a certain number of words.
- Newlines in the string will be stripped.
+ Whitespace within the string will not be modified before the
+ truncation point.
+ maximum is reached.
@timgraham timgraham commented on the diff Aug 4, 2014
django/utils/text.py
"""
Truncates a string after a certain number of words.
- Newlines in the string will be stripped.
@timgraham
timgraham Aug 4, 2014

newlines being stripped is a documented behavior and needs to remain, I think. Here's a test that passes now and fails after your patch.

self.assertEqual(
    truncatewords('Text with a newline\n in the middle', 10),
    'Text with a newline in the middle')
@timgraham
Django member

Please send a new PR if you can update, thanks.

@timgraham timgraham closed this Aug 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment