lib/Serge/Engine/Plugin/parse_pot.pm

filter: Statementstmt 12.5% 23 / 184 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 30.7% 4 / 13 Podpod 0.0% 0 / 5 totaltotal 9.3% 27 / 289 SCAR 72.3
CC
42
Cov
9%
parse
1260.0
_generate_mo
42.0
validate_data
6.0
CRAP 1379.5
« lib/Serge/DB/Cached.pm ↑ Summary lib/Serge/DB.pm »
1 package Serge::Engine::Plugin::parse_pot;
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 1 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 use Serge::Util qw(glue_plural_string split_plural_string
9 1 ▶ generate_key po_serialize_msgstr unescape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $MODE_DEFAULT = 0;
12 1 my $MODE_MSGID = 1;
13 1 my $MODE_MSGID_PLURAL = 2;
14 1 my $MODE_MSGCTXT = 3;
15 1 my $MODE_MSGSTR = 4;
16  
17 1 my $MO_PLURAL_SEPARATOR = chr(0);
18 1 my $MO_CTX_SEPARATOR = chr(4);
19 1 my $MO_MAGIC = 0x950412de;
20 1 my $MO_FORMAT = 0;
21 1 my $MO_HEADER_SIZE = 28;
22  
23 sub name {
24 0 ▶ return 'Gettext .PO/.POT parser plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
25 }
26  
27 sub init {
28 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
29  
30 0 $self->SUPER::init(@_);
31  
32 0 $self->merge_schema({
33 output_mo_path => 'STRING',
34 });
35 }
36  
37 sub validate_data {
38 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=2 SCAR=17.9
Pod undocumented
39  
40 0 $self->SUPER::validate_data;
41  
42 0 ▶ if (defined $self->{data}->{output_mo_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"data"}{"output_mo_path"}) 0 0 0
43 0 $self->{strings} = {};
44 0 $self->add('after_save_localized_file', \&after_save_localized_file);
45 0 $self->_add_string([''], '', ["Content-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: 8bit\n"]);
46 }
47 }
48  
49 sub parse {
50 0 ▶ my ($self, $textref, $callbackref, $lang) = @_;
Subroutine parse: not called CC=35 SCAR=71.4
Pod undocumented
51  
52 0 ▶ die 'callbackref not specified' unless $callbackref;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbackref 0 0 0
53  
54 0 ▶ print "*** IMPORT MODE ***\n" if $self->{import_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"import_mode"} 0 0 0
55  
56 0 my @out;
57 my @comments;
58 0 my $msgctxt;
59 0 my @msgid;
60 0 my @msgstr;
61 0 my $msgstr_idx = 0;
62 0 my $mode = $MODE_DEFAULT;
63 0 my $n = 0;
64  
65 0 my @lines = split(/\n/, $$textref);
66 0 push @lines, ''; # add extra blank line at the end for parser to catch the last unit
67 0 my $last_line = scalar @lines;
68  
69 0 foreach my $line (@lines) {
70 0 $n++;
71 0 my $orig_line = $line;
72  
73 0 $line =~ s/^\s+//g;
74 0 $line =~ s/\s+$//g;
75  
76 # dev/translator comment or reference
77 0 ▶ if (($mode == $MODE_DEFAULT) && ($line =~ m/^#[\.:]?(\s+.+)?$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_DEFAULT and $line =~ /^#[\.:]?(\s+.+)?$/) 0 0 0
Condition0%0/3
$mode == $MODE_DEFAULT and $line =~ /^#[\.:]?(\s+.+)?$/
!ll&&!rl&&r
000
MC/DC0%0/2
$mode == $MODE_DEFAULT and $line =~ /^#[\.:]?(\s+.+)?$/
$mode == $MODE_DEFAULT $line =~ /^#[\.:]?(\s+.+)?$/
Truth table0%0/3
A and B
A= $mode == $MODE_DEFAULTB= $line =~ /^#[\.:]?(\s+.+)?$/
ABresult
0X0
100
111
78 0 $line =~ s/^#[\.:]?\s*//;
79 0 push @comments, $line;
80 0 push @out, $orig_line;
81 0 next;
82 }
83  
84 # flags
85 0 ▶ if (($mode == $MODE_DEFAULT) && ($line =~ m/^#,/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_DEFAULT and $line =~ /^#,/) 0 0 0
Condition0%0/3
$mode == $MODE_DEFAULT and $line =~ /^#,/
!ll&&!rl&&r
000
MC/DC0%0/2
$mode == $MODE_DEFAULT and $line =~ /^#,/
$mode == $MODE_DEFAULT $line =~ /^#,/
Truth table0%0/3
A and B
A= $mode == $MODE_DEFAULTB= $line =~ /^#,/
ABresult
0X0
100
111
86 0 push @out, $orig_line;
87 0 next;
88 }
89  
90 # non-standard comment lines
91 0 ▶ if (($mode == $MODE_DEFAULT) && ($line =~ m/^#(.)\s+/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_DEFAULT and $line =~ /^#(.)\s+/) 0 0 0
Condition0%0/3
$mode == $MODE_DEFAULT and $line =~ /^#(.)\s+/
!ll&&!rl&&r
000
MC/DC0%0/2
$mode == $MODE_DEFAULT and $line =~ /^#(.)\s+/
$mode == $MODE_DEFAULT $line =~ /^#(.)\s+/
Truth table0%0/3
A and B
A= $mode == $MODE_DEFAULTB= $line =~ /^#(.)\s+/
ABresult
0X0
100
111
92 0 ▶ print "WARNING: unsupported comment at line $n: $line\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
93 0 push @out, $orig_line;
94 0 next;
95 }
96  
97 0 ▶ if ($line =~ m/^msgid\s+"(.*)"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^msgid\s+"(.*)"$/) 0 0 0
98 0 $mode = $MODE_MSGID;
99 0 $msgid[0] = $1;
100 0 push @out, $orig_line;
101 0 next;
102 }
103  
104 0 ▶ if ($line =~ m/^msgctxt\s+"(.*)"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^msgctxt\s+"(.*)"$/) 0 0 0
105 0 $mode = $MODE_MSGCTXT;
106 0 $msgctxt = $1;
107 0 push @out, $orig_line;
108 0 next;
109 }
110  
111 0 ▶ if ($line =~ m/^msgid_plural\s+"(.*)"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^msgid_plural\s+"(.*)"$/) 0 0 0
112 0 $mode = $MODE_MSGID_PLURAL;
113 0 $msgid[1] = $1;
114 0 push @out, $orig_line;
115 0 next;
116 }
117  
118 0 ▶ if ($line =~ m/^msgstr(\[(\d+)\])?\s+"(.*)"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^msgstr(\[(\d+)\])?\s+"(.*)"$/) 0 0 0
119 0 $mode = $MODE_MSGSTR;
120  
121 # deal with the .po file header which has an empty msgid
122 0 ▶ if ($msgid[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($msgid[0] eq "") 0 0 0
123 0 push @out, $orig_line;
124 0 next;
125 }
126  
127 0 $msgstr_idx = $2;
128 0 $msgstr[$msgstr_idx] = $3;
129 0 ▶ push @out, $orig_line if $self->{import_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"import_mode"} 0 0 0
130  
131 0 next;
132 }
133  
134 # multiline string part
135 0 ▶ if ($line =~ m/^"(.*)"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^"(.*)"$/) 0 0 0
136 0 ▶ if ($mode == $MODE_MSGID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_MSGID) 0 0 0
137 0 $msgid[0] .= $1;
138 0 push @out, $orig_line;
139 0 next;
140 }
141  
142 0 ▶ if ($mode == $MODE_MSGID_PLURAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_MSGID_PLURAL) 0 0 0
143 0 $msgid[1] .= $1;
144 0 push @out, $orig_line;
145 0 next;
146 }
147  
148 0 ▶ if ($mode == $MODE_MSGCTXT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_MSGCTXT) 0 0 0
149 0 $msgctxt .= $1;
150 0 push @out, $orig_line;
151 0 next;
152 }
153  
154 0 ▶ if ($mode == $MODE_MSGSTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == $MODE_MSGSTR) 0 0 0
155 # deal with the .po file header which has an empty msgid
156 0 ▶ if ($msgid[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($msgid[0] eq "") 0 0 0
157 0 push @out, $orig_line;
158 0 next;
159 }
160  
161 0 $msgstr[$msgstr_idx] .= $1;
162 0 ▶ push @out, $orig_line if $self->{import_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"import_mode"} 0 0 0
163 0 next;
164 }
165 }
166  
167 # empty line denotes the end of the unit
168 0 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
169 0 ▶ if (scalar(@msgid) > 0 && $msgid[0] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @msgid > 0 and $msgid[0] ne "") 0 0 0
Condition0%0/3
scalar @msgid > 0 and $msgid[0] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @msgid > 0 and $msgid[0] ne ""
scalar @msgid > 0 $msgid[0] ne ""
Truth table0%0/3
A and B
A= scalar @msgid > 0B= $msgid[0] ne ""
ABresult
0X0
100
111
170 0 my $msgid_0 = $msgid[0];
171 0 unescape_strref(\$msgid_0);
172  
173 # need to use own variable ($x) instead of $_ since Perl would not
174 # allow to modify the reference for $_ for missing array entries
175 # (when array indexes have gaps)
176 0 @msgid = map { my $x = $_; unescape_strref(\$x); $x } @msgid;
177 0 @msgstr = map { my $x = $_; unescape_strref(\$x); $x } @msgstr;
178  
179 0 ▶ my $str = glue_plural_string($self->{import_mode} ? @msgstr : @msgid);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'import_mode'} ? : 0 0 0
180 0 ▶ if ($str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne "") 0 0 0
181 0 ▶ my $comment = @comments > 0 ? join("\n", @comments) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@comments > 0 ? : 0 0 0
182  
183 0 my $key = generate_key($msgid_0, $msgctxt);
184 0 ▶ if (!$lang or $self->{import_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lang or $self->{'import_mode'}) { } 0 0 0
Condition0%0/3
not $lang or $self->{"import_mode"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $lang or $self->{"import_mode"}
not $lang $self->{"import_mode"}
Truth table0%0/3
not(A) or B
A= not $langB= $self->{"import_mode"}
ABresult
1X1
011
000
185 0 &$callbackref($str, $msgctxt, $comment, undef, $lang, $key);
186 } else {
187 0 my $translation = &$callbackref($str, $msgctxt, $comment, undef, $lang, $key);
188 0 push @out, po_serialize_msgstr($translation);
189 0 ▶ if (exists $self->{strings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"strings"}) 0 0 0
190 0 $self->_add_string(\@msgid, $msgctxt, [split_plural_string($translation)]);
191 }
192 }
193 }
194 }
195  
196 # reset state
197  
198 0 @comments = ();
199 0 @msgid = ();
200 0 @msgstr = ();
201 0 $msgstr_idx = 0;
202 0 $msgctxt = undef;
203 0 $mode = $MODE_DEFAULT;
204 0 ▶ push @out, $orig_line unless $n == $last_line; # do not output the extra blank line that we added
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $last_line 0 0 0
205 0 next;
206 }
207  
208 0 die "Failed to parse .PO at line $n: '$orig_line'";
209 }
210  
211 0 ▶ return $lang ? join("\n", @out) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 0 0 0
212 }
213  
214 sub _add_string {
215 0 ▶ my ($self, $msgidref, $msgctxt, $msgstrref) = @_;
Subroutine _add_string: not called CC=2 SCAR=17.9
216 0 my $key = join($MO_PLURAL_SEPARATOR, map { encode_utf8($_) } @$msgidref);
217 0 ▶ $key = $msgctxt.$MO_CTX_SEPARATOR.$key if $msgctxt ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $msgctxt ne "" 0 0 0
218 0 $self->{strings}->{$key} = join($MO_PLURAL_SEPARATOR, map { encode_utf8($_) } @$msgstrref);
219 }
220  
221 sub _mo_chr {
222 0 ▶ return map { pack "N*", $_ } @_;
Subroutine _mo_chr: not called CC=1 SCAR=6.9
223 }
224  
225 sub _mo_str {
226 0 ▶ return shift.chr(0);
Subroutine _mo_str: not called CC=1 SCAR=6.9
227 }
228  
229 sub _generate_mo {
230 0 ▶ my ($self, $file) = @_;
Subroutine _generate_mo: not called CC=6 SCAR=37.4
231  
232 # .MO file format documentation:
233 # https://www.gnu.org/software/gettext/manual/html_node/MO-Files.html
234  
235 0 my $count = scalar keys %{$self->{strings}};
236 0 my $offset = $MO_HEADER_SIZE + $count * 16;
237 0 my @sorted = sort keys %{$self->{strings}};
238 0 my @translations = map { $self->{strings}->{$_} } @sorted;
239  
240 0 ▶ open my $OUT, ">", $file or die "Failed to open file [$file] for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $file or die "Failed to open file [$file] for writing: $!" 0 0 0
241 0 binmode $OUT;
242 0 print $OUT _mo_chr($MO_MAGIC);
243 0 print $OUT _mo_chr($MO_FORMAT);
244 0 print $OUT _mo_chr($count);
245 0 print $OUT _mo_chr($MO_HEADER_SIZE); # offset of table with original strings
246 0 print $OUT _mo_chr($MO_HEADER_SIZE + $count * 8); # offset of table with translation strings
247 0 print $OUT _mo_chr(0); # size of hashing table (we don't generate it)
248 0 print $OUT _mo_chr($offset); # offset of hashing table (this must be set!)
249  
250 0 foreach (@sorted) {
251 0 my $length = length($_);
252 0 print $OUT _mo_chr($length);
253 0 print $OUT _mo_chr($offset);
254 0 $offset += $length + 1;
255 }
256  
257 0 foreach (@translations) {
258 0 my $length = length($_);
259 0 print $OUT _mo_chr($length);
260 0 print $OUT _mo_chr($offset);
261 0 $offset += $length + 1;
262 }
263  
264 0 foreach (@sorted) {
265 0 print $OUT _mo_str($_);
266 }
267  
268 0 foreach (@translations) {
269 0 print $OUT _mo_str($_);
270 }
271  
272 0 close $OUT;
273 }
274  
275 sub after_save_localized_file {
276 0 ▶ my ($self, $phase, $relfile, $lang, $contentref) = @_;
Subroutine after_save_localized_file: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my $dstpath = $self->{parent}->{engine}->get_full_output_path($relfile, $lang, $self->{data}->{output_mo_path});
278 0 print "\t\tCompiling $dstpath\n";
279 0 $self->_generate_mo($dstpath);
280 }
281  
282 1 1;
« lib/Serge/DB/Cached.pm ↑ Summary lib/Serge/DB.pm »