librelist archives

« back to archive

Issue with truncate filter

Issue with truncate filter

From:
Helielson Santos
Date:
2012-07-11 @ 19:20
Hi everybody.

I'm using the truncate filter in a autocomplete search.
The result of the autocomplete has a highlight, something like "<b>query
matched</b> something".
The problem with this is that the do_truncate function of the jinja2 uses
the length of string to cut, included the tags:

def do_truncate(s, length=255, killwords=False, end='...'):
if len(s) <= length:

        return s


So, I need something like this: to use the length of tag without count the
tags (striptags) and return it truncated with tags.



Thanks in advance.

*Helielson Santos*

Re: Issue with truncate filter

From:
Helielson Santos
Date:
2012-07-13 @ 12:55
I still don't know how to solve my problem. Any help
would be appreciated. Thanks all!


*Helielson Santos*

Re: [flask] Re: Issue with truncate filter

From:
Sean Lynch
Date:
2012-07-13 @ 13:53
It sounds like you'll need to write your own filter that uses a regular
expression to replace the content between tags.  I'm not sure if you'll
always have the same markup/tags (<b></b>), or if it will vary, and if
you'll have more than 1 tag, or just the <b> to show the highlighted word.
 Lastly, and the tricky part, is if what you're truncating is always inside
the tag, outside the tag, or both (tougher situation)


import re

from jinja import filters



@app.template_filter()
>
@evalcontextfilter

def custom_truncate(eval_ctx, s, lengh=255, killwords=False, end='...'):
>     matches = re.match(r'\<.*\>(.*)\</.*\>', s)
>

   # TODO: truncate inside matches, outside matches, or both, but need to
> handle truncate across the match/unmatch boundies



    # TODO: perform a re.sub to replace matches with truncated values, etc



    if eval_ctx.autoescape:
>         result = Markup(result)
>     return result


Sorry it's not more complete, but this should get you going in the right
direction.


On Fri, Jul 13, 2012 at 8:55 AM, Helielson Santos <
helielson@jusbrasil.com.br> wrote:

> I still don't know how to solve my problem. Any help
> would be appreciated. Thanks all!
>
>
> *Helielson Santos*
>

Re: [flask] Re: Issue with truncate filter

From:
Helielson Santos
Date:
2012-07-13 @ 14:26
@Simon Sapin, I can't add the HTML after truncating.
I'll take a look at ElementTree.

@Sean Lynch, I can try doing it, but it should be a bit slow.
Anyway, I'll try.

Thanks all.


*Helielson Santos*

Re: [flask] Issue with truncate filter

From:
Simon Sapin
Date:
2012-07-13 @ 13:38
Le 11/07/2012 21:20, Helielson Santos a écrit :
> I'm using the truncate filter in a autocomplete search.
> The result of the autocomplete has a highlight, something like "<b>query
> matched</b> something".
> The problem with this is that the do_truncate function of the jinja2uses
> the length of string to cut, included the tags:
>
> defdo_truncate(s,length=255,killwords=False,end='...'):
> iflen(s)<=length:
>
> return s
>
>
> So, I need something like this: to use the length of tag without count
> the tags (striptags) and return it truncated with tags.
>

Hi,

If you can, add the HTML (highlighting the matched search result) after 
truncating.

Otherwise, if you want to do this correctly, you will need to parse the 
HTML, work on the tree, and serialize it back. Have a look at ElementTree:

http://docs.python.org/library/xml.etree.elementtree.html

Regards,
-- 
Simon Sapin