[Tutor] Highlighting Searched-for text
Allen Schmidt
aschmidt@fredericksburg.com
Mon Oct 28 17:08:02 2002
Ok, that sorta works...
I forgot to mention that the keywords field is coming in from the form
defined as 'tokens' so that it is a list as it gets to Python.
This is what I did with your code below in a Python script in Zope:
colour=3D"#FF0000"
start =3D '<font colour=3D"%s">' % colour
end =3D '</font>'
if keywords:
for words in keywords:
return adtext.replace(words, start+words+end)
else:
return adtext
If the adtext is "This is a fish dish day." and the keyword is "is", this=
is
what I get:
Th<font colour=3D"#FF0000">i</font>s <font colour=3D"#FF0000">i</font>s a=
f<font
colour=3D"#FF0000">i</font>sh d<font colour=3D"#FF0000">i</font>sh day.
Its just finding the first letter of the keyword. So do I need to inform
Python that keywords is a list?? How does it know if its just a single wo=
rd?
Probably obvious things but I am a PyNewbie...more or less...
Thanks!
Allen
----- Original Message -----
From: "Magnus Lycka" <magnus@thinkware.se>
To: "Schmidt, Allen J." <aschmidt@nvcc.edu>; "'Python Tutor'"
<tutor@python.org>
Sent: Monday, October 28, 2002 12:35 PM
Subject: Re: [Tutor] Highlighting Searched-for text
> 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 system=
s
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 f=
or
> (<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...
>
>
> --
> 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
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>