select excerpts that have the term as a word in them
authorTony Cook <tony@develop-help.com>
Fri, 6 Jun 2014 01:18:14 +0000 (11:18 +1000)
committerTony Cook <tony@develop-help.com>
Fri, 6 Jun 2014 01:18:14 +0000 (11:18 +1000)
Previously the code that selected the text to highlight did a simple
text match, without word boundary checks, so a search for "tree" would
select text from the content with "street" which wouldn't then be
highlighted.  The code to select the text now does word matching.

site/cgi-bin/modules/Generate.pm

index d676bd2d8863f42463ab5cf2d4eb00f4478b0af9..87d3b3e09d9bd756a04ff330568dd52fad10ca9b 100644 (file)
@@ -313,7 +313,7 @@ Conditional tag, true if the current article is being embedded.
 
 =cut
 
-our $VERSION = "1.021";
+our $VERSION = "1.022";
 
 my $excerptSize = 300;
 
@@ -1375,21 +1375,28 @@ sub baseActs {
     );
 }
 
+sub _highlight_partial {
+  my ($self) = @_;
+
+  $self->{cfg}->entryBool('search', 'highlight_partial', 1);
+}
+
 sub find_terms {
-  my ($body, $case_sensitive, $terms) = @_;
-  
+  my ($self, $body, $case_sensitive, $terms) = @_;
+
+  my $eow = $self->_highlight_partial ? "" : qr/\b/;
   # locate the terms
   my @found;
   if ($case_sensitive) {
     for my $term (@$terms) {
-      if ($$body =~ /^(.*?)\Q$term/s) {
+      if ($$body =~ /^(.*?)\b\Q$term\E$eow/s) {
        push(@found, [ length($1), $term ]);
       }
     }
   }
   else {
     for my $term (@$terms) {
-      if ($$body =~ /^(.*?)\Q$term/is) {
+      if ($$body =~ /^(.*?)\b\Q$term\E$eow/is) {
        push(@found, [ length($1), $term ]);
       }
     }
@@ -1428,7 +1435,7 @@ sub excerpt {
 
   $type ||= 'body';
 
-  my @found = find_terms(\$body, $case_sensitive, $terms);
+  my @found = $self->find_terms(\$body, $case_sensitive, $terms);
 
   my @reterms = @$terms;
   for (@reterms) {
@@ -1441,7 +1448,7 @@ sub excerpt {
   my $re_str = join("|", reverse sort @reterms);
   my $re;
   my $cfg = $self->{cfg};
-  if ($cfg->entryBool('search', 'highlight_partial', 1)) {
+  if ($self->_highlight_partial) {
     $re = $case_sensitive ? qr/\b($re_str)/ : qr/\b($re_str)/i;
   }
   else {