blib/lib/HTML/HiLiter.pm

filter: Statementstmt 69.6% 188 / 270 Branchbran 60.9% 67 / 110 Conditioncond 36.1% 17 / 47 MC/DCmcdc 10.3% 3 / 29 Subroutinesub 89.1% 33 / 37 Podpod 100% 4 / 4 totaltotal 62.7% 312 / 497 CC 74 SCAR 23.8
CC
74
Cov
64%
_get_url
30.0
_setup_back_compat
22.5
_check_count
20.0
CRAP 10.8
↑ Summary
1 package HTML::HiLiter;
2 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use 5.008003; # Search::Tools requires this
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 with 'Search::Tools::ArgNormalizer';
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Search::Tools::QueryParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Search::Tools::HiLiter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # HTML::Tagset::isHeadElement doesn't define these,
14 # so we add them here
15 6 $HTML::Tagset::isHeadElement{'head'}++;
16 6 $HTML::Tagset::isHeadElement{'html'}++;
17  
18 0 ▶sub _init_debug { return $ENV{PERL_DEBUG} || 0 }
Subroutine _init_debug: not called CC=2 SCAR=17.9
Condition0%0/2
$ENV{'PERL_DEBUG'} || 0
l!l
00
MC/DC0%0/1
$ENV{'PERL_DEBUG'} || 0
$ENV{'PERL_DEBUG'}
Truth table0%0/2
A
A= $ENV{'PERL_DEBUG'}
Aresult
11
00
19  
20 6 has 'debug' => (
21 is => 'rw',
22 isa => 'Maybe[Int]',
23 lazy => 1,
24 builder => '_init_debug',
25 );
26 6 has 'hiliter' => (
27 is => 'rw',
28 isa => 'Search::Tools::HiLiter',
29 lazy => 1,
30 builder => '_init_hiliter',
31 );
32 6 has 'query' => (
33 is => 'rw',
34 isa => 'Search::Tools::Query',
35 );
36 8 ▶has 'buffer_limit' => ( is => 'rw', isa => 'Int', default => sub { 2**16 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 ▶has 'print_stream' => ( is => 'rw', isa => 'Bool', default => sub {1} );
Subroutine __ANON__: called CC=1 SCAR=0.0
38 4 ▶has 'fh' => ( is => 'rw', isa => 'FileHandle', default => sub { \*STDOUT } );
Subroutine __ANON__: called CC=1 SCAR=0.0
39 6 has 'style_header' => ( is => 'rw', isa => 'Maybe[Str]', );
40  
41 # Search::Tools::HiLilter attributes we want to proxy through
42 8 ▶has 'tag' => ( is => 'rw', isa => 'Str', default => sub {'span'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 6 for my $attr (qw( class style colors text_color tty )) {
44 30 has $attr => ( is => 'rw' );
45 }
46  
47 6 our $VERSION = '0.201';
48  
49 # some global debugging vars
50 6 my $open_comment = "\n<!--\n";
51 6 my $close_comment = "\n-->\n";
52  
53 ################################################################################
54 # char tables below are from pre 0.14. keeping here for reference, just in case.
55 #
56 #
57 # a subset of chars per SWISH
58 #$ISO_ext
59 # = 'ªµºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ';
60  
61 ######################################################################################
62 # http://www.pemberley.com/janeinfo/latin1.html
63 # The CP1252 characters that are not part of ANSI/ISO 8859-1, and that should therefore
64 # always be encoded as Unicode characters greater than 255, are the following:
65  
66 # Windows Unicode Char.
67 # char. HTML code test Description of Character
68 # ----- ----- --- ------------------------
69 #ALT-0130 &#8218; â Single Low-9 Quotation Mark
70 #ALT-0131 &#402; Ä Latin Small Letter F With Hook
71 #ALT-0132 &#8222; ã Double Low-9 Quotation Mark
72 #ALT-0133 &#8230; É Horizontal Ellipsis
73 #ALT-0134 &#8224;   Dagger
74 #ALT-0135 &#8225; à Double Dagger
75 #ALT-0136 &#710; ö Modifier Letter Circumflex Accent
76 #ALT-0137 &#8240; ä Per Mille Sign
77 #ALT-0138 &#352; ? Latin Capital Letter S With Caron
78 #ALT-0139 &#8249; Ü Single Left-Pointing Angle Quotation Mark
79 #ALT-0140 &#338; Î Latin Capital Ligature OE
80 #ALT-0145 &#8216; Ô Left Single Quotation Mark
81 #ALT-0146 &#8217; Õ Right Single Quotation Mark
82 #ALT-0147 &#8220; Ò Left Double Quotation Mark
83 #ALT-0148 &#8221; Ó Right Double Quotation Mark
84 #ALT-0149 &#8226; ¥ Bullet
85 #ALT-0150 &#8211; Ð En Dash
86 #ALT-0151 &#8212; Ñ Em Dash
87 #ALT-0152 &#732; ÷ Small Tilde
88 #ALT-0153 &#8482; ª Trade Mark Sign
89 #ALT-0154 &#353; ? Latin Small Letter S With Caron
90 #ALT-0155 &#8250; Ý Single Right-Pointing Angle Quotation Mark
91 #ALT-0156 &#339; Ï Latin Small Ligature OE
92 #ALT-0159 &#376; Ù Latin Capital Letter Y With Diaeresis
93 #
94 #######################################################################################
95  
96 # NOTE that all the Char tests will likely fail above unless your terminal/editor
97 # supports Unicode
98  
99 # browsers should support these numbers, and in order for perl < 5.8 to work correctly,
100 # we add the most common if missing
101  
102 #%unicodes = (
103 # 8218 => "'",
104 # 402 => 'f',
105 # 8222 => '"',
106 # 8230 => '...',
107 # 8224 => 't',
108 # 8225 => 't',
109 # 8216 => "'",
110 # 8217 => "'",
111 # 8220 => '"',
112 # 8221 => '"',
113 # 8226 => '*',
114 # 8211 => '-',
115 # 8212 => '-',
116 # 732 => '~',
117 # 8482 => '(TM)',
118 # 376 => 'Y',
119 # 352 => 'S',
120 # 353 => 's',
121 # 8250 => '>',
122 # 8249 => '<',
123 # 710 => '^',
124 # 338 => 'OE',
125 # 339 => 'oe',
126 #);
127 #
128 #for ( keys %unicodes ) {
129 #
130 # # quotemeta required since build_regexp will look for the \
131 # my $ascii = quotemeta( $unicodes{$_} );
132 # next if length $ascii > 2;
133 #
134 # #warn "pushing $_ into $ascii\n";
135 # push( @{ $codeunis{$ascii} }, $_ );
136 #}
137  
138 ################################################################################
139  
140 8 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod documented
141 8 my $self = shift;
142  
143 8 ▶ $self->{debug} ||= 0;
Condition50%1/2
$self->{"debug"} ||= 0
l!l
08
MC/DC0%0/1
$self->{"debug"} ||= 0
$self->{"debug"}
Truth table50%1/2
A
A= $self->{"debug"}
Aresult
11
00
144  
145 #dump $self;
146 8 $self->_setup_back_compat();
147 8 $self->_setup();
148  
149 #dump $self;
150  
151 8 return $self;
152 }
153  
154 8 ▶sub _setup_back_compat {
Subroutine _setup_back_compat: called CC=10 SCAR=31.1
155 8 my $self = shift;
156  
157 8 ▶ if ( defined( $self->{Print} ) && $self->{Print} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Print"} and $self->{"Print"} == 0) 0 8 8
Condition33%1/3
defined $self->{"Print"} and $self->{"Print"} == 0
!ll&&!rl&&r
800
MC/DC0%0/2
defined $self->{"Print"} and $self->{"Print"} == 0
defined $self->{"Print"} $self->{"Print"} == 0
Truth table33%1/3
A and B
A= defined $self->{"Print"}B= $self->{"Print"} == 0
ABresult
0X0
100
111
158 0 $self->{print_stream} = 0;
159 }
160 8 ▶ if ( exists $self->{TagFilter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"TagFilter"}) 0 8 8
161 0 $self->{tag_filter} = delete $self->{TagFilter};
162 }
163 8 ▶ if ( exists $self->{TextFilter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"TextFilter"}) 0 8 8
164 0 $self->{text_filter} = delete $self->{TextFilter};
165 }
166 8 ▶ if ( exists $self->{HiTag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"HiTag"}) 0 8 8
167 0 $self->{tag} = delete $self->{HiTag};
168 }
169 8 ▶ if ( exists $self->{HiClass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"HiClass"}) 0 8 8
170 0 $self->{class} = delete $self->{HiClass};
171 }
172 8 ▶ if ( exists $self->{Colors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Colors"}) 0 8 8
173 0 $self->{colors} = delete $self->{Colors};
174 }
175 8 ▶ if ( exists $self->{Links} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Links"}) 0 8 8
176 0 $self->{hilite_links} = delete $self->{Links};
177 }
178  
179 8 ▶ if ( exists $self->{noplain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"noplain"}) 0 8 8
180 0 carp
181 "'noplain' is deprecated, and is always performed automatically.";
182 }
183  
184 }
185  
186 8 ▶sub _setup {
Subroutine _setup: called CC=3 SCAR=13.3
187 8 my $self = shift;
188  
189 8 ▶ if ( exists $self->{parser} && $self->{parser} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"parser"} and $self->{"parser"} == 0) 0 8 8
Condition33%1/3
exists $self->{"parser"} and $self->{"parser"} == 0
!ll&&!rl&&r
800
MC/DC0%0/2
exists $self->{"parser"} and $self->{"parser"} == 0
exists $self->{"parser"} $self->{"parser"} == 0
Truth table33%1/3
A and B
A= exists $self->{"parser"}B= $self->{"parser"} == 0
ABresult
0X0
100
111
190 0 croak
191 "use Search::Tools::HiLiter directly instead of HTML::HiLiter without a parser";
192 }
193  
194 8 $self->{_terms_regex} = $self->{query}->terms_as_regex;
195 }
196  
197 7 ▶sub _init_hiliter {
Subroutine _init_hiliter: called CC=1 SCAR=0.0
198 7 my $self = shift;
199 7 return Search::Tools::HiLiter->new(
200 tag => $self->tag,
201 class => $self->class,
202 colors => $self->colors,
203 style => $self->style,
204 text_color => $self->text_color,
205 query => $self->query,
206 tty => $self->tty,
207 debug => $self->debug,
208 );
209 }
210  
211 139 ▶sub _handle_tag {
Subroutine _handle_tag: called CC=7 SCAR=25.7
212 139 my ($self, $parser, $tag, $tagname, $offset,
213 $length, $offset_end, $attr, $text
214 ) = @_;
215  
216 139 my $is_end_tag = $tag =~ m/^\//;
217  
218 # $tag has ! for declarations and / for endtags
219 # $tagname is just bare tagname
220  
221 139 ▶ if ( $self->debug >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug >= 3) 0 139 139
222 0 print { $self->{fh} } $open_comment;
223 0 print { $self->{fh} } "\n" . '=' x 20 . "\n";
224 0 print { $self->{fh} } "Tag :$tag:\n";
225 0 print { $self->{fh} } "TagName :$tagname:\n";
226 0 print { $self->{fh} } "Offset :$offset\n";
227 0 print { $self->{fh} } "Length :$length\n";
228 0 print { $self->{fh} } "Offset_end :$offset_end\n";
229 0 print { $self->{fh} } "Text :$text\n";
230 0 print { $self->{fh} } "Attr :" . dump($attr) . "\n";
231 0 print { $self->{fh} } "skipping_tag :$self->{_skipping_tag}:\n";
232 0 print { $self->{fh} } "is_end_tag :$is_end_tag\n";
233 0 print { $self->{fh} } $close_comment;
234 }
235  
236 # turn HiLiting ON if we are not inside the <head> tagset.
237 # this prevents us from hiliting a <title> for example.
238 139 ▶ if ( !$self->{_is_hiliting} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_is_hiliting"}) 32 107 139
239 32 ▶ if ( !exists $HTML::Tagset::isHeadElement{$tagname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $HTML::Tagset::isHeadElement{$tagname}) { } 8 24 32
240 8 ▶ $self->debug and carp "turning is_hiliting on for <$tag>";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and carp("turning is_hiliting on for <$tag>") 0 8 8
241 8 $self->{_is_hiliting} = 1;
242 }
243 else {
244  
245 24 $self->_meta_charset_check( $tag, $attr, \$text );
246  
247 # still in <head> section. handle and continue.
248 24 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 6 18 24
249 6 print { $self->{fh} } $text;
250 }
251 else {
252 18 $self->{output_buffer} .= $text;
253 }
254 24 return;
255 }
256 }
257  
258 115 ▶ if ($is_end_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_end_tag) { } 56 59 115
259 56 $self->_handle_end_tag( $parser, $tag, $tagname, $offset, $length,
260 $offset_end, $attr, $text );
261 }
262 else {
263 59 $self->_handle_start_tag( $parser, $tag, $tagname, $offset, $length,
264 $offset_end, $attr, $text );
265 }
266 }
267  
268 24 ▶sub _meta_charset_check {
Subroutine _meta_charset_check: called CC=10 SCAR=26.7
269 24 my ( $self, $tag, $attr, $text ) = @_;
270  
271 # if this is a meta tag, check for encoding. we want to make sure
272 # we do not declare anything other than utf-8 or ascii in the output,
273 # since Search::Tools::HiLiter always returns utf-8.
274 24 ▶ if ( lc($tag) eq 'meta' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $tag eq "meta") 4 20 24
275 4 ▶ if ( exists $attr->{'http-equiv'} or exists $attr->{'HTTP-EQUIV'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr->{"http-equiv"} or exists $attr->{"HTTP-EQUIV"}) 1 3 4
Condition66%2/3
exists $attr->{"http-equiv"} or exists $attr->{"HTTP-EQUIV"}
l!l&&r!l&&!r
103
MC/DC50%1/2
exists $attr->{"http-equiv"} or exists $attr->{"HTTP-EQUIV"}
exists $attr->{"http-equiv"} exists $attr->{"HTTP-EQUIV"}
Truth table66%2/3
A or B
A= exists $attr->{"http-equiv"}B= exists $attr->{"HTTP-EQUIV"}
ABresult
1X1
011
000
276 1 ▶ if ( exists $attr->{content} or exists $attr->{CONTENT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr->{"content"} or exists $attr->{"CONTENT"}) 1 0 1
Condition33%1/3
exists $attr->{"content"} or exists $attr->{"CONTENT"}
l!l&&r!l&&!r
100
MC/DC0%0/2
exists $attr->{"content"} or exists $attr->{"CONTENT"}
exists $attr->{"content"} exists $attr->{"CONTENT"}
Truth table33%1/3
A or B
A= exists $attr->{"content"}B= exists $attr->{"CONTENT"}
ABresult
1X1
011
000
277 1 ▶ my $name = $attr->{'http-equiv'} || $attr->{'HTTP-EQUIV'};
Condition33%1/3
$attr->{'http-equiv'} || $attr->{'HTTP-EQUIV'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$attr->{'http-equiv'} || $attr->{'HTTP-EQUIV'}
$attr->{'http-equiv'} $attr->{'HTTP-EQUIV'}
Truth table33%1/3
A || B
A= $attr->{'http-equiv'}B= $attr->{'HTTP-EQUIV'}
ABresult
1X1
011
000
278 1 ▶ my $content = $attr->{content} || $attr->{CONTENT};
Condition33%1/3
$attr->{'content'} || $attr->{'CONTENT'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$attr->{'content'} || $attr->{'CONTENT'}
$attr->{'content'} $attr->{'CONTENT'}
Truth table33%1/3
A || B
A= $attr->{'content'}B= $attr->{'CONTENT'}
ABresult
1X1
011
000
279 1 ▶ if ( lc($name) eq 'content-type'
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq "content-type" and not lc($content) =~ /ascii|utf-8/i) 1 0 1
Condition33%1/3
lc $name eq "content-type" and not lc($content) =~ /ascii|utf-8/i
!ll&&!rl&&r
001
MC/DC0%0/2
lc $name eq "content-type" and not lc($content) =~ /ascii|utf-8/i
lc $name eq "content-type" not lc($content) =~ /ascii|utf-8/i
Truth table33%1/3
A and not(B)
A= lc $name eq "content-type"B= not lc($content) =~ /ascii|utf-8/i
ABresult
0X0
100
111
280 && lc($content) !~ m/ascii|utf-8/i )
281 {
282 1 $$text
283 = qq(<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>);
284 }
285 }
286 }
287 }
288 }
289  
290 56 ▶sub _handle_end_tag {
Subroutine _handle_end_tag: called CC=4 SCAR=13.9
291 56 my ($self, $parser, $tag, $tagname, $offset,
292 $length, $offset_end, $attr, $text
293 ) = @_;
294  
295 56 ▶ if ( $self->{_skipping_tag} eq $tagname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_skipping_tag"} eq $tagname) 1 55 56
296  
297 # should be endtag
298 1 $self->{_skipping_tag} = '';
299 }
300  
301 56 $self->{_buffer} .= $text;
302  
303 56 ▶ if ( !$HTML::Tagset::isPhraseMarkup{$tagname} or lc($tag) eq '/head' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $HTML::Tagset::isPhraseMarkup{$tagname} or lc $tag eq "/head") 36 20 56
Condition66%2/3
not $HTML::Tagset::isPhraseMarkup{$tagname} or lc $tag eq "/head"
l!l&&r!l&&!r
36020
MC/DC50%1/2
not $HTML::Tagset::isPhraseMarkup{$tagname} or lc $tag eq "/head"
not $HTML::Tagset::isPhraseMarkup{$tagname} lc $tag eq "/head"
Truth table66%2/3
not(A) or B
A= not $HTML::Tagset::isPhraseMarkup{$tagname}B= lc $tag eq "/head"
ABresult
1X1
011
000
304 36 $self->_flush_buffer();
305 }
306  
307 }
308  
309 60 ▶sub _matches_any_term {
Subroutine _matches_any_term: called CC=2 SCAR=7.0
310 60 my $self = shift;
311 60 my $buf = shift;
312  
313 60 ▶ $self->debug and carp "check '$buf' against $self->{_terms_regex}";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and carp(qq[check '${buf}' against $self->{"_terms_regex"}]) 0 60 60
314  
315 60 return $buf =~ m/$self->{_terms_regex}/;
316 }
317  
318 68 ▶sub _flush_buffer {
Subroutine _flush_buffer: called CC=9 SCAR=24.5
319 68 my ($self) = @_;
320  
321 68 ▶ if ( !length $self->{_buffer} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->{"_buffer"}) 8 60 68
322 8 return;
323 }
324  
325 # if we have a buffer limit defined and the current $buffer
326 # length exceeds that limit, deal with it immediately
327 # and don't highlight
328 60 ▶ if (( $self->{buffer_limit}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'buffer_limit'} and length $self->{'_buffer'} > $self->{'buffer_limit'} or not $self->{'_is_hiliting'}) { } 0 60 60
Condition33%2/6
$self->{"buffer_limit"} and length $self->{"_buffer"} > $self->{"buffer_limit"} or not $self->{"_is_hiliting"}
l!l&&r!l&&!r
0060
$self->{"buffer_limit"} and length $self->{"_buffer"} > $self->{"buffer_limit"}
!ll&&!rl&&r
0600
MC/DC0%0/3
$self->{"buffer_limit"} and length $self->{"_buffer"} > $self->{"buffer_limit"} or not $self->{"_is_hiliting"}
$self->{"buffer_limit"} length $self->{"_buffer"} > $self->{"buffer_limit"} not $self->{"_is_hiliting"}
Truth table20%1/5
A and B or not(C)
A= $self->{"buffer_limit"}B= length $self->{"_buffer"} > $self->{"buffer_limit"}C= not $self->{"_is_hiliting"}
ABCresult
11X1
0X11
1011
0X00
1000
329 && length( $self->{_buffer} ) > $self->{buffer_limit}
330 )
331 || ( !$self->{_is_hiliting} )
332 )
333 {
334 0 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 0 0 0
335 0 print { $self->{fh} } $self->{_buffer};
336 }
337 else {
338 0 $self->{output_buffer} .= $self->{_buffer};
339 }
340 }
341 else {
342  
343 # otherwise, call the hiliter on $buffer
344 # this is the main event
345  
346 60 ▶ $self->debug and carp "flushing buffer";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and carp("flushing buffer") 0 60 60
347  
348 60 my $hilited;
349  
350 60 ▶ if ( $self->_matches_any_term( $self->{_decoded_buffer} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_matches_any_term($self->{'_decoded_buffer'})) { } 46 14 60
351 46 $hilited = $self->apply_hiliting( $self->{_buffer} );
352 }
353 else {
354 14 $hilited = $self->{_buffer};
355 }
356  
357 # remove any markers we inserted to skip hiliting.
358 # doing it in 2 expressions instead of |'d together single expre
359 # is much faster, nytprof tells me.
360 60 $hilited =~ s/\002//g;
361 60 $hilited =~ s/\003//g;
362  
363 60 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 18 42 60
364 18 print { $self->{fh} } $hilited;
365 }
366 else {
367 42 $self->{output_buffer} .= $hilited;
368 }
369  
370 }
371  
372 60 $self->{_buffer} = '';
373 }
374  
375 59 ▶sub _handle_start_tag {
Subroutine _handle_start_tag: called CC=11 SCAR=27.4
376 59 my ($self, $parser, $tag, $tagname, $offset,
377 $length, $offset_end, $attr, $text
378 ) = @_;
379  
380 59 ▶ if ( $attr->{nohiliter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"nohiliter"}) 1 58 59
381  
382 # we want to not highlight this tag's contents
383  
384 1 $self->{_skipping_tag} = $tagname;
385  
386 #warn "skipping <$tag> with nohiliter\n";
387  
388 }
389  
390 # if we encounter an inline tag, add it to the buffer
391 # for later evaluation
392 # PhraseMarkup is closest to libxml2 'inline' definition
393 59 ▶ if ( $HTML::Tagset::isPhraseMarkup{$tagname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::isPhraseMarkup{$tagname}) 27 32 59
394  
395 27 my $tag_filter = $self->{tag_filter};
396 27 ▶ my $reassemble
Branch: 1 branch
BranchTrueFalseTotal
defined $tag_filter ? : 0 27 27
397 = defined $tag_filter
398 ? $tag_filter->(
399 $parser, $tag, $tagname, $offset, $length, $offset_end, $attr,
400 $text
401 )
402 : $text;
403  
404 27 ▶ warn "$open_comment adding :$reassemble: to buffer $close_comment"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug >= 3 0 27 27
405 if $self->debug >= 3;
406  
407 # add to the buffer for later evaluation as a potential match
408 27 $self->{_buffer} .= $reassemble;
409  
410 #warn "INLINEBUFFER:$buffer:INLINEBUFFER";
411  
412 27 return;
413  
414 }
415  
416 # flush the buffer before handling this tag.
417 32 $self->_flush_buffer();
418  
419 # now handle this tag
420 32 $self->_reset_state();
421  
422 # use reassemble to futz with attribute values or tagnames
423 # before printing them.
424 # otherwise, default to what we have in original HTML
425 #
426 # NOTE: this is where we could change HREF values, for example
427  
428 32 my $tag_filter = $self->{tag_filter};
429 32 ▶ my $reassemble
Branch: 1 branch
BranchTrueFalseTotal
defined $tag_filter ? : 0 32 32
430 = defined $tag_filter
431 ? $tag_filter->(
432 $parser, $tag, $tagname, $offset, $length, $offset_end, $attr, $text
433 )
434 : $text;
435  
436 32 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 9 23 32
437 9 print { $self->{fh} } $reassemble;
438 }
439 else {
440 23 $self->{_buffer} .= $reassemble;
441 }
442  
443 # if this is the opening <head> tag,
444 # add the <style> declarations for the hiliting
445 # this lets later <link css> tags in a doc
446 # override our local <style>
447  
448 32 ▶ if ( lc($tag) eq 'head' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $tag eq "head") 0 32 32
449 0 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 0 0 0
450 0 print { $self->{fh} } $self->{style_header}
451 0 ▶ if $self->{style_header};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"style_header"} 0 0 0
452 }
453 else {
454 $self->{_buffer} .= $self->{style_header}
455 0 ▶ if $self->{style_header};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"style_header"} 0 0 0
456 }
457 }
458  
459 }
460  
461 129 ▶sub _handle_text {
Subroutine _handle_text: called CC=7 SCAR=21.4
462 129 my ( $self, $parser, $decoded_text, $text, $offset, $length ) = @_;
463 129 my $text_filter = $self->{text_filter};
464 129 ▶ my $filtered
Branch: 1 branch
BranchTrueFalseTotal
defined $text_filter ? : 0 129 129
465 = defined $text_filter
466 ? $text_filter->( $parser, $decoded_text, $text, $offset, $length )
467 : $text;
468  
469 129 ▶ if ( !$self->{_is_hiliting} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_is_hiliting"}) 24 105 129
470  
471 # still in <head> section. handle and continue.
472 24 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 6 18 24
473 6 print { $self->{fh} } $filtered;
474 }
475 else {
476 18 $self->{output_buffer} .= $filtered;
477 }
478 24 return;
479 }
480  
481 # remember decoded to eval before calling hilite()
482 # this replaces the addtional 'tagless' algorithm
483 # that hilite() was doing
484 105 $self->{_decoded_buffer} .= $decoded_text;
485  
486 105 ▶ if ( $self->{_skipping_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_skipping_tag'}) { } 1 104 105
487  
488 # we don't want to highlight this text but we do want to output it later
489 # so delimit the text with the NULL character and skip that fragment
490 # in hilite()
491  
492 1 $self->{_buffer} .= "\002" . $filtered . "\003";
493 }
494 else {
495 104 $self->{_buffer} .= $filtered;
496 }
497  
498 105 ▶ if ( $self->debug >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug >= 3) 0 105 105
499 0 print { $self->{fh} } $open_comment
500 . "text :$text:\n"
501 . "filtered :$filtered:\n";
502 0 print { $self->{fh} } "Added text to buffer\n"
503 0 ▶ if $self->{_is_hiliting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_is_hiliting"} 0 0 0
504 0 print { $self->{fh} } "decoded :$decoded_text:\n"
505 . "Offset :$offset\n"
506 . "Length :$length\n"
507 . $close_comment;
508 }
509  
510 }
511  
512 0 ▶sub _check_count {
Subroutine _check_count: not called CC=4 SCAR=30.0
513 0 my $self = shift;
514  
515 # return total count for all keys
516 0 my $done;
517 0 for ( sort keys %{ $_[0] } ) {
518 0 $done += $_[0]->{$_};
519 0 ▶ if ( $self->debug >= 1 and $_[0]->{$_} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug >= 1 and $_[0]{$_} > 0) 0 0 0
Condition0%0/3
$self->debug >= 1 and $_[0]{$_} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug >= 1 and $_[0]{$_} > 0
$self->debug >= 1 $_[0]{$_} > 0
Truth table0%0/3
A and B
A= $self->debug >= 1B= $_[0]{$_} > 0
ABresult
0X0
100
111
520 0 print { $self->{fh} }
521 "$open_comment $_[0]->{$_} remaining to hilite for: $_ $close_comment";
522 }
523 }
524 0 return $done;
525 }
526  
527 0 ▶sub Queries {
Subroutine Queries: not called CC=1 SCAR=6.9
Pod documented
528 0 croak "Queries() is deprecated. Set the 'query' param in new()";
529 }
530  
531 40 ▶sub _reset_state {
Subroutine _reset_state: called CC=1 SCAR=0.0
532 40 my $self = shift;
533 40 $self->{_buffer} = '';
534 40 $self->{_skipping_tag} = '';
535 40 $self->{_decoded_buffer} = '';
536 40 return $self;
537 }
538  
539 8 ▶sub _reset_output_buffer {
Subroutine _reset_output_buffer: called CC=1 SCAR=0.0
540 8 my $self = shift;
541 8 $self->{output_buffer} = '';
542 8 return $self;
543 }
544  
545 16 ▶sub _handle_default {
Subroutine _handle_default: called CC=2 SCAR=6.9
546 16 my ( $self, $parser, $text ) = @_;
547 16 ▶ if ( $self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'print_stream'}) { } 2 14 16
548 2 print { $self->{fh} } $text;
549 }
550 else {
551 14 $self->{_buffer} .= $text;
552 }
553 }
554  
555 6 *Run = \&run;
556  
557 8 ▶sub run {
Subroutine run: called CC=9 SCAR=23.0
Pod documented
558 8 my $self = shift;
559 8 my $string = shift;
560 8 ▶ if ( !defined $string ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 8 8
561 0 croak "file or string required";
562 }
563 8 $self->{_is_hiliting} = 0;
564 8 $self->_reset_output_buffer;
565 8 $self->_reset_state;
566  
567 my $parser = HTML::Parser->new(
568 unbroken_text => 1,
569 api_version => 3,
570 text_h => [
571 129 ▶ sub { $self->_handle_text(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
572 'self,dtext,text,offset,length'
573 ],
574 start_h => [
575 75 ▶ sub { $self->_handle_tag(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
576 'self,tag,tagname,offset,length,offset_end,attr,text'
577 ],
578 end_h => [
579 64 ▶ sub { $self->_handle_tag(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
580 'self,tag,tagname,offset,length,offset_end,undef,text'
581 ],
582 8 ▶ default_h => [ sub { $self->_handle_default(@_) }, 'self,text' ]
Subroutine __ANON__: called CC=1 SCAR=0.0
583 );
584  
585 8 my $return;
586 8 ▶ if ( !ref($string) && -e $string ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $string and -e $string) { } 1 7 8
elsif ($string =~ m[^https?://]i) { } 0 7 7
elsif (ref $string eq 'SCALAR') { } 7 0 7
Condition66%2/3
not ref $string and -e $string
!ll&&!rl&&r
701
MC/DC50%1/2
not ref $string and -e $string
not ref $string -e $string
Truth table66%2/3
not(A) and B
A= not ref $stringB= -e $string
ABresult
0X0
100
111
587 1 $return = $parser->parse( to_utf8( Search::Tools->slurp($string) ) );
588 }
589 elsif ( $string =~ m/^https?:\/\//i ) {
590 0 $return = $parser->parse( to_utf8( $self->_get_url($string) ) );
591 }
592 elsif ( ref $string eq 'SCALAR' ) {
593 7 $return = $parser->parse( to_utf8($$string) );
594 }
595 else {
596 0 croak
597 "$string is neither a file nor a filehandle nor a scalar ref!\n";
598 }
599  
600 8 ▶ if ( !$return ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($return) 0 8 8
601 0 $self->{error} = $!; # TODO correct error msg?
602 }
603 8 ▶ if ( !$self->{print_stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'print_stream'}) { } 7 1 8
604 7 $self->{output_buffer} .= "\n";
605 }
606 else {
607 1 print { $self->{fh} }
608 "\n"; # does parser intentionlly chomp last line?
609 }
610  
611 # reset parser -- TODO need this since it goes out of scope here?
612 8 $parser->eof;
613  
614 8 ▶ return $self->{output_buffer} || $return;
Condition66%2/3
$self->{'output_buffer'} || $return
l!l&&r!l&&!r
710
MC/DC0%0/2
$self->{'output_buffer'} || $return
$self->{'output_buffer'} $return
Truth table66%2/3
A || B
A= $self->{'output_buffer'}B= $return
ABresult
1X1
011
000
615 }
616  
617 46 ▶sub apply_hiliting {
Subroutine apply_hiliting: called CC=2 SCAR=7.4
Pod documented
618 46 my $self = shift;
619 46 my $str = shift;
620 46 ▶ if ( !defined $str ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str) 0 46 46
621 0 croak "string required";
622 }
623 46 return $self->hiliter->light($str);
624 }
625  
626 0 ▶sub _get_url {
Subroutine _get_url: not called CC=5 SCAR=34.0
627  
628 0 require HTTP::Request;
629 0 require LWP::UserAgent;
630  
631 0 my $self = shift;
632 0 ▶ my $url = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
633  
634 0 my ( $http_ua, $request, $response, $content_type, $buf, $size );
635  
636 0 $http_ua = LWP::UserAgent->new;
637 0 $request = HTTP::Request->new( GET => $url );
638 0 $response = $http_ua->request($request);
639 0 ▶ $content_type ||= '';
Condition0%0/2
$content_type ||= ""
l!l
00
MC/DC0%0/1
$content_type ||= ""
$content_type
Truth table0%0/2
A
A= $content_type
Aresult
11
00
640 0 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
641 0 warn "Error: Couldn't get '$url': response code "
642 . $response->code . "\n";
643 0 return;
644 }
645  
646 0 ▶ if ( $response->headers_as_string =~ m/^Content-Type:\s*(.+)$/im ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->headers_as_string =~ /^Content-Type:\s*(.+)$/im) 0 0 0
647 0 $content_type = $1;
648 0 $content_type =~ s/^(.*?);.*$/$1/; # ignore possible charset value???
649 }
650  
651 0 $buf = $response->content;
652 0 $size = length($buf);
653  
654 0 $url = $response->base;
655 0 return ( $buf, $url, $response->last_modified, $size, $content_type );
656  
657 }
658  
659 6 1;
660  
661 __END__
↑ Summary