#1919: filter truncatewords wipes newlines from string, so not chainable with
markup filters
----------------------------+-----------------------------------------------
Reporter: derelm | Owner: nobody
Status: reopened | Component: Template system
Version: SVN | Resolution:
Keywords: sprintsept14 | Stage: Accepted
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
----------------------------+-----------------------------------------------
Comment (by arien <[EMAIL PROTECTED]>):
The last patch contains a version of trunacte_words that preserves all
whitespace before the nth "word" (sequence of non-blanks); see the tests
for exact behaviour.
(This version of truncate_words differs from the previous version in that
it doesn't use re.UNICODE for the regex and that it updates the counter by
hand instead of using enumerate. This patch also contains a couple more
tests for trailing whitespace handling.)
This version is somewhat slower than the current version when the string
contains few words or is to be truncated after a large fraction of the
words in the string, but is faster when truncating after a small fraction
of the words in a strings containing many words. YMMV.
I tried to a version using re.split as well (splitting on and capturing
non-whitespace, with max_split=length) but it consistently performed worse
than the attached version IIRC.
One comment about testing the performance of the current version and
alternatives to each other: be sure to compare like with like, i.e., make
sure your benchmarking code looks like this:
{{{
from django.utils.text import truncate_words
from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy
def my_truncate_words(s, num):
length = int(num)
s = force_unicode(s)
# code to return a Unicode string
my_truncate_words = allow_lazy(my_truncate_words, unicode)
# other versions follow same pattern
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/1919#comment:15>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---