Opened 17 years ago

Closed 17 years ago

#4655 closed (duplicate)

truncate_words removes newlines as well as truncating text

Reported by: colin@… Owned by: nobody
Component: Template system Version: dev
Severity: Keywords: truncatewords
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The truncate_words function in django.utils.text and used by the truncatewords filter strips out newline characters from it's output. This is a problem if you then want to chain the output into another filter that utilises newlines (such as linebreaks).

The attached patch fixes this by first breaking the input text into lines, adding each line into the output (including original linebreaks), before truncating the final line to meet the word count.

Attachments (4)

truncate_words.patch (974 bytes ) - added by colin@… 17 years ago.
Patch to truncate_words that preserves newlines in the output.
truncate_words.2.patch (1004 bytes ) - added by colin@… 17 years ago.
Updated with a fix for single line short text.
truncatewords.patch (1.7 KB ) - added by Chris Beaven 17 years ago.
truncatewords.2.patch (1.7 KB ) - added by Chris Beaven 17 years ago.

Download all attachments as: .zip

Change History (7)

by colin@…, 17 years ago

Attachment: truncate_words.patch added

Patch to truncate_words that preserves newlines in the output.

by colin@…, 17 years ago

Attachment: truncate_words.2.patch added

Updated with a fix for single line short text.

by Chris Beaven, 17 years ago

Attachment: truncatewords.patch added

comment:1 by Chris Beaven, 17 years ago

Triage Stage: UnreviewedDesign decision needed

My patch is a bit more comprehensive in preserving all whitespace and includes tests.

The design decision is "do we want to bother?"

Personally, I think we should.

by Chris Beaven, 17 years ago

Attachment: truncatewords.2.patch added

comment:2 by Chris Beaven, 17 years ago

This will fix the issue mentioned in #4560 (which is closed as a quasi-dupe of this one)

comment:3 by James Bennett, 17 years ago

Resolution: duplicate
Status: newclosed

This is a dupe of #1919. The resolution there -- using the linebreak-sensitive filter first, then passing to truncatewords_html -- seems applicable here as well.

Note: See TracTickets for help on using tickets.
Back to Top