[Zope-CMF] Render question/Search term highlight

Florent Guillaume fg@nuxeo.com
29 Nov 2001 13:37:58 GMT


If it's a Document object, you can use the CookedBody() method.


Florent

R. Herold <ralf.herold@charite.de> wrote:
> Hello, during preparation of a forthcoming Zope-CMF-information portal, I
> wondered how user-defined search terms could be highlighted in individual
> web pages, e.g. when viewing a search result. I thought to have a special
> viewing method, but I don't know how to render the body (which has em-tags
> surrounding the search term). Headers and footers are rendered, but I don't
> know how to return a rendered content from the modified self.document_src.
> 
> # - - - - - - - - - - - - - - - - - - - - - - - -
> # ralf.herold@charite.de, last edited 27.11.2001
> 
> from string import replace
> import re
> 
> def hilite(self, REQUEST=None, mark_text=''):
>     """Render terms highligted, e.g.
> aaa/bbb/ccc/hilite?mark_text=whattohilight"""
> 
>     if not hasattr (REQUEST, 'mark_text'): mark_text =
> 'a_very_unusual_nonsense_phrase'
>     if mark_text == '': mark_text = 'a_very_unusual_nonsense_phrase'
> 
>     regexp = re.compile (mark_text, re.IGNORECASE)
>     body   = self.document_src (REQUEST, REQUEST.RESPONSE)
>     tokens = regexp.findall (body)
> 
>     for t in tokens: body = replace (body, t, "<em>" + mark_text + "</em>")
> 
>     if REQUEST: REQUEST.RESPONSE.setHeader ('Content-Type', 'text/html')
> 
>     return self.standard_html_header (self, REQUEST=REQUEST) + body +
> self.standard_html_footer (self, REQUEST=REQUEST)
> # - - - - - - - - - - - - - - - - - - - - - - - -
> 
> Could you help, please? Is this a trivial pythonic problem?
> Is it necessary to make "mark_text" secure for the consecutive re...?
> I would be glad to learn how to do this. Thanks! -- Greetings, Ralf
-- 
Florent Guillaume, Nuxeo SARL (Paris, France)
+33 1 40 33 79 10  http://nuxeo.com  mailto:fg@nuxeo.com