blib/lib/Search/Tools/HiLiter.pm

filter: Statementstmt 90.0% 261 / 290 Branchbran 67.2% 74 / 110 Conditioncond 47.6% 20 / 42 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 80.7% 21 / 26 Podpod 100% 10 / 10 totaltotal 77.4% 389 / 502 CC 84 SCAR 25.8
CC
84
Cov
80%
plain
37.1
_build_tags
12.6
plain_stemmer
12.2
CRAP 13.3
« blib/lib/Search/Tools/SpellCheck.pm ↑ Summary blib/lib/Search/Tools/UTF8.pm »
1 package Search::Tools::HiLiter;
2 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 extends 'Search::Tools::Object';
4 16 with 'Search::Tools::ArgNormalizer';
5 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Search::Tools::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Search::Tools::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 our $VERSION = '1.007';
14  
15 16 my $XML = Search::Tools::XML->new;
16  
17 16 my @attrs = qw(
18 query
19 tag
20 class
21 style
22 text_color
23 colors
24 tty
25 ttycolors
26 no_html
27 );
28  
29 16 for my $attr (@attrs) {
30 144 has $attr => ( is => 'rw' );
31 }
32  
33 23 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=20.1
Pod documented
34 23 my $self = shift;
35  
36 23 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 23 23
37 0 carp "debug level set at " . $self->debug;
38 }
39  
40 23 $self->{_tokenizer} = Search::Tools::Tokenizer->new(
41 re => $self->query->qp->term_re,
42 debug => $self->debug,
43 );
44  
45 23 ▶ $self->{tag} ||= 'span';
Condition100%2/2
$self->{"tag"} ||= "span"
l!l
518
MC/DC100%1/1
$self->{"tag"} ||= "span"
$self->{"tag"}
Truth table100%2/2
A
A= $self->{"tag"}
Aresult
11
00
46 23 ▶ $self->{colors} ||= [ '#ffff99', '#99ffff', '#ffccff', '#ccccff' ];
Condition50%1/2
$self->{"colors"} ||= ["#ffff99", "#99ffff", "#ffccff", "#ccccff"]
l!l
023
MC/DC0%0/1
$self->{"colors"} ||= ["#ffff99", "#99ffff", "#ffccff", "#ccccff"]
$self->{"colors"}
Truth table50%1/2
A
A= $self->{"colors"}
Aresult
11
00
47 23 ▶ $self->{ttycolors} ||= [ 'bold blue', 'bold red', 'bold green' ];
Condition50%1/2
$self->{"ttycolors"} ||= ["bold blue", "bold red", "bold green"]
l!l
023
MC/DC0%0/1
$self->{"ttycolors"} ||= ["bold blue", "bold red", "bold green"]
$self->{"ttycolors"}
Truth table50%1/2
A
A= $self->{"ttycolors"}
Aresult
11
00
48  
49 23 ▶ if ( $self->tty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tty) 7 16 23
50 7 eval { require Term::ANSIColor };
51 7 ▶ $self->tty(0) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
52 }
53  
54 23 $self->_build_tags;
55 }
56  
57 0 ▶sub terms {
Subroutine terms: not called CC=1 SCAR=6.9
Pod documented
58 0 return shift->{query}->terms;
59 }
60  
61 0 ▶sub keywords {
Subroutine keywords: not called CC=1 SCAR=6.9
Pod documented
62 0 return @{ shift->terms };
63 }
64  
65 0 ▶sub _phrases {
Subroutine _phrases: not called CC=1 SCAR=6.9
66 0 my $self = shift;
67 0 my $q = $self->{query};
68 0 return grep { $self->_regex_for($_)->is_phrase } @{ $q->terms };
69 }
70  
71 0 ▶sub _singles {
Subroutine _singles: not called CC=1 SCAR=6.9
72 0 my $self = shift;
73 0 my $q = $self->{query};
74 0 return grep { !$self->_regex_for($_)->is_phrase } @{ $q->terms };
75 }
76  
77 48 ▶sub _kworder {
Subroutine _kworder: called CC=4 SCAR=13.9
78 48 my $self = shift;
79 48 my $q = $self->{query};
80 48 my $qstr = $q->str;
81 48 ▶ if ( exists $self->{_kworder_cache}->{$qstr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_kworder_cache"}{$qstr}) 25 23 48
82 25 return @{ $self->{_kworder_cache}->{$qstr} };
83 }
84  
85 # do phrases first so that duplicates privilege phrases
86 23 my ( @phrases, @singles );
87  
88 23 for ( @{ $q->terms } ) {
89 60 ▶ if ( $self->_regex_for($_)->is_phrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_regex_for($_)->is_phrase) { } 15 45 60
90 15 push @phrases, $_;
91 }
92 else {
93 45 push @singles, $_;
94 }
95 }
96  
97 23 $self->{_kworder_cache}->{$qstr} = [ @phrases, @singles ];
98  
99 23 return ( @phrases, @singles );
100 }
101  
102 23 ▶sub _build_tags {
Subroutine _build_tags: called CC=12 SCAR=25.3
103 23 my $self = shift;
104  
105 23 my $t = {};
106 23 my @colors = @{ $self->colors };
107 23 my @ttycolors = @{ $self->ttycolors };
108 23 my $tag = $self->tag;
109  
110 23 my $n = 0;
111 23 my $m = 0;
112  
113 23 for my $q ( $self->_kworder ) {
114  
115 # if tty flag is on, use ansicolor instead of html
116 # if debug flag is on, use both html and ansicolor
117  
118 60 my ( %tags, $opener );
119 60 $tags{open} = '';
120 60 $tags{close} = '';
121 60 ▶ if ( $self->class ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->class) { } 10 50 60
elsif ($self->style) { } 0 50 50
elsif ($self->text_color) { } 6 44 50
122 10 $opener = qq/<$tag class='/ . $self->class . qq/'>/;
123 }
124 elsif ( $self->style ) {
125 0 $opener = qq/<$tag style='/ . $self->style . qq/'>/;
126 }
127 elsif ( $self->text_color ) {
128 6 $opener
129 = qq/<$tag style='color:/
130 . $self->text_color
131 . qq/;background:/
132 . $colors[$n] . qq/'>/;
133 }
134 else {
135 44 $opener = qq/<$tag style='background:/ . $colors[$n] . qq/'>/;
136 }
137  
138 60 ▶ if ( $self->tty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tty) { } 15 45 60
139 15 ▶ $tags{open} .= $opener if $self->debug && !$self->no_html;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug and not $self->no_html 0 15 15
Condition33%1/3
$self->debug and not $self->no_html
!ll&&!rl&&r
1500
MC/DC0%0/2
$self->debug and not $self->no_html
$self->debug not $self->no_html
Truth table33%1/3
A and not(B)
A= $self->debugB= not $self->no_html
ABresult
0X0
100
111
140 15 $tags{open} .= Term::ANSIColor::color( $ttycolors[$m] );
141 15 $tags{close} .= Term::ANSIColor::color('reset');
142 15 ▶ $tags{close} .= "</$tag>" if $self->debug && !$self->no_html;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug and not $self->no_html 0 15 15
Condition33%1/3
$self->debug and not $self->no_html
!ll&&!rl&&r
1500
MC/DC0%0/2
$self->debug and not $self->no_html
$self->debug not $self->no_html
Truth table33%1/3
A and not(B)
A= $self->debugB= not $self->no_html
ABresult
0X0
100
111
143 }
144 else {
145 45 $tags{open} .= $opener;
146 45 $tags{close} .= "</$tag>";
147 }
148  
149 60 $t->{$q} = \%tags;
150  
151 60 ▶ $n = 0 if ++$n > $#colors;
Branch: 1 branch
BranchTrueFalseTotal
if ++$n > $#colors 6 54 60
152 60 ▶ $m = 0 if ++$m > $#ttycolors;
Branch: 1 branch
BranchTrueFalseTotal
if ++$m > $#ttycolors 13 47 60
153 }
154  
155 23 $self->{_tags} = $t;
156 }
157  
158 69 ▶sub open_tag {
Subroutine open_tag: called CC=3 SCAR=11.7
Pod documented
159 69 my $self = shift;
160 69 ▶ my $q = shift or croak "need query to get open_tag";
Branch: 1 branch
BranchTrueFalseTotal
my $q = shift() or croak("need query to get open_tag") 69 0 69
161 69 ▶ return $self->{_tags}->{$q}->{open} || '';
Condition50%1/2
$self->{'_tags'}{$q}{'open'} || ''
l!l
690
MC/DC0%0/1
$self->{'_tags'}{$q}{'open'} || ''
$self->{'_tags'}{$q}{'open'}
Truth table50%1/2
A
A= $self->{'_tags'}{$q}{'open'}
Aresult
11
00
162 }
163  
164 69 ▶sub close_tag {
Subroutine close_tag: called CC=3 SCAR=11.7
Pod documented
165 69 my $self = shift;
166 69 ▶ my $q = shift or croak "need query to get close_tag";
Branch: 1 branch
BranchTrueFalseTotal
my $q = shift() or croak("need query to get close_tag") 69 0 69
167 69 ▶ return $self->{_tags}->{$q}->{close} || '';
Condition50%1/2
$self->{'_tags'}{$q}{'close'} || ''
l!l
690
MC/DC0%0/1
$self->{'_tags'}{$q}{'close'} || ''
$self->{'_tags'}{$q}{'close'}
Truth table50%1/2
A
A= $self->{'_tags'}{$q}{'close'}
Aresult
11
00
168 }
169  
170 25 ▶sub light {
Subroutine light: called CC=6 SCAR=18.3
Pod documented
171 25 my $self = shift;
172 25 ▶ my $text = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or return "" 25 0 25
173  
174 # force upgrade. this is so regex will match ok.
175 25 $text = to_utf8($text);
176  
177 25 ▶ if ( $XML->looks_like_html($text) && !$self->no_html ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML->looks_like_html($text) and not $self->no_html) { } 11 14 25
Condition66%2/3
$XML->looks_like_html($text) and not $self->no_html
!ll&&!rl&&r
14011
MC/DC50%1/2
$XML->looks_like_html($text) and not $self->no_html
$XML->looks_like_html($text) not $self->no_html
Truth table66%2/3
A and not(B)
A= $XML->looks_like_html($text)B= not $self->no_html
ABresult
0X0
100
111
178  
179 #warn "running ->html";
180 11 ▶ if ( $self->query->qp->stemmer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->qp->stemmer) 0 11 11
181 0 return $self->html_stemmer($text);
182 }
183 11 return $self->html($text);
184 }
185 else {
186  
187 #warn "running ->plain";
188 14 ▶ if ( $self->query->qp->stemmer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->qp->stemmer) 1 13 14
189 1 return $self->plain_stemmer($text);
190 }
191 13 return $self->plain($text);
192 }
193 }
194  
195 16 *hilite = \&light;
196  
197 29 ▶sub _get_real_html {
Subroutine _get_real_html: called CC=5 SCAR=16.7
198 29 my $self = shift;
199 29 my $text = shift;
200 29 my $re = shift;
201 29 my $m = {};
202 29 ▶ my $debug = $self->debug > 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug > 1 ? : 0 29 29
203  
204 # $1 should be st_bound, $2 should be query, $3 should be end_bound
205 # N.B. The XS version of this algorithm is only a hair faster,
206 # since the $re is the bottleneck.
207 29 ▶ while ( $$text =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$re/g) 43 29 72
208  
209 43 my $pos = pos($$text);
210  
211 43 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 43 43
212 0 carp "$2 matches $re";
213 0 carp "\$1='$1'\n\$2='$2'\n\$3='$3'\npos=$pos";
214 }
215  
216 43 $m->{$2}++;
217  
218 # move back and consider $3 again as possible $1 for next match
219 43 ▶ if ( length($3) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $3) 43 0 43
220 43 pos($$text) = $pos - 1;
221 }
222  
223 }
224  
225 29 return $m;
226  
227 }
228  
229 124 ▶sub _regex_for {
Subroutine _regex_for: called CC=3 SCAR=11.0
230 124 my $self = shift;
231 124 ▶ my $term = shift or croak "term required";
Branch: 1 branch
BranchTrueFalseTotal
my $term = shift() or croak("term required") 124 0 124
232 124 ▶ if ( exists $self->{_regex_for}->{$term} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_regex_for"}{$term}) 64 60 124
233 64 return $self->{_regex_for}->{$term};
234 }
235 60 $self->{_regex_for}->{$term} = $self->query->regex_for($term);
236 60 return $self->{_regex_for}->{$term};
237 }
238  
239 # based on HTML::HiLiter hilite()
240 11 ▶sub html {
Subroutine html: called CC=8 SCAR=20.8
Pod documented
241 11 my $self = shift;
242 11 ▶ my $text = shift or croak "need text to light()";
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or croak("need text to light()") 11 0 11
243  
244 ###################################################################
245 # 1. create hash of query -> [ array of real HTML to hilite ]
246 # using the prebuilt regexp
247 # 2. hilite the real HTML
248 ###################################################################
249  
250 ## 1
251  
252 11 my $q2real = {};
253  
254 # this is going to be query => [ real_html ]
255  
256 # if the query text matched in the text, then we need to
257 # use our prebuilt regexp
258 11 my @kworder = $self->_kworder;
259  
260 # don't consider anything we've marked
261 # with a 'nohiliter' attribute
262 11 my $text_copy = $text;
263 11 $text_copy =~ s/\002.*?\003//sgi;
264  
265 11 Q: for my $query (@kworder) {
266 29 my $re = $self->_regex_for($query)->html;
267 29 my $real = $self->_get_real_html( \$text_copy, $re );
268  
269 29 R: for my $r ( keys %$real ) {
270 35 ▶ push( @{ $q2real->{$query} }, $r ) while $real->{$r}--;
Branch: 1 branch
BranchTrueFalseTotal
$real->{$r}-- and do { push @{$$q2real{$query};}, $r } 43 35 78
271 }
272 }
273  
274 ## 2
275  
276 11 HILITE: for my $q (@kworder) {
277  
278 29 my %uniq_reals = ();
279 29 $uniq_reals{$_}++ for @{ $q2real->{$q} };
280  
281 29 REAL: for my $real ( keys %uniq_reals ) {
282  
283 35 $self->_add_hilite_tags( \$text, $q, $real );
284  
285 }
286  
287 }
288  
289 11 return $text;
290 }
291  
292 35 ▶sub _add_hilite_tags {
Subroutine _add_hilite_tags: called CC=5 SCAR=16.2
293 35 my $self = shift;
294 35 my $text = shift; # reference
295 35 my $query = shift;
296 35 my $html = shift;
297  
298 # $text is reference to original text
299 # $html is the real html that matched our regexp
300  
301 # we still check boundaries just to be safe
302 35 my $st_bound = $self->query->qp->start_bound;
303 35 my $end_bound = $self->query->qp->end_bound;
304  
305 35 my $o = $self->open_tag($query);
306 35 my $c = $self->close_tag($query);
307  
308 35 my $safe = quotemeta($html);
309  
310 # pre-fix nested tags in match
311 35 my $pre_fixed = $html;
312 35 my $tag_re = $self->query->qp->tag_re;
313 35 my $pre_added = $pre_fixed =~ s(${tag_re}+)$c$1$og;
314 35 my $len_added = length( $c . $o ) * $pre_added;
315  
316 # should be same as length( $to_hilite) - length( $prefixed );
317 35 my $len_diff = ( length($html) - length($pre_fixed) );
318 35 ▶ $len_diff *= -1
Branch: 1 branch
BranchTrueFalseTotal
if $len_diff < 0 8 27 35
319 if $len_diff < 0; # pre_added might be -1 if no subs were made
320 35 ▶ if ( $len_diff != $len_added ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_diff != $len_added) 0 35 35
321 0 carp "length math failed!"
322 . "len_diff = $len_diff\nlen_added = $len_added";
323 }
324  
325 35 ▶ while ( $$text =~ m/($st_bound)($safe)($end_bound)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /($st_bound)($safe)($end_bound)/g) 41 35 76
326 41 my $s = $1;
327 41 my $m = $2;
328 41 my $e = $3;
329 41 ▶ if ( $self->debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug > 1) 0 41 41
330 0 carp "matched:\n'$s'\n'$m'\n'$e'\n"
331 . "\$1 is "
332 . ord($s)
333 . "\$3 is "
334 . ord($e);
335 }
336  
337 # use substr to do what s// would normally do if pos() wasn't an issue
338 # -- is this a big speed hit?
339 41 my $len = length( $s . $m . $e );
340 41 my $pos = pos($$text);
341 41 my $newstring = $s . $o . $pre_fixed . $c . $e;
342 41 substr( $$text, $pos - $len, $len, $newstring );
343  
344 41 pos($$text) = $pos + length( $o . $c ) + $len_added - 1;
345  
346 # adjust for new text added
347 # $pre_fixed is the hard bit, since we must take $len_added into account
348 # move back 1 to reconsider $3 as next $1
349  
350 # warn "pos was $pos\nnow ", pos( $html ), "\n";
351 # warn "new: '$html'\n";
352 # warn "new text: '$newstring'\n";
353 # warn "first chars of new pos are '", substr( $html, pos($html), 10 ), "'\n";
354  
355 }
356  
357 35 $self->_clean_up_hilites( $text, $query, $o, $c, $safe );
358  
359 }
360  
361 # no algorithm is perfect. fix it as best we can.
362 35 ▶sub _clean_up_hilites {
Subroutine _clean_up_hilites: called CC=5 SCAR=16.3
363  
364 35 my $self = shift;
365 35 my ( $text, $query, $o, $c, $safe ) = @_;
366  
367 # empty hilites are useless
368 35 ▶ my $empty = ( $$text =~ s,\Q$o$c\E,,sgi ) || 0;
Condition50%1/2
$$text =~ s/\Q$o$c\E//sig || 0
l!l
035
MC/DC0%0/1
$$text =~ s/\Q$o$c\E//sig || 0
$$text =~ s/\Q$o$c\E//sig
Truth table50%1/2
A
A= $$text =~ s/\Q$o$c\E//sig
Aresult
11
00
369  
370 #$self->debug and carp "looking for split entities: (&[\\w#]*)\Q$o\E(?:\Q$c\E)(${safe})\Q$c\E([\\w#]*;)";
371  
372 # to be safe: in some cases we might match against entities or within tag content.
373 35 ▶ my $ent_split = (
Condition50%1/2
$$text =~ s/(&[\w#]*)\Q$o\E(?:\Q$c\E)?($safe)\Q$c\E([\w#]*;)/$1$2$3/sig || 0
l!l
035
MC/DC0%0/1
$$text =~ s/(&[\w#]*)\Q$o\E(?:\Q$c\E)?($safe)\Q$c\E([\w#]*;)/$1$2$3/sig || 0
$$text =~ s/(&[\w#]*)\Q$o\E(?:\Q$c\E)?($safe)\Q$c\E([\w#]*;)/$1$2$3/sig
Truth table50%1/2
A
A= $$text =~ s/(&[\w#]*)\Q$o\E(?:\Q$c\E)?($safe)\Q$c\E([\w#]*;)/$1$2$3/sig
Aresult
11
00
374 $$text
375 =~ s/(&[\w#]*)\Q$o\E(?:\Q$c\E)?(${safe})\Q$c\E([\w#]*;)/$1$2$3/igs # is i and s necessary?
376 ) || 0;
377  
378 #$self->debug and carp "found $ent_split split entities";
379  
380 35 my $tag_split = 0;
381 35 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /(<[^<>]*)\Q$o\E($safe)\Q$c\E([^>]*>)/gisx) 4 35 39
382 $$text
383 =~ m/(<[^<>]*)\Q$o\E($safe)\Q$c\E([^>]*>)/gxsi # are these xsi flags necessary?
384 )
385 {
386  
387 4 my $first = $1;
388 4 my $second = $2;
389 4 my $third = $3;
390 4 ▶ carp "appears to split tag: $first - $second - $third"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 4 4
391 if $self->debug > 1;
392  
393 # TODO this would be one place to highlight text where attributes match
394  
395 4 $tag_split += (
396 $$text =~ s/(<[^<>]*)\Q$o\E($safe)\Q$c\E([^>]*>)/$1$2$3/gxsi );
397  
398 }
399  
400 }
401  
402 0 ▶sub html_stemmer {
Subroutine html_stemmer: not called CC=1 SCAR=6.9
Pod documented
403 0 my $self = shift;
404 0 my $text = shift;
405 0 return $self->plain_stemmer($text);
406 }
407  
408 1 ▶sub plain_stemmer {
Subroutine plain_stemmer: called CC=12 SCAR=25.0
Pod documented
409 1 my $self = shift;
410 1 ▶ my $text = shift or croak "need text";
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or croak("need text") 1 0 1
411 1 my $debug = $self->debug;
412  
413 1 my @kworder = $self->_kworder;
414  
415 # if stemmer is on, we must stem each token to look for a match
416 1 my $qre = $self->query->terms_as_regex(1);
417 1 $qre =~ s/(\\ )+/\|/g; # TODO OR phrases together if (0) above?
418  
419 1 my $re = qr/^$qre$/;
420 1 my $stemmer = $self->query->qp->stemmer;
421 1 my $qp = $self->query->qp;
422 1 my $wildcard = $qp->wildcard;
423 34 ▶ my $heat_seeker = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
424 34 my ($token) = @_;
425 34 my $st = $stemmer->( $qp, $token->str );
426 34 return $st =~ m/$re/;
427 1 };
428  
429 1 my $tokens = $self->{_tokenizer}->tokenize( $text, $heat_seeker );
430  
431 # create a new string
432 1 my $buf;
433  
434 # iterate over tokens, looking for any hot ones,
435 # and create a new string
436 1 ▶TOK: while ( my $tok = $tokens->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tok = $tokens->next) 69 1 70
437 69 my $str = $tok->str;
438 69 ▶ if ( $tok->is_hot ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->is_hot) 3 66 69
439  
440 # find the matching query term
441  
442 3 my $stemmed = $stemmer->( $qp, $str );
443 3 my $found_match = 0;
444 3 Q: for my $query (@kworder) {
445 4 my $regex = $self->_regex_for($query);
446 4 my @regex_to_try;
447  
448 # if it is a phrase, try each term in the phrase
449 4 ▶ if ( $regex->is_phrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex->is_phrase) { } 3 1 4
450 3 @regex_to_try = @{ $regex->phrase_terms };
451 }
452 else {
453 1 @regex_to_try = ($regex);
454 }
455 4 REGEX: for my $r (@regex_to_try) {
456 6 my $term_re = $r->term_re;
457 6 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "testing '${stemmed}' against '${query}' with '${term_re}'" 0 6 6
458 and warn
459 "testing '$stemmed' against '$query' with '$term_re'";
460 6 ▶ if ( $stemmed =~ m/$term_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stemmed =~ /$term_re/) 3 3 6
461 3 my $open = $self->open_tag($query);
462 3 my $close = $self->close_tag($query);
463 3 ▶ $debug and warn "$str is hot with match '$query'";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "$str is hot with match '${query}'" 0 3 3
464 3 $str = $open . $str . $close;
465 3 $found_match = 1;
466 3 last Q;
467 }
468  
469 }
470 }
471  
472 3 ▶ if ( !$found_match ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_match) 0 3 3
473  
474 # common case is phrases?
475 0 ▶ $debug and warn "failed to find match for '$stemmed'";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "failed to find match for '${stemmed}'" 0 0 0
476  
477 }
478 }
479 69 $buf .= $str;
480 }
481 1 return $buf;
482 }
483  
484 # based on HTML::HiLiter plaintext()
485 13 ▶sub plain {
Subroutine plain: called CC=23 SCAR=36.1
Pod documented
486 13 my $self = shift;
487 13 ▶ my $text = shift or croak "need text to light()";
Branch: 1 branch
BranchTrueFalseTotal
my $text = shift() or croak("need text to light()") 13 0 13
488 13 my $debug = $self->debug;
489 13 my $query_obj = $self->{query};
490 13 my @kworder = $self->_kworder;
491  
492 13 my $i = 0;
493 13 my @markers;
494 13 Q: for my $query (@kworder) {
495 31 my $regex = $self->_regex_for($query);
496 31 my $re = $regex->plain;
497 31 my $term_re = $regex->term_re;
498 31 my $open = $self->open_tag($query);
499 31 my $close = $self->close_tag($query);
500  
501 # use open/close markers rather than actual html tags
502 # because we do not want to get double matches on text
503 # like 'span' or 'style'
504 31 my $o = chr($i) . "\002";
505 31 my $c = chr($i) . "\003";
506 31 my $length_we_add = length( $o . $c ) - 1;
507 31 push @markers, [ $open, $close ];
508  
509 # cache this
510 31 ▶ my $query_re = $self->{_compiled_query_regex}->{"$query"}
Condition66%2/3
$self->{'_compiled_query_regex'}{"$query"} || quotemeta $query
l!l&&r!l&&!r
1300
MC/DC0%0/2
$self->{'_compiled_query_regex'}{"$query"} || quotemeta $query
$self->{'_compiled_query_regex'}{"$query"} quotemeta $query
Truth table66%2/3
A || B
A= $self->{'_compiled_query_regex'}{"$query"}B= quotemeta $query
ABresult
1X1
011
000
511 || quotemeta($query);
512 31 ▶ if ( !$self->{_compiled_query_regex}->{"$query"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_compiled_query_regex"}{"$query"}) 30 1 31
513 30 $self->{_compiled_query_regex}->{"$query"} = qr/$query_re/;
514 }
515  
516 31 ▶ $debug > 1
Branch: 1 branch
BranchTrueFalseTotal
$debug > 1 and carp("plain hiliter looking for: $re against '${query}' in '${text}'") 0 31 31
517 and carp
518 "plain hiliter looking for: $re against '$query' in '$text'";
519  
520 # because s/// fails to find duplicate instances like 'foo foo'
521 # we use a while loop and increment pos()
522  
523 # this can suck into an infinite loop because increm pos()-- results
524 # in repeated match on nonwordchar: > (since we just added a tag)
525  
526 31 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 31 31
527 0 ▶ if ( $text =~ m/\b$query_re\b/i && $text !~ m/$re/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\b$query_re\b/i and not $text =~ /$re/i) 0 0 0
Condition0%0/3
$text =~ /\b$query_re\b/i and not $text =~ /$re/i
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ /\b$query_re\b/i and not $text =~ /$re/i
$text =~ /\b$query_re\b/i not $text =~ /$re/i
Truth table0%0/3
A and not(B)
A= $text =~ /\b$query_re\b/iB= not $text =~ /$re/i
ABresult
0X0
100
111
528 0 my ($snip) = ( $text =~ m/(.....$query_re.....)/gi );
529 0 croak "bad regex for '$query' [$snip]: $re";
530 }
531 }
532  
533 31 my $found_matches = 0;
534 31 ▶ while ( $text =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$re/g) 31 31 62
535  
536 31 ▶ my $s = $1 || '';
Condition100%2/2
$1 || ''
l!l
301
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
537 31 ▶ my $m = $2 || $query;
Condition33%1/3
$2 || $query
l!l&&r!l&&!r
3100
MC/DC0%0/2
$2 || $query
$2 $query
Truth table33%1/3
A || B
A= $2B= $query
ABresult
1X1
011
000
538 31 ▶ my $e = $3 || '';
Condition50%1/2
$3 || ''
l!l
310
MC/DC0%0/1
$3 || ''
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
539  
540 31 $found_matches++;
541  
542 31 ▶ $debug > 1 and carp "matched $s $m $e against $re";
Branch: 1 branch
BranchTrueFalseTotal
$debug > 1 and carp("matched $s $m $e against $re") 0 31 31
543  
544 # use substr to do what s/// would normally do
545 # if pos() wasn't an issue -- is this a big speed diff?
546 31 my $len = length( $s . $m . $e );
547 31 my $pos = pos($text);
548 31 ▶ $debug > 1 and carp "pos==$pos len==$len";
Branch: 1 branch
BranchTrueFalseTotal
$debug > 1 and carp("pos==$pos len==$len") 0 31 31
549 31 my $newstring = $s . $o . $m . $c . $e;
550 31 substr( $text, $pos - $len, $len, $newstring );
551  
552 31 ▶ last if $pos == length $text;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == length $text 0 31 31
553  
554 # need to account for all the new chars we just added
555 31 pos($text) = $pos + $length_we_add;
556 31 ▶ $debug > 1
Branch: 1 branch
BranchTrueFalseTotal
$debug > 1 and carp("length_we_add==$length_we_add pos==" . pos($text)) 0 31 31
557 and carp "length_we_add==$length_we_add pos==" . pos($text);
558  
559 }
560  
561 31 ▶ $debug and warn "found $found_matches matches";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "found $found_matches matches" 0 31 31
562  
563 # sanity check similar to Snipper->_re_snip()
564 31 ▶ if ( $debug and !$found_matches and $text =~ m/$query_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $found_matches and $text =~ /$query_re/) 0 31 31
Condition33%2/6
$debug and not $found_matches and $text =~ /$query_re/
!ll&&!rl&&r
3100
$debug and not $found_matches
!ll&&!rl&&r
3100
MC/DC0%0/3
$debug and not $found_matches and $text =~ /$query_re/
$debug not $found_matches $text =~ /$query_re/
Truth table25%1/4
A and not(B) and C
A= $debugB= not $found_matchesC= $text =~ /$query_re/
ABCresult
0XX0
10X0
1100
1111
565 0 ▶ $debug and warn "ERROR: regex failure for '$query'";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "ERROR: regex failure for '${query}'" 0 0 0
566 0 $text = $self->html($text);
567 }
568  
569 # increment the marker
570 31 $i++;
571  
572 }
573  
574 # now our markers replaced with actual tags
575 13 $i = 0;
576 13 for my $set (@markers) {
577 31 my $ichr = quotemeta( chr($i) );
578 31 $text =~ s/$ichr\002/$set->[0]/g;
579 31 $text =~ s/$ichr\003/$set->[1]/g;
580 31 $i++;
581 }
582  
583 #warn "plain done";
584  
585 13 return $text;
586  
587 }
588  
589 16 1;
590 __END__
« blib/lib/Search/Tools/SpellCheck.pm ↑ Summary blib/lib/Search/Tools/UTF8.pm »