On Wednesday 27 September 2006 08:09, Gábor Farkas wrote:

> i have the following problem:
>
>
> in a page-template, i have to display the first 100 characters of
> some html content.
>
> the problem is, that if i simply slice the html-content as if it were
> plaintext, then sometimes some tags get cut in the half etc...
>
> so, is there a way to do this slicing without losing html-validness?

What if your 100 characters stops with some tag still open, or multiple 
tags still open -- e.g. in the middle of a cell of a table?  In 
general, you can't solve this.  How should those tags be closed again?  
You could in theory have a naive solution that just closes all opens 
tags, but there's no guarantee this will be appropriate.

The only way I can think to do this is strip the HTML, then slice.  
Also, remember that Django currently uses bytestrings internally 
(default UTF8), so you might want to decode into unicode strings before 
slicing, then re-encode.

Luke

-- 
Hofstadter's Law: It always takes longer than you expect, even when you 
take into account Hofstadter's Law.

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to