[Tutor] Highlighting Searched-for text

Magnus Lycka magnus@thinkware.se
Mon Oct 28 12:29:01 2002


At 10:36 2002-10-25 -0400, Schmidt, Allen J. wrote:
>How can I then highlight (change font to red or something) the search text
>on the resulting page display. I have seen this in other online systems but
>want to do it in Python/Zope but have not found a way. I have basic,=
 general
>Python experience.

A na=EFve implementation would be:

 >>> a =3D "This is a text with a word we search for (text) appearing"
 >>> def highlight(text, word, colour=3D"#FF0000"):
...     start =3D '<font colour=3D"%s">' % colour
...     end =3D '</font>'
...     return text.replace(word, start+word+end)
...
 >>> highlight(a, 'text')
'This is a <font colour=3D"#FF0000">text</font> with a word we search for=20
(<font colour=3D"#FF0000">text</font>) appearing'

I think this would work if we know that the string
we make the replacement in is plain text. But if
we do this in HTML we might get into trouble. If
someone would search for "href" in an HTML file
we'll mess up the href tags with this implementation.

Maybe it can somehow be used in conjunction with
a HTML parser or something like that...


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se