[Zope-CMF] Render question/Search term highlight

R. Herold ralf.herold@charite.de
Thu, 29 Nov 2001 00:22:19 +0100


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

-- Dr. med. Ralf Herold
| Koordinationszentrale Kompetenznetz
| Pädiatrische Onkologie und Hämatologie
| Charité Campus Virchow-Klinikum
| Medizinische Fakultät Humboldt-Universität
| D-13353 Berlin, Augustenburger Platz 1
| mailto:ralf.herold@charite.de