blib/lib/LUGS/Events/Parser/Filter.pm

filter: Statementstmt 100% 166 / 166 Branchbran 88.6% 39 / 44 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 97.1% 240 / 247 CC 44 SCAR 21.7
CC
44
Cov
97%
_rewrite_tags
21.0
_strip_html
7.0
_strip_text
5.0
CRAP 8.8
↑ Summary blib/lib/LUGS/Events/Parser.pm »
1 package LUGS::Events::Parser::Filter;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use boolean qw(true);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use HTML::Entities qw(decode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTML::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.07';
12  
13 5 my (@tags, @stack);
14  
15 sub _init_parser
16 {
17 5 ▶ my $self = shift;
Subroutine _init_parser: called CC=1 SCAR=0.0
18  
19 5 my $parser = HTML::Parser->new(
20 api_version => 3,
21 start_h => [ \&_start_tag, 'tagname,attr,attrseq' ],
22 text_h => [ \&_text_tag, 'text' ],
23 end_h => [ \&_end_tag, 'tagname' ],
24 );
25  
26 5 $parser->attr_encoded(true);
27  
28 5 return $parser;
29 }
30  
31 sub _parse_html
32 {
33 104 ▶ my $self = shift;
Subroutine _parse_html: called CC=2 SCAR=6.9
34 104 my ($chunk, $html) = @_;
35  
36 104 $self->{parser}->parse($chunk);
37  
38 104 undef @stack;
39  
40 104 ▶ return unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 73 31 104
41  
42 31 @$html = @tags;
43 31 undef @tags;
44 }
45  
46 sub _eof_parser
47 {
48 5 ▶ my $self = shift;
Subroutine _eof_parser: called CC=1 SCAR=0.0
49  
50 5 $self->{parser}->eof;
51 }
52  
53 sub _start_tag
54 {
55 49 ▶ my ($tagname, $attr, $attrseq) = @_;
Subroutine _start_tag: called CC=1 SCAR=0.0
56  
57 49 push @stack, { name => $tagname, attr => $attr, attrseq => $attrseq };
58 }
59  
60 sub _text_tag
61 {
62 116 ▶ my ($text) = @_;
Subroutine _text_tag: called CC=2 SCAR=6.9
63  
64 116 ▶ return unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 72 44 116
65  
66 44 $stack[-1]->{text} = $text;
67 }
68  
69 sub _end_tag
70 {
71 41 ▶ my ($tagname) = @_;
Subroutine _end_tag: called CC=3 SCAR=11.0
72  
73 41 ▶ return unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 3 38 41
74  
75 38 ▶ if ($stack[-1]->{name} eq $tagname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack[-1]{"name"} eq $tagname) 38 0 38
76 push @tags, {
77 $tagname => {
78 38 map { $_ => $stack[-1]->{$_} }
79 qw(text attr attrseq),
80 },
81 };
82 38 pop @stack;
83 }
84 }
85  
86 sub _rewrite_tags
87 {
88 13 ▶ my $self = shift;
Subroutine _rewrite_tags: called CC=21 SCAR=30.5
89 13 my ($fields) = @_;
90  
91 my $preserve_brackets = sub
92 {
93 33 ▶ my ($field, $subst) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
94 33 my %purge_tags = map { $_ => true } @{$self->{Purge_tags}};
95 33 ▶ return unless $purge_tags{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $purge_tags{$field} 23 10 33
96 10 my $pkg = __PACKAGE__;
97 10 $$subst =~ s/<(.+?)>/\[$pkg\]$1\[\/$pkg\]/g;
98 13 };
99  
100 13 foreach my $field (keys %{$fields->{_html}}) {
101 27 my %rewritten;
102 27 foreach my $html (@{$fields->{_html}->{$field}}) {
103 38 foreach my $tag (keys %$html) {
104 38 my @tagnames;
105 38 ▶ if (%{$html->{$tag}->{attr}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$html->{$tag}{'attr'};}) { } 37 1 38
106 37 foreach my $attr (keys %{$html->{$tag}->{attr}}) {
107 37 ▶ if (exists $self->{Tag_handlers}->{"$tag $attr"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Tag_handlers"}{"$tag $attr"}) 37 0 37
108 37 push @tagnames, "$tag $attr";
109 }
110 }
111 }
112 else {
113 1 ▶ if (exists $self->{Tag_handlers}->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Tag_handlers"}{$tag}) 1 0 1
114 1 push @tagnames, $tag;
115 }
116 }
117 38 foreach my $tagname (@tagnames) {
118 38 foreach my $handler (@{$self->{Tag_handlers}->{$tagname}}) {
119 57 ▶ if ($self->_field_rewrite($field, $handler)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_field_rewrite($field, $handler)) 32 25 57
120 32 ▶ unless (exists $rewritten{$tagname}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $rewritten{$tagname}) 23 9 32
121 23 $rewritten{$tagname} = true;
122 }
123 32 my $subst = $handler->{rewrite};
124 32 foreach my $subst_item ($self->_subst_data($html, $tag)) {
125 63 ▶ next unless defined $subst_item->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subst_item->[1] 1 62 63
126 62 my ($identifier, $replacement) = @$subst_item;
127 62 my $place_holder = uc $identifier;
128 62 $subst =~ s/\$$place_holder/$replacement/;
129 }
130 32 my $re = $self->_subst_pattern($html, $tag);
131 32 ▶ if (defined $html->{$tag}->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $html->{$tag}{'text'}) { } 31 1 32
132 31 $preserve_brackets->($field, \$subst);
133 31 $fields->{$field} =~ s{$re}{$subst};
134 }
135 else {
136 1 $fields->{$field} =~ s{$re}{$1};
137 }
138 }
139 }
140 }
141 }
142 }
143 27 foreach my $tagname (grep !$rewritten{$_}, keys %{$self->{Tag_handlers}}) {
144 54 foreach my $handler (@{$self->{Tag_handlers}->{$tagname}}) {
145 54 ▶ if ($self->_field_rewrite($field, $handler)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_field_rewrite($field, $handler)) 47 7 54
146 47 ▶ if ($tagname !~ /\b\s+?\b/
Branch: 1 branch
BranchTrueFalseTotal
if (not $tagname =~ /\b\s+?\b/ and $fields->{$field} =~ /<$tagname>/ and not $fields->{$field} =~ m[</$tagname>]) 2 45 47
Condition83%5/6
not $tagname =~ /\b\s+?\b/ and $fields->{$field} =~ /<$tagname>/ and not $fields->{$field} =~ m[</$tagname>]
!ll&&!rl&&r
4502
not $tagname =~ /\b\s+?\b/ and $fields->{$field} =~ /<$tagname>/
!ll&&!rl&&r
15302
MC/DC66%2/3
not $tagname =~ /\b\s+?\b/ and $fields->{$field} =~ /<$tagname>/ and not $fields->{$field} =~ m[</$tagname>]
not $tagname =~ /\b\s+?\b/ $fields->{$field} =~ /<$tagname>/ not $fields->{$field} =~ m[</$tagname>]
Truth table75%3/4
not(A) and B and not(C)
A= not $tagname =~ /\b\s+?\b/B= $fields->{$field} =~ /<$tagname>/C= not $fields->{$field} =~ m[</$tagname>]
ABCresult
0XX0
10X0
1100
1111
147 && $fields->{$field} =~ m{<$tagname>}
148 && $fields->{$field} !~ m{</$tagname>}
149 ) {
150 2 my $subst = $handler->{rewrite};
151 2 $preserve_brackets->($field, \$subst);
152 2 $fields->{$field} =~ s{<$tagname>}{$subst}g;
153 }
154 }
155 }
156 }
157 }
158 }
159  
160 sub _purge_tags
161 {
162 13 ▶ my $self = shift;
Subroutine _purge_tags: called CC=3 SCAR=11.0
163 13 my ($fields) = @_;
164  
165 13 my $pkg = __PACKAGE__;
166  
167 13 my %subst = (
168 "[$pkg]" => '<',
169 "[/$pkg]" => '>',
170 );
171  
172 13 ▶ foreach my $field (grep { !/^\_/ && exists $fields->{$_} } @{$self->{Purge_tags}}) {
Branch: 1 branch
BranchTrueFalseTotal
not /^\_/ and exists $fields->{$_} 3 0 3
173 3 $fields->{$field} = do {
174 3 local $_ = $fields->{$field};
175 3 s/<\/?\w+?>//g;
176 3 s/^\s+//;
177 3 s/\s+$//;
178 3 $_
179 };
180 3 $fields->{$field} =~ s/(\[\/?$pkg\])/$subst{$1}/g;
181 }
182 }
183  
184 sub _strip_html
185 {
186 31 ▶ my $self = shift;
Subroutine _strip_html: called CC=7 SCAR=19.5
187 31 my ($html) = @_;
188  
189 31 foreach my $html (@$html) {
190 38 foreach my $tag (keys %$html) {
191 38 foreach my $item (@{$self->{Strip_text}}) {
192 46 ▶ if (defined $html->{$tag}->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $html->{$tag}{"text"}) 45 1 46
193 45 $html->{$tag}->{text} =~ s/\Q$item\E//gi;
194 }
195 46 foreach my $attr (keys %{$html->{$tag}->{attr}}) {
196 45 ▶ if (defined $html->{$tag}->{attr}->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $html->{$tag}{"attr"}{$attr}) 45 0 45
197 45 $html->{$tag}->{attr}->{$attr} =~ s/\Q$item\E//gi;
198 }
199 }
200 }
201 }
202 }
203 }
204  
205 sub _strip_text
206 {
207 13 ▶ my $self = shift;
Subroutine _strip_text: called CC=5 SCAR=16.1
208 13 my ($fields) = @_;
209  
210 13 foreach my $field (grep !/^\_/, keys %$fields) {
211 92 foreach my $item (@{$self->{Strip_text}}) {
212 127 ▶ while ($fields->{$field} =~ /<.+?"[^"]*?(?=\Q$item\E[^"]*?".*?>)/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$field} =~ /<.+?"[^"]*?(?=\Q$item\E[^"]*?".*?>)/gi) 11 127 138
213 11 $fields->{$field} =~ s/\G\Q$item\E//i;
214 }
215 127 ▶ while ($fields->{$field} =~ /(?:^|>)[^<>]*?(?=\Q$item\E[^<>]*?(?:<|$))/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$field} =~ /(?:^|>)[^<>]*?(?=\Q$item\E[^<>]*?(?:<|$))/gi) 3 127 130
216 3 $fields->{$field} =~ s/\G\Q$item\E//i;
217 }
218 }
219 }
220 }
221  
222 sub _decode_entities
223 {
224 13 ▶ my $self = shift;
Subroutine _decode_entities: called CC=2 SCAR=6.9
225 13 my ($fields) = @_;
226  
227 13 foreach my $field (grep !/^\_/, keys %$fields) {
228 92 decode_entities($fields->{$field});
229 }
230 }
231  
232 sub _encode_safe
233 {
234 13 ▶ my $self = shift;
Subroutine _encode_safe: called CC=2 SCAR=6.9
235 13 my ($fields) = @_;
236  
237 my $encode = sub
238 {
239 43 ▶ my $f;
Subroutine __ANON__: called CC=2 SCAR=6.9
240 43 ▶ $f = eval { decode('UTF-8', $_[0], Encode::FB_CROAK) } or $f = $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$f = eval { do { &decode("UTF-8", $_[0], 1) } } or $f = $_[0] 18 25 43
241 43 return encode('UTF-8', $f);
242 13 };
243  
244 13 foreach my $field (grep exists $fields->{$_}, qw(title location responsible more)) {
245 43 $fields->{$field} = $encode->($fields->{$field});
246 }
247 }
248  
249 sub _field_rewrite
250 {
251 111 ▶ my $self = shift;
Subroutine _field_rewrite: called CC=2 SCAR=6.9
252 111 my ($field, $handler) = @_;
253  
254 111 my %rewrite = map { $_ => true } @{$handler->{fields}};
255  
256 111 ▶ return ($rewrite{$field} || $rewrite{'*'});
Condition100%3/3
$rewrite{$field} || $rewrite{'*'}
l!l&&r!l&&!r
314832
MC/DC100%2/2
$rewrite{$field} || $rewrite{'*'}
$rewrite{$field} $rewrite{'*'}
Truth table100%3/3
A || B
A= $rewrite{$field}B= $rewrite{'*'}
ABresult
1X1
011
000
257 }
258  
259 sub _subst_data
260 {
261 32 ▶ my $self = shift;
Subroutine _subst_data: called CC=1 SCAR=0.0
262 32 my ($html, $tag) = @_;
263  
264 return (map {
265 31 [ $_ => $html->{$tag}->{attr}->{$_} ]
266 32 } keys %{$html->{$tag}->{attr}}),
267 (map {
268 32 [ $_ => $html->{$tag}->{$_} ]
269 32 } grep /^(?:text)$/, keys %{$html->{$tag}});
270 }
271  
272 sub _subst_pattern
273 {
274 32 ▶ my $self = shift;
Subroutine _subst_pattern: called CC=3 SCAR=11.0
275 32 my ($html, $tag) = @_;
276  
277 32 ▶ if (@{$html->{$tag}->{attrseq}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$html->{$tag}{'attrseq'};}) { } 31 1 32
278 my $attr = join ' ',
279 map "${_}=\"$html->{$tag}->{attr}->{$_}\"",
280 31 @{$html->{$tag}->{attrseq}};
281 31 my $text = $html->{$tag}->{text};
282 31 ▶ return defined $text
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 30 1 31
283 ? qr{<$tag\s+?\Q$attr\E>$text</$tag>}
284 : qr{<$tag\s+?\Q$attr\E>(.*?)</$tag>};
285 }
286 else {
287 1 return qr{<$tag>(.*?)</$tag>};
288 }
289 }
290  
291 5 1;
↑ Summary blib/lib/LUGS/Events/Parser.pm »