Opened 17 years ago
Closed 17 years ago
#4655 closed (duplicate)
truncate_words removes newlines as well as truncating text
Reported by: | 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)
Change History (7)
by , 17 years ago
Attachment: | truncate_words.patch added |
---|
by , 17 years ago
Attachment: | truncate_words.2.patch added |
---|
Updated with a fix for single line short text.
by , 17 years ago
Attachment: | truncatewords.patch added |
---|
comment:1 by , 17 years ago
Triage Stage: | Unreviewed → 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.
by , 17 years ago
Attachment: | truncatewords.2.patch added |
---|
comment:2 by , 17 years ago
This will fix the issue mentioned in #4560 (which is closed as a quasi-dupe of this one)
comment:3 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → 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.
Patch to truncate_words that preserves newlines in the output.