Opened 8 years ago

Closed 8 years ago

#4655 closed (duplicate)

truncate_words removes newlines as well as truncating text

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

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@… 8 years ago.
Patch to truncate_words that preserves newlines in the output.
truncate_words.2.patch (1004 bytes) - added by colin@… 8 years ago.
Updated with a fix for single line short text.
truncatewords.patch (1.7 KB) - added by SmileyChris 8 years ago.
truncatewords.2.patch (1.7 KB) - added by SmileyChris 8 years ago.

Download all attachments as: .zip

Change History (7)

Changed 8 years ago by colin@…

Patch to truncate_words that preserves newlines in the output.

Changed 8 years ago by colin@…

Updated with a fix for single line short text.

Changed 8 years ago by SmileyChris

comment:1 Changed 8 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design 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.

Changed 8 years ago by SmileyChris

comment:2 Changed 8 years ago by SmileyChris

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

comment:3 Changed 8 years ago by ubernostrum

  • Resolution set to duplicate
  • Status changed from new to closed

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