arunmep has asked for the wisdom of the Perl Monks concerning the following question:

hai every body iam working on a search engine in perl\tk iam using tk to open the search results in a separate window say in word doc,PDF, openofficedoc etc.now my problem is i need to highlight the search key words in the files can it be done please suggest me a way thank you

Replies are listed 'Best First'.
Re: how to open and highlight text
by zentara (Cardinal) on Jan 11, 2006 at 13:16 UTC
    I am not clear on what you are trying to do. You say you are displaying the results with Tk, and you want to highlight something. Highlight what? Words, links? I would think you would need a text widget, or even a canvas to do this. Are you looking for something like this?
    #!/usr/bin/perl use Tk; $mw = MainWindow->new( -title => "hyperlinks" ); $t = $mw->Scrolled('Text')->pack; $tag = "tag000"; foreach (<DATA>) { chomp; split (/(http:\S+)/); foreach (@_) { if (/(http:\S+)/) { $t->insert( 'end', $_, $tag ); $t->tagConfigure( $tag, -foreground => 'blue' ); $t->tagBind( $tag, '<Any-Enter>' => [ \&manipulate_link, $tag, 'raised', +'hand2' ] ); $t->tagBind( $tag, '<Any-Leave>' => [ \&manipulate_link, $tag, 'flat', 'x +term' ] ); $t->tagBind( $tag, '<Button-1>' => [ \&manipulate_link, $tag, 'sunken' ] +); $t->tagBind( $tag, '<ButtonRelease-1>' => [ \&manipulate_link, $tag, 'raised', undef, \&printm +e ] ); $tag++; } else { $t->insert( 'end', $_ ); } } $t->insert( 'end', "\n" ); } MainLoop; sub printme { local ($,) = " "; print "printme:", @_, "\n"; } sub manipulate_link { # manipulate the link as you press the mouse key my ($a) = shift; my ($tag) = shift; my ($relief) = shift; my ($cursor) = shift; my ($after) = shift; # by configuring the relief (to simulate a button press) $a->tagConfigure( $tag, -relief => $relief, -borderwidth => 1 ); # by changing the cursor between hand and xterm $a->configure( -cursor => $cursor ) if ($cursor); # and by scheduling the specified action to run "soon" if ($after) { my ($s) = $a->get( $a->tagRanges($tag) ); $main::mw->after( 200, [ $after, $a, $s, $tag, @_ ] ) if ($aft +er); } } __DATA__ Hi there. This is text. THis is more text but http://this.is.a/hyperlink in a line. http://this.is.another/hyperlink followed by http://this.is.a.third/hyperlink __END__

    I'm not really a human, but I play one on earth. flash japh
Re: how to open and highlight text
by thundergnat (Deacon) on Jan 11, 2006 at 15:32 UTC

    I did something similar not too long ago. Here's a little demo script showing one way to do it. It has configurable search parameters and highlight colors.

    I see zentara has already posted an example. Oh well. Here's another :-) For demonstration purposes, it loads itself into the text widget.

Re: how to open and highlight text
by dorward (Curate) on Jan 11, 2006 at 13:09 UTC

    I'm having some trouble understanding your question. When you say

    iam using tk to open the search results in a separate window say in word doc,PDF, openofficedoc etc.

    Are you generating such a file (containing the listing of search results) on the fly and want to highlight terms in a summary associated with the result?

    Are you displaying the results in some other way, but when a result is selected you want to open it in its native application with highlighting? (e.g. User searches for "foo", "foo" is found in "bar.pdf", user clicks the "bar.pdf" result in the results display, "bar.pdf" is opened in Adobe Acrobat Reader with highlighting of each instance of "foo").

    Something else?

      actually i have developed a search engine which searches a set of data in different file formats(*.doc,*.pdf etc). the users gives the keyword and the search engine searches the files for the keyword instance once it is found the results are displayed along with a link to open the file in its native file format. i have done up to this part now i need to have the instance of the words in the opened file to be high lighted

        a link to open the file in its native file format

        i need to have the instance of the words in the opened file to be high lighted

        In some, and likely all, cases you are going to have to modify the file. This means that either you are going to be creating a temporary file and presenting that to the user (making highlighted search results effectively Read Only), or you are going to be making changes to the original document.

        Either way, how you go about making the changes to the files depends on the data format (i.e. you are going to need a different solution for PDF then for OpenDocument).

        Given that you are searching them already, I think it is safe to assume that you can read the files without much difficulty. This means you might want to consider the Google approach - which is to create a copy of the document in a single standard format (Google use a combination of invalid HTML and invalid CSS) and make your changes to that copy.