--- search.cgi.in Wed Sep 1 12:47:41 2004 +++ searchhiliter.cgi.in Fri Nov 5 12:24:32 2004 @@ -24,8 +24,7 @@ #------------------- Modules -------------------------------------- use SWISH::API; # for searching the index file -use SWISH::ParseQuery; # Parses the query string -use SWISH::PhraseHighlight; # for highlighting +use SWISH::HiLiter; # for highlighting use CGI; # provides a param() method -- could use Apache::Request, for example. use HTML::FillInForm; # makes the form elements sticky use Template; # Template-Toolkit: http://tt2.org or see http://search.cpan.org @@ -212,7 +211,10 @@ # and cache the highlighting object # Note if searching more than one index with differing settings then need one # highlight object for each index - $instance->{highlight_object} = SWISH::PhraseHighlight->new( \%highlight_settings, \%headers, { swish => $swish } ); + $instance->{highlight_object} = SWISH::HiLiter->new( + %highlight_settings, + swish => $swish, + query => $request->{query} ); } @@ -289,7 +291,7 @@ # Create a highlight filter if any results - create_highlight_filter( $instance ) if $result->{hits}; + create_hiliter_filter( $instance ) if $result->{hits}; my $template_output; @@ -301,60 +303,32 @@ # Creates a filter for highlighting search terms -sub create_highlight_filter { - my ( $instance ) = @_; +sub create_hiliter_filter { + my ($instance) = @_; + my $result = $instance->{result}; - my $config = $instance->{config}; - - - # Now create a filter 'highlight' for use in the template to highlight terms - # Usage requires passing in the *metaname* associated with the property - # that's being highlighted -- this allows the program to know what - # search words to use in highlighting - - my $parsed_query = parse_query( join ' ', $result->{results_obj}->ParsedWords( $config->{index} ) ); - - - - + my $hiliter = $instance->{highlight_object}; + # save for Data::Dumper - $result->{parsed_query} = $parsed_query; - + $result->{parsed_query} = $hiliter->query->str; + # Now create the Template-Toolkit "filter" $instance->{template_object}->context->define_filter( 'highlight', - sub { - my ( $context, $property_name, $result_object ) = @_; - - my @phrases; - - # Do we need to map the property name to a metaname? - my $metas = $config->{prop_to_meta}{$property_name} || [ $property_name ]; - - - # Now collect the query words used, if any - # Might also check for duplicate phrases for a small optimization - - for ( @$metas ) { - push @phrases, @{$parsed_query->{$_}} if $parsed_query->{$_}; - } - - # Longest phrases first - @phrases = sort { @$b <=> @$a } @phrases; - - - # Here's the filter factory - return sub { - my $text = shift; - $instance->{highlight_object}->highlight( \$text, \@phrases, $property_name, $result_object ); - return $text; - } - }, - - 1, + sub { + return sub { + my $text = shift; + return $hiliter->light( $hiliter->snip( $text ) ); + } + + }, + + 1, ); } + + __END__ =head1 NAME