From 806f3b26f80c729dede7f10b463691d1f5aae13e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 7 Aug 2010 14:57:24 +0000 Subject: [PATCH] Fixed #11021 -- Clarified newline stripping behavior in the truncatewords and truncatewords_html filters. Thanks to Ben Spaulding for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13555 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaultfilters.py | 4 ++++ django/utils/text.py | 12 +++++++++--- docs/ref/templates/builtins.txt | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index d8e7e91efe..1e58ff71ca 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -256,6 +256,8 @@ def truncatewords(value, arg): Truncates a string after a certain number of words. Argument: Number of words to truncate after. + + Newlines within the string are removed. """ from django.utils.text import truncate_words try: @@ -271,6 +273,8 @@ def truncatewords_html(value, arg): Truncates HTML after a certain number of words. Argument: Number of words to truncate after. + + Newlines in the HTML are preserved. """ from django.utils.text import truncate_html_words try: diff --git a/django/utils/text.py b/django/utils/text.py index 5d633b7afe..b05460486d 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -39,7 +39,10 @@ wrap = allow_lazy(wrap, unicode) def truncate_words(s, num, end_text='...'): """Truncates a string after a certain number of words. Takes an optional argument of what should be used to notify that the string has been - truncated, defaults to ellipsis (...)""" + truncated, defaulting to ellipsis (...) + + Newlines in the string will be stripped. + """ s = force_unicode(s) length = int(num) words = s.split() @@ -51,10 +54,13 @@ def truncate_words(s, num, end_text='...'): truncate_words = allow_lazy(truncate_words, unicode) def truncate_html_words(s, num, end_text='...'): - """Truncates html to a certain number of words (not counting tags and + """Truncates HTML to a certain number of words (not counting tags and comments). Closes opened tags if they were correctly closed in the given html. Takes an optional argument of what should be used to notify that the - string has been truncated, defaults to ellipsis (...).""" + string has been truncated, defaulting to ellipsis (...). + + Newlines in the HTML are preserved. + """ s = force_unicode(s) length = int(num) if length <= 0: diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 002aa3f416..0cf445cab7 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -1883,6 +1883,8 @@ For example:: If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is ..."``. +Newlines within the string will be removed. + .. templatefilter:: truncatewords_html truncatewords_html @@ -1902,6 +1904,8 @@ For example:: If ``value`` is ``"

Joel is a slug

"``, the output will be ``"

Joel is ...

"``. +Newlines in the HTML content will be preserved. + .. templatefilter:: unordered_list unordered_list