lib/Serge/Engine/Plugin/metaparser.pm

filter: Statementstmt 5.1% 7 / 135 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 9.5% 2 / 21 Podpod 0.0% 0 / 17 totaltotal 2.9% 9 / 309 SCAR 78.7
CC
53
Cov
3%
_flush
72.0
process_line
56.0
find_keyvalue
42.0
CRAP 2629.9
« lib/Serge/Engine/Plugin/limit_languages.pm ↑ Summary lib/Serge/Importer.pm »
1 package Serge::Engine::Plugin::metaparser;
2 1 ▶use parent Serge::Engine::Plugin::Base::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub name {
7 0 ▶ return 'Metaparser plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
8 }
9  
10 sub init {
11 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
12  
13 0 $self->SUPER::init(@_);
14  
15 0 $self->merge_schema({
16 hint => 'STRING',
17 key => 'STRING',
18 value => 'STRING',
19 keyvalue => 'STRING',
20 localize => 'STRING',
21 context => 'STRING',
22 reset => 'STRING',
23 skip => 'STRING',
24 multiline => 'STRING',
25  
26 unescape => {'' => 'LIST',
27 '*' => 'ARRAY'
28 },
29  
30 escape => {'' => 'LIST',
31 '*' => 'ARRAY'
32 },
33 });
34 }
35  
36 sub validate_data {
37 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=4 SCAR=30.0
Pod undocumented
38  
39 0 $self->SUPER::validate_data;
40  
41 0 ▶ if (!(defined $self->{data}->{keyvalue} || defined $self->{data}->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"data"}{"keyvalue"} or defined $self->{"data"}{"value"}) 0 0 0
Condition0%0/3
defined $self->{"data"}{"keyvalue"} or defined $self->{"data"}{"value"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $self->{"data"}{"keyvalue"} or defined $self->{"data"}{"value"}
defined $self->{"data"}{"keyvalue"} defined $self->{"data"}{"value"}
Truth table0%0/3
A or B
A= defined $self->{"data"}{"keyvalue"}B= defined $self->{"data"}{"value"}
ABresult
1X1
011
000
42 0 die 'Neither "keyvalue" nor "value" is defined';
43 }
44  
45 0 ▶ die '"localize" is not defined' unless defined $self->{data}->{localize};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{"localize"} 0 0 0
46 }
47  
48 sub parse {
49 0 ▶ my ($self, $textref, $callbackref, $lang) = @_;
Subroutine parse: not called CC=5 SCAR=34.0
Pod undocumented
50  
51 0 ▶ die 'callbackref not specified' unless $callbackref;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbackref 0 0 0
52  
53 0 $self->_reset;
54  
55 0 $self->{callbackref} = $callbackref;
56 0 $self->{lang} = $lang;
57  
58 # make a copy of the string as we will change it
59 0 my $source_text = $$textref;
60  
61 0 $self->merge_multiline_strings(\$source_text);
62  
63 0 $self->preprocess(\$source_text);
64  
65 0 my @output;
66  
67 0 foreach my $line (split(/\n/, $source_text)) {
68 0 $self->{line} = $line;
69 0 $self->process_line($line);
70 0 ▶ if (defined $self->{key} && defined $self->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"key"} and defined $self->{"value"}) 0 0 0
Condition0%0/3
defined $self->{"key"} and defined $self->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"key"} and defined $self->{"value"}
defined $self->{"key"} defined $self->{"value"}
Truth table0%0/3
A and B
A= defined $self->{"key"}B= defined $self->{"value"}
ABresult
0X0
100
111
71 0 $self->_flush;
72 0 $self->_reset;
73 }
74 0 push @output, $self->{line};
75 }
76  
77 0 my $result = join("\n", @output);
78  
79 0 $self->postprocess(\$result);
80  
81 0 return $result;
82 }
83  
84 sub _reset {
85 0 ▶ my ($self) = @_;
Subroutine _reset: not called CC=2 SCAR=17.9
86  
87 0 $self->{skip} = undef;
88 0 $self->{hints} = [];
89 0 $self->{context} = undef;
90 0 $self->{key} = undef;
91 0 $self->{value} = undef;
92  
93 0 ▶ print "reset\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
94 }
95  
96 sub preprocess {
97 0 ▶ my ($self, $strref) = @_;
Subroutine preprocess: not called CC=1 SCAR=6.9
Pod undocumented
98 # do nothing
99 }
100  
101 sub postprocess {
102 0 ▶ my ($self, $strref) = @_;
Subroutine postprocess: not called CC=1 SCAR=6.9
Pod undocumented
103 # do nothing
104 }
105  
106 sub merge_multiline_strings {
107 0 ▶ my ($self, $strref) = @_;
Subroutine merge_multiline_strings: not called CC=2 SCAR=17.9
Pod undocumented
108  
109 0 my $re = $self->{data}->{multiline};
110 0 ▶ if ($re ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($re ne "") 0 0 0
111 0 $$strref =~ s/$re//sg;
112 }
113 }
114  
115 sub process_line {
116 0 ▶ my ($self, $line) = @_;
Subroutine process_line: not called CC=7 SCAR=40.3
Pod undocumented
117  
118 0 ▶ $self->find_context ||
Branch: 1 branch
BranchTrueFalseTotal
$self->find_context or $self->find_hint or $self->find_reset or $self->find_skip or $self->find_key or $self->find_value or $self->find_keyvalue 0 0 0
Condition0%0/15
$self->find_context or $self->find_hint or $self->find_reset or $self->find_skip or $self->find_key or $self->find_value
l!l&&r!l&&!r
000
$self->find_context or $self->find_hint or $self->find_reset or $self->find_skip or $self->find_key
l!l&&r!l&&!r
000
$self->find_context or $self->find_hint or $self->find_reset or $self->find_skip
l!l&&r!l&&!r
000
$self->find_context or $self->find_hint or $self->find_reset
l!l&&r!l&&!r
000
$self->find_context or $self->find_hint
l!l&&r!l&&!r
000
MC/DC0%0/6
$self->find_context or $self->find_hint or $self->find_reset or $self->find_skip or $self->find_key or $self->find_value
$self->find_context $self->find_hint $self->find_reset $self->find_skip $self->find_key $self->find_value
Truth table0%0/7
A or B or C or D or E or F
A= $self->find_contextB= $self->find_hintC= $self->find_resetD= $self->find_skipE= $self->find_keyF= $self->find_value
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
119 $self->find_hint ||
120 $self->find_reset ||
121 $self->find_skip ||
122 $self->find_key ||
123 $self->find_value ||
124 $self->find_keyvalue;
125 }
126  
127 sub find_context {
128 0 ▶ my ($self) = @_;
Subroutine find_context: not called CC=5 SCAR=34.0
Pod undocumented
129  
130 0 ▶ if (($self->{data}->{context} ne '') && ($self->{line} =~ m/$self->{data}->{context}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"context"} ne "" and $self->{"line"} =~ /$self->{"data"}{"context"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"context"} ne "" and $self->{"line"} =~ /$self->{"data"}{"context"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"context"} ne "" and $self->{"line"} =~ /$self->{"data"}{"context"}/
$self->{"data"}{"context"} ne "" $self->{"line"} =~ /$self->{"data"}{"context"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"context"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"context"}/
ABresult
0X0
100
111
131 0 ▶ die "'context' pattern returned empty value" unless $1 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $1 ne "" 0 0 0
132 0 $self->{context} = $1;
133 0 ▶ print "context: '$1'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
134 0 return 1; # skip processing
135 }
136  
137 0 return undef; # continue processing
138 }
139  
140 sub find_hint {
141 0 ▶ my ($self) = @_;
Subroutine find_hint: not called CC=5 SCAR=34.0
Pod undocumented
142  
143 0 ▶ if (($self->{data}->{hint} ne '') && ($self->{line} =~ m/$self->{data}->{hint}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"hint"} ne "" and $self->{"line"} =~ /$self->{"data"}{"hint"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"hint"} ne "" and $self->{"line"} =~ /$self->{"data"}{"hint"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"hint"} ne "" and $self->{"line"} =~ /$self->{"data"}{"hint"}/
$self->{"data"}{"hint"} ne "" $self->{"line"} =~ /$self->{"data"}{"hint"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"hint"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"hint"}/
ABresult
0X0
100
111
144 0 ▶ die "'hint' pattern returned empty value" unless $1 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $1 ne "" 0 0 0
145 0 push @{$self->{hints}}, $1;
146 0 ▶ print "hint: '$1'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
147 0 return 1; # skip processing
148 }
149  
150 0 return undef; # continue processing
151 }
152  
153 sub find_reset {
154 0 ▶ my ($self) = @_;
Subroutine find_reset: not called CC=3 SCAR=24.8
Pod undocumented
155  
156 0 ▶ if (($self->{data}->{reset} ne '') && ($self->{line} =~ m/$self->{data}->{reset}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"reset"} ne "" and $self->{"line"} =~ /$self->{"data"}{"reset"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"reset"} ne "" and $self->{"line"} =~ /$self->{"data"}{"reset"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"reset"} ne "" and $self->{"line"} =~ /$self->{"data"}{"reset"}/
$self->{"data"}{"reset"} ne "" $self->{"line"} =~ /$self->{"data"}{"reset"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"reset"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"reset"}/
ABresult
0X0
100
111
157 0 $self->_reset;
158 0 return 1; # skip processing
159 }
160  
161 0 return undef; # continue processing
162 }
163  
164 sub find_skip {
165 0 ▶ my ($self) = @_;
Subroutine find_skip: not called CC=4 SCAR=30.0
Pod undocumented
166  
167 0 ▶ if (($self->{data}->{skip} ne '') && ($self->{line} =~ m/$self->{data}->{skip}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"skip"} ne "" and $self->{"line"} =~ /$self->{"data"}{"skip"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"skip"} ne "" and $self->{"line"} =~ /$self->{"data"}{"skip"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"skip"} ne "" and $self->{"line"} =~ /$self->{"data"}{"skip"}/
$self->{"data"}{"skip"} ne "" $self->{"line"} =~ /$self->{"data"}{"skip"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"skip"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"skip"}/
ABresult
0X0
100
111
168 0 $self->{skip} = 1;
169 0 ▶ print "skip\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
170 0 return 1; # skip processing
171 }
172  
173 0 return undef; # continue processing
174 }
175  
176 sub find_key {
177 0 ▶ my ($self) = @_;
Subroutine find_key: not called CC=5 SCAR=34.0
Pod undocumented
178  
179 0 ▶ if (($self->{data}->{key} ne '') && ($self->{line} =~ m/$self->{data}->{key}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"key"} ne "" and $self->{"line"} =~ /$self->{"data"}{"key"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"key"} ne "" and $self->{"line"} =~ /$self->{"data"}{"key"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"key"} ne "" and $self->{"line"} =~ /$self->{"data"}{"key"}/
$self->{"data"}{"key"} ne "" $self->{"line"} =~ /$self->{"data"}{"key"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"key"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"key"}/
ABresult
0X0
100
111
180 0 ▶ die "'key' pattern returned empty value" unless $1 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $1 ne "" 0 0 0
181 0 $self->{key} = $1;
182 0 ▶ print "key: '$1'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
183 0 return 1; # skip processing
184 }
185  
186 0 return undef; # continue processing
187 }
188  
189 sub find_value {
190 0 ▶ my ($self) = @_;
Subroutine find_value: not called CC=5 SCAR=34.0
Pod undocumented
191  
192 0 ▶ if (($self->{data}->{value} ne '') && ($self->{line} =~ m/$self->{data}->{value}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"value"} ne "" and $self->{"line"} =~ /$self->{"data"}{"value"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"value"} ne "" and $self->{"line"} =~ /$self->{"data"}{"value"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"value"} ne "" and $self->{"line"} =~ /$self->{"data"}{"value"}/
$self->{"data"}{"value"} ne "" $self->{"line"} =~ /$self->{"data"}{"value"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"value"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"value"}/
ABresult
0X0
100
111
193 0 ▶ die "'value' pattern returned empty value" unless $1 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $1 ne "" 0 0 0
194 0 $self->{value} = $1;
195 0 ▶ print "value: '$1'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
196 0 return 1; # skip processing
197 }
198  
199 0 return undef; # continue processing
200 }
201  
202 sub find_keyvalue {
203 0 ▶ my ($self) = @_;
Subroutine find_keyvalue: not called CC=6 SCAR=37.4
Pod undocumented
204  
205 0 ▶ if (($self->{data}->{keyvalue} ne '') && ($self->{line} =~ m/$self->{data}->{keyvalue}/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"keyvalue"} ne "" and $self->{"line"} =~ /$self->{"data"}{"keyvalue"}/) 0 0 0
Condition0%0/3
$self->{"data"}{"keyvalue"} ne "" and $self->{"line"} =~ /$self->{"data"}{"keyvalue"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"data"}{"keyvalue"} ne "" and $self->{"line"} =~ /$self->{"data"}{"keyvalue"}/
$self->{"data"}{"keyvalue"} ne "" $self->{"line"} =~ /$self->{"data"}{"keyvalue"}/
Truth table0%0/3
A and B
A= $self->{"data"}{"keyvalue"} ne ""B= $self->{"line"} =~ /$self->{"data"}{"keyvalue"}/
ABresult
0X0
100
111
206 0 ▶ die "'keyvalue' pattern returned empty key" unless $1 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $1 ne "" 0 0 0
207 0 ▶ die "'keyvalue' pattern returned empty string value" unless $2 ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $2 ne "" 0 0 0
208 0 $self->{key} = $1;
209 0 $self->{value} = $2;
210 0 ▶ print "keyvalue: '$self->{key}'=>'$self->{value}'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
211 0 return 1; # skip processing
212 }
213  
214 0 return undef; # continue processing
215 }
216  
217 sub _flush {
218 0 ▶ my ($self) = @_;
Subroutine _flush: not called CC=8 SCAR=42.8
219  
220 0 ▶ print "_flush\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
221  
222 0 ▶ return if $self->{skip};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"skip"} 0 0 0
223  
224 # add key as the first hint line if it differs from the source value
225 0 ▶ unshift @{$self->{hints}}, $self->{key} if $self->{key} ne $self->{value};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"key"} ne $self->{"value"} 0 0 0
226  
227 # convert hints array to a multi-line string
228 0 my $hint = join("\n", @{$self->{hints}});
229  
230 0 my $translated_str;
231  
232 0 $self->{value} = $self->unescape($self->{value});
233  
234 0 ▶ if ($self->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"value"}) 0 0 0
235 0 $translated_str = &{$self->{callbackref}}(
236 $self->{value},
237 $self->{context},
238 $hint,
239 undef,
240 $self->{lang},
241 $self->{key}
242 0 );
243 }
244  
245 0 ▶ if ($self->{lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lang"}) 0 0 0
246 0 my $re = $self->{data}->{localize};
247 0 $translated_str = $self->escape($translated_str);
248 0 ▶ if ($self->{line} =~ m/$re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'line'} =~ /$re/) { } 0 0 0
249 0 ▶ die "'localize' pattern returned empty \$2 capture group" unless defined $2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $2 0 0 0
250 } else {
251 0 die "'localize' pattern didn't match anything";
252 }
253 0 $self->{line} =~ s/$re/$1$translated_str$3/;
254 }
255 }
256  
257 sub unescape {
258 0 ▶ my ($self, $source) = @_;
Subroutine unescape: not called CC=3 SCAR=24.8
Pod undocumented
259  
260 0 my $rules = $self->{data}->{unescape};
261 0 foreach my $rule (@{$self->{data}->{unescape}}) {
262 0 my ($from, $to, $modifiers) = @$rule;
263  
264 0 my $eval_line = "\$source =~ s/$from/$to/$modifiers;";
265 0 eval($eval_line);
266 0 ▶ die "eval() failed on: '$eval_line'\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
267 }
268  
269 0 return $source;
270 }
271  
272 sub escape {
273 0 ▶ my ($self, $translation) = @_;
Subroutine escape: not called CC=3 SCAR=24.8
Pod undocumented
274  
275 0 foreach my $rule (@{$self->{data}->{escape}}) {
276 0 my ($from, $to, $modifiers) = @$rule;
277 0 my $eval_line = "\$translation =~ s/$from/$to/$modifiers;";
278 0 eval($eval_line);
279 0 ▶ die "eval() failed on: '$eval_line'\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
280 }
281  
282 0 return $translation;
283 }
284  
285 1 1;
« lib/Serge/Engine/Plugin/limit_languages.pm ↑ Summary lib/Serge/Importer.pm »