Extract/HTML.pm

filter: Statementstmt 90.0% 100 / 111 Branchbran 52.0% 26 / 50 Conditioncond 45.4% 5 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 76.7% 152 / 198 CC 31 SCAR 16.2
CC
31
Cov
76%
__ANON__
9.8
__ANON__
6.3
__ANON__
6.0
CRAP 5.1
« Extract/YAML.pm ↑ Summary Extract/Role/File.pm »
1 package Locale::TextDomain::OO::Extract::HTML; ## no critic (TidyCode MainComplexity)
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MooX::Types::MooseLike::Base qw(ArrayRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 2 our $VERSION = '2.017';
10
11 2 extends qw(
12 Locale::TextDomain::OO::Extract::Base::RegexBasedExtractor
13 );
14 2 with qw(
15 Locale::TextDomain::OO::Extract::Role::File
16 );
17
18 has filter => (
19 is => 'rw',
20 isa => ArrayRef[Str],
21 lazy => 1,
22 1 ▶ default => sub {[ 'all' ]},
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 );
24
25 3 ▶sub _filtered_start_rule {
Subroutine _filtered_start_rule: called CC=2 SCAR=7.4
26 3 my $self = shift;
27
28 3 my %filter_of = map { $_ => 1 } @{ $self->filter };
29 9 ▶ my $list_if = sub {
Subroutine __ANON__: called CC=4 SCAR=17.1
30 9 my ( $key, @list ) = @_;
31 my $condition
32 = $filter_of{all} && ! $filter_of{"!$key"}
33 9 ▶ || $filter_of{$key};
Condition33%2/6
$filter_of{'all'} && !$filter_of{"!$key"} || $filter_of{$key}
l!l&&r!l&&!r
900
$filter_of{"all"} and not $filter_of{"!$key"}
!ll&&!rl&&r
009
MC/DC0%0/3
$filter_of{'all'} && !$filter_of{"!$key"} || $filter_of{$key}
$filter_of{"all"} not $filter_of{"!$key"} $filter_of{$key}
Truth table20%1/5
A and not(B) || C
A= $filter_of{"all"}B= not $filter_of{"!$key"}C= $filter_of{$key}
ABCresult
11X1
0X11
1011
0X00
1000
34 9 ▶ return $condition ? @list : ();
Branch: 1 branch
BranchTrueFalseTotal
$condition ? : 9 0 9
35 3 };
36 3 my $with_bracket = join "\n| ", (
37 $list_if->('Gettext', qr{ ["] [^"]*? \b __ \b [^"]*? ["] }xms,
38 qr{ ['] [^']*? \b __ \b [^']*? ['] }xms),
39 $list_if->('Gettext::Loc', qr{ ["] [^"]*? \b loc_ \b [^"]*? ["] }xms,
40 qr{ ['] [^']*? \b loc_ \b [^']*? ['] }xms),
41 $list_if->('Maketext', qr{ ["] [^"]*? \b loc \b [^"]*? ["] }xms,
42 qr{ ['] [^']*? \b loc \b [^']*? ['] }xms),
43 );
44 3 ▶ $with_bracket ||= '(?!)';
Condition50%1/2
$with_bracket ||= "(?!)"
l!l
30
MC/DC0%0/1
$with_bracket ||= "(?!)"
$with_bracket
Truth table50%1/2
A
A= $with_bracket
Aresult
11
00
45
46 3 return qr{
47 [<] [^>]*?
48 \b class \s* [=] \s*
49 (?: $with_bracket )
50 }xms;
51 }
52
53 ## no critic (ComplexRegexes)
54 2 my $text_rule = qr{ \s* ( [^<]+ ) }xms;
55
56 my $rules = [
57 # <input class="... loc_|__|loc ..." ... placeholder="text to extract" ... />
58 # <input class="... loc_|__|loc ..." ... title="text to extract" ... />
59 # <input class="... loc_|__|loc ..." ... value="text to extract" ... />
60 [
61 'begin',
62 32 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=22.8
63 32 my $content_ref = shift;
64
65 32 my $regex = qr{
66 [<] input \b
67 ( [^>]* )
68 />
69 }xms;
70 32 ▶ $content_ref
Branch: 1 branch
BranchTrueFalseTotal
$content_ref or return $regex 32 0 32
71 or return $regex;
72 my ( $full_match, $inner )
73 32 ▶ = ${$content_ref} =~ m{ \G ( $regex ) }xms
Branch: 1 branch
BranchTrueFalseTotal
() = ${$content_ref;} =~ / \G ( $regex ) /msx or return 18 14 32
74 or return;
75
76 18 ▶ my @match = (
Branch: 3 branches
BranchTrueFalseTotal
$inner =~ / \b type \s* [=] \s* ["] (?: submit | reset | button ) ["] /msx || $inner =~ / \b type \s* [=] \s* ['] (?: submit | reset | button ) ['] /msx ? : 9 9 18
$inner =~ / \b value \s* [=] \s* ["] ( [^"]+ ) ["] /msx ? : 9 0 9
$inner =~ / \b value \s* [=] \s* ['] ( [^']+ ) ['] /msx ? : 0 0 0
Condition66%2/3
$inner =~ / \b type \s* [=] \s* ["] (?: submit | reset | button ) ["] /msx or $inner =~ / \b type \s* [=] \s* ['] (?: submit | reset | button ) ['] /msx
l!l&&r!l&&!r
909
MC/DC50%1/2
$inner =~ / \b type \s* [=] \s* ["] (?: submit | reset | button ) ["] /msx or $inner =~ / \b type \s* [=] \s* ['] (?: submit | reset | button ) ['] /msx
$inner =~ / \b type \s* [=] \s* ["] (?: submit | reset | button ) ["] /msx $inner =~ / \b type \s* [=] \s* ['] (?: submit | reset | button ) ['] /msx
Truth table66%2/3
A or B
A= $inner =~ / \b type \s* [=] \s* ["] (?: submit | reset | button ) ["] /msxB= $inner =~ / \b type \s* [=] \s* ['] (?: submit | reset | button ) ['] /msx
ABresult
1X1
011
000
77 $inner =~ m{ \b placeholder \s* [=] \s* ["] ( [^"]+ ) ["] }xms,
78 $inner =~ m{ \b placeholder \s* [=] \s* ['] ( [^']+ ) ['] }xms,
79 $inner =~ m{ \b title \s* [=] \s* ["] ( [^"]+ ) ["] }xms,
80 $inner =~ m{ \b title \s* [=] \s* ['] ( [^']+ ) ['] }xms,
81 (
82 $inner =~ m{ \b type \s* [=] \s* ["] (?: submit | reset | button ) ["] }xms
83 || $inner =~ m{ \b type \s* [=] \s* ['] (?: submit | reset | button ) ['] }xms
84 )
85 ? (
86 $inner =~ m{ \b value \s* [=] \s* ["] ( [^"]+ ) ["] }xms
87 ? $1
88 : $inner =~ m{ \b value \s* [=] \s* ['] ( [^']+ ) ['] }xms
89 ? $1
90 : ()
91 )
92 : (),
93 );
94 @match
95 18 ▶ and return +( $full_match, @match );
Branch: 1 branch
BranchTrueFalseTotal
@match and return $full_match, @match 18 0 18
96
97 0 return;
98 },
99 'end',
100 ],
101 'or',
102 # <textarea
103 # class="... loc_|__|loc ..."
104 # placeholder="text to extract"
105 # ...
106 # ></textarea>
107 [
108 'begin',
109 14 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=18.4
110 14 my $content_ref = shift;
111
112 14 my $regex = qr{
113 [<] textarea \b
114 ( [^>]* )
115 [>]
116 }xms;
117 14 ▶ $content_ref
Branch: 1 branch
BranchTrueFalseTotal
$content_ref or return $regex 14 0 14
118 or return $regex;
119 my ( $full_match, $inner )
120 14 ▶ = ${$content_ref} =~ m{ \G ( $regex ) }xms
Branch: 1 branch
BranchTrueFalseTotal
() = ${$content_ref;} =~ / \G ( $regex ) /msx or return 3 11 14
121 or return;
122 3 ▶ $inner =~ m{ \b placeholder \s* [=] \s* ["] ( [^"]+ ) ["] }xms
Branch: 1 branch
BranchTrueFalseTotal
$inner =~ / \b placeholder \s* [=] \s* ["] ( [^"]+ ) ["] /msx and return $full_match, $1 3 0 3
123 and return +( $full_match, $1 );
124 0 ▶ $inner =~ m{ \b placeholder \s* [=] \s* ['] ( [^']+ ) ['] }xms
Branch: 1 branch
BranchTrueFalseTotal
$inner =~ / \b placeholder \s* [=] \s* ['] ( [^']+ ) ['] /msx and return $full_match, $1 0 0 0
125 and return +( $full_match, $1 );
126
127 0 return;
128 },
129 'end',
130 ],
131 'or',
132 # <img class="... loc_|__|loc ..." ... alt="text to extract" ... />
133 [
134 'begin',
135 11 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
136 11 my $content_ref = shift;
137
138 11 my $regex = qr{
139 [<] img \b
140 ( [^>]* )
141 />
142 }xms;
143 11 ▶ $content_ref
Branch: 1 branch
BranchTrueFalseTotal
$content_ref or return $regex 11 0 11
144 or return $regex;
145 my ( $full_match, $inner )
146 11 ▶ = ${$content_ref} =~ m{ \G ( $regex ) }xms
Branch: 1 branch
BranchTrueFalseTotal
() = ${$content_ref;} =~ / \G ( $regex ) /msx or return 0 11 11
147 or return;
148
149 0 my @match = (
150 $inner =~ m{ \b alt \s* [=] \s* ["] ( [^"]+ ) ["] }xms,
151 $inner =~ m{ \b alt \s* [=] \s* ['] ( [^']+ ) ['] }xms,
152 );
153 @match
154 0 ▶ and return +( $full_match, @match );
Branch: 1 branch
BranchTrueFalseTotal
@match and return $full_match, @match 0 0 0
155
156 0 return;
157 },
158 'end',
159 ],
160 'or',
161 # <a class="... loc_|__|loc ..." ... title="text to extract" ... >text_to_extract</a>
162 [
163 'begin',
164 11 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
165 11 my $content_ref = shift;
166
167 11 my $regex = qr{
168 [<] [a] \b
169 ( [^>]* )
170 [>]
171 ( [^<]* )
172 }xms;
173 11 ▶ $content_ref
Branch: 1 branch
BranchTrueFalseTotal
$content_ref or return $regex 11 0 11
174 or return $regex;
175 my ( $full_match, $inner, $text )
176 11 ▶ = ${$content_ref} =~ m{ \G ( $regex ) }xms
Branch: 1 branch
BranchTrueFalseTotal
() = ${$content_ref;} =~ / \G ( $regex ) /msx or return 8 3 11
177 or return;
178
179 8 my @match = (
180 $inner =~ m{ \b title \s* [=] \s* ["] ( [^"]+ ) ["] }xms,
181 $inner =~ m{ \b title \s* [=] \s* ['] ( [^']+ ) ['] }xms,
182 );
183 @match
184 8 ▶ and return +( $full_match, $text, @match );
Branch: 1 branch
BranchTrueFalseTotal
@match and return $full_match, $text, @match 0 8 8
185
186 8 return;
187 },
188 'end',
189 ],
190 'or',
191 # <button class="... loc_|__|loc ..." ... title="text to extract" ... >text_to_extract</button>
192 [
193 'begin',
194 11 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
195 11 my $content_ref = shift;
196
197 11 my $regex = qr{
198 [<] button \b
199 ( [^>]* )
200 [>]
201 ( [^<]* )
202 }xms;
203 11 ▶ $content_ref
Branch: 1 branch
BranchTrueFalseTotal
$content_ref or return $regex 11 0 11
204 or return $regex;
205 my ( $full_match, $inner, $text )
206 11 ▶ = ${$content_ref} =~ m{ \G ( $regex ) }xms
Branch: 1 branch
BranchTrueFalseTotal
() = ${$content_ref;} =~ / \G ( $regex ) /msx or return 0 11 11
207 or return;
208
209 0 my @match = (
210 $inner =~ m{ \b title \s* [=] \s* ["] ( [^"]+ ) ["] }xms,
211 $inner =~ m{ \b title \s* [=] \s* ['] ( [^']+ ) ['] }xms,
212 );
213 @match
214 0 ▶ and return +( $full_match, $text, @match );
Branch: 1 branch
BranchTrueFalseTotal
@match and return $full_match, $text, @match 0 0 0
215
216 0 return;
217 },
218 'end',
219 ],
220 'or',
221 # < class="... loc_|__|loc ..." ... title="text to extract" ... >text_to_extract<
222 [
223 'begin',
224 11 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
225 11 my $content_ref = shift;
226
227 11 my $regex = qr{
228 [<] \w+ \b
229 ( [^>]* )
230 [>]
231 ( [^<]* )
232 }xms;
233 11 ▶ $content_ref
Branch: 1 branch
BranchTrueFalseTotal
$content_ref or return $regex 11 0 11
234 or return $regex;
235 my ( $full_match, $inner, $text )
236 11 ▶ = ${$content_ref} =~ m{ \G ( $regex ) }xms
Branch: 1 branch
BranchTrueFalseTotal
() = ${$content_ref;} =~ / \G ( $regex ) /msx or return 11 0 11
237 or return;
238
239
240 11 my @match = (
241 $inner =~ m{ \b title \s* [=] \s* ["] ( [^"]+ ) ["] }xms,
242 $inner =~ m{ \b title \s* [=] \s* ['] ( [^']+ ) ['] }xms,
243 $text,
244 );
245 @match
246 11 ▶ and return +( $full_match, @match );
Branch: 1 branch
BranchTrueFalseTotal
@match and return $full_match, @match 11 0 11
247
248 0 return;
249 },
250 2 'end',
251 ],
252 'or',
253 # <... class="... loc_|__|loc ..." ... >text to extract<
254 [
255 'begin',
256 qr{
257 [<] [^>]*?
258 \b class \s* [=] \s* ["] [^"]*?
259 \b (?: loc_ | __ | loc ) \b
260 [^"]*? ["]
261 [^>]* [>]
262 }xms,
263 'and',
264 $text_rule,
265 'end',
266 ],
267 'or',
268 # <... class='... loc_|__|loc ...' ... >text to extract<
269 [
270 'begin',
271 qr{
272 [<] [^>]*?
273 \b class \s* [=] \s* ['] [^']*?
274 \b (?: loc_ | __ | loc ) \b
275 [^']*? [']
276 [^>]* [>]
277 }xms,
278 'and',
279 $text_rule,
280 'end',
281 ],
282 ];
283 ## use critic (ComplexRegexes)
284
285 # remove code between <!-- -->
286 3 ▶sub preprocess {
Subroutine preprocess: called CC=1 SCAR=0.0
Pod documented
287 3 my $self = shift;
288
289 3 my $content_ref = $self->content_ref;
290
291 3 ${$content_ref} =~ s{ \r? \n }{\n}xmsg;
292 3 ${$content_ref} =~ s{ <!-- ( .*? ) --> }{
293 0 join q{}, $1 =~ m{ ( \n ) }xmsg
294 }xmsge;
295
296 3 return $self;
297 }
298
299 32 ▶sub stack_item_mapping {
Subroutine stack_item_mapping: called CC=3 SCAR=11.0
Pod documented
300 32 my $self = shift;
301
302 32 my $match = $_->{match};
303 32 ▶ @{$match}
Branch: 1 branch
BranchTrueFalseTotal
@{$match;} or return 32 0 32
304 or return;
305
306 32 ▶ while ( my $string = shift @{$match} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $string = shift @{$match;}) 32 32 64
307 32 $string =~ s{ \s+ \z }{}xms;
308 32 $string =~ s{ \A \s+ }{}xms;
309 32 my ( $msgctxt, $msgid )
310 = $string =~ m{ \A (?: ( .*? ) \s* \Q{CONTEXT_SEPARATOR}\E )? \s* ( .* ) \z }xms;
311 $self->add_message({
312 32 reference => ( sprintf '%s:%s', $self->filename, $_->{line_number} ),
313 msgctxt => $msgctxt,
314 msgid => $msgid,
315 });
316 }
317
318 32 return;
319 }
320
321 3 ▶sub extract {
Subroutine extract: called CC=2 SCAR=6.9
Pod documented
322 3 my $self = shift;
323
324 3 $self->start_rule( $self->_filtered_start_rule );
325 3 $self->rules($rules);
326 3 $self->preprocess;
327 3 $self->SUPER::extract;
328 3 for ( @{ $self->stack } ) {
329 32 $self->stack_item_mapping;
330 }
331
332 3 return $self;
333 }
334
335 2 __PACKAGE__->meta->make_immutable;
336
337 2 1;
338
339 __END__
« Extract/YAML.pm ↑ Summary Extract/Role/File.pm »