blib/lib/Locale/PO/Callback.pm

filter: Statementstmt 78.7% 133 / 169 Branchbran 54.8% 34 / 62 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 73.6% 14 / 19 Podpod 100% 7 / 7 totaltotal 72.9% 197 / 270 CC 46 SCAR 24.8
CC
46
Cov
72%
__ANON__
25.5
__ANON__
17.9
__ANON__
13.4
CRAP 11.9
↑ Summary
1 package Locale::PO::Callback;
2  
3 # TO DO:
4 # - support encodings other than UTF-8
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = 0.04;
12  
13 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
14 4 my ($class, $callback) = @_;
15 4 my $self = {
16 callback => $callback,
17 };
18  
19 4 return bless($self, $class);
20 }
21  
22 0 ▶sub read_string {
Subroutine read_string: not called CC=1 SCAR=6.9
Pod documented
23 0 my ($self, $string) = @_;
24  
25 0 $self->read($string, 1);
26 }
27  
28 0 ▶sub read_file {
Subroutine read_file: not called CC=1 SCAR=6.9
Pod documented
29 0 my ($self, $filename) = @_;
30  
31 0 $self->read($filename, 0);
32 }
33  
34 3 ▶sub read {
Subroutine read: called CC=10 SCAR=23.4
Pod documented
35 3 my ($self, $filename, $is_string) = @_;
36  
37 3 ▶ if ($is_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_string) { } 0 3 3
38 0 ▶ open PO, '<', \$filename or die "Couldn't read string: $!";
Branch: 1 branch
BranchTrueFalseTotal
open PO, "<", \$filename or die "Couldn't read string: $!" 0 0 0
39 } else {
40 3 ▶ open PO, "<$filename" or die "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open PO, "<$filename" or die "Couldn't open ${filename}: $!" 3 0 3
41 }
42  
43 3 binmode PO, ":utf8";
44  
45 # Every line in a .po file is either:
46 # * a comment, applying to the next stanza
47 # (Some comments may have machine-readable meanings,
48 # but they remain comments)
49 # * a command, part of a stanza
50 # * a continuation of a previous command
51 # * something else, which should be passed through
52 #
53 # The first stanza may map the empty string to a non-empty
54 # string, and if so it is magic and defines headers.
55  
56 18 ▶ my $empty_stanza = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 {
58 18 comments => '',
59 };
60 3 };
61  
62 21 ▶ my $escaper = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
63 21 my %escapes = (
64 'n' => "\n",
65 't' => "\t",
66 );
67 21 my ($char) = @_;
68  
69 21 ▶ return $escapes{$char} if defined $escapes{$char};
Branch: 1 branch
BranchTrueFalseTotal
if defined $escapes{$char} 21 0 21
70 0 return $char;
71 3 };
72  
73 51 ▶ my $unstring = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 51 my ($str) = @_;
75 51 $str =~ s/^"//;
76 51 $str =~ s/"$//;
77 51 $str =~ s/\\(.)/$escaper->($1)/ge;
78
79 51 return $str;
80 3 };
81  
82 3 my $first = 1;
83  
84 3 my $stanza = $empty_stanza->();
85  
86 # Which command a continuation is a continuation of.
87 3 my $continuing = 'msgstr';
88  
89 18 ▶ my $handle_stanza = sub {
Subroutine __ANON__: called CC=13 SCAR=26.0
90 18 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) { } 3 15 18
91  
92 3 ▶ if ($stanza->{'msgid'} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stanza->{'msgid'} eq '') { } 3 0 3
93 # good, that's what we expected
94 3 $stanza->{'type'} = 'header';
95 3 $stanza->{'headers'} = {};
96 3 $stanza->{'header_order'} = [];
97 # FIXME allow for continuation lines
98 # although nobody ever uses them
99 3 for my $line (split /\n/, $stanza->{'msgstr'}) {
100 21 $line =~ /^(.*): (.*)$/;
101 21 $stanza->{'headers'}->{lc $1} = $2;
102 21 push @{ $stanza->{'header_order'} }, $1;
103 }
104 3 delete $stanza->{'msgid'};
105 3 delete $stanza->{'msgstr'};
106 } else {
107 # Oh dear, no header. Fake one.
108 0 $self->{callback}->({
109 type => 'header',
110 headers => {},
111 header_order => [],
112 });
113 }
114  
115 3 $first = 0;
116 } else {
117 15 $stanza->{'type'} = 'translation';
118 15 $stanza->{'locations'} = [];
119 15 $stanza->{'flags'} = {};
120 15 ▶ $stanza->{'comments'} = '' unless defined $stanza->{'comments'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stanza->{"comments"} 0 15 15
121  
122 15 my @comments;
123  
124 15 for my $comment (split /\n/, $stanza->{'comments'}) {
125 9 ▶ if ($comment =~ /^#: (.*):(\d*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($comment =~ /^#: (.*):(\d*)$/) { } 3 6 9
elsif ($comment =~ /^#, (.*)$/) { } 6 0 6
126 3 push @{ $stanza->{'locations'} }, [$1, $2];
127 } elsif ($comment =~ /^#, (.*)$/) {
128 6 my $flags = $1;
129 6 $flags =~ s/\s*,\s*/,/g;
130 6 for my $flag (split m/,/, $flags) {
131 6 $stanza->{'flags'}->{lc $flag} = 1;
132 }
133 } else {
134 0 push @comments, $comment;
135 }
136 }
137  
138 # Anything we didn't handle goes back in the comments field.
139 15 $stanza->{'comments'} = join("\n", @comments);
140 }
141  
142 18 ▶ if (defined $stanza->{'msgid'} ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stanza->{"msgid"} or defined $stanza->{"msgstr"} or defined $stanza->{"msgstr[0]"} or defined $stanza->{"headers"}) 15 3 18
Condition77%7/9
defined $stanza->{"msgid"} or defined $stanza->{"msgstr"} or defined $stanza->{"msgstr[0]"} or defined $stanza->{"headers"}
l!l&&r!l&&!r
1233
defined $stanza->{"msgid"} or defined $stanza->{"msgstr"} or defined $stanza->{"msgstr[0]"}
l!l&&r!l&&!r
1206
defined $stanza->{"msgid"} or defined $stanza->{"msgstr"}
l!l&&r!l&&!r
1206
MC/DC50%2/4
defined $stanza->{"msgid"} or defined $stanza->{"msgstr"} or defined $stanza->{"msgstr[0]"} or defined $stanza->{"headers"}
defined $stanza->{"msgid"} defined $stanza->{"msgstr"} defined $stanza->{"msgstr[0]"} defined $stanza->{"headers"}
Truth table60%3/5
A or B or C or D
A= defined $stanza->{"msgid"}B= defined $stanza->{"msgstr"}C= defined $stanza->{"msgstr[0]"}D= defined $stanza->{"headers"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
143 defined $stanza->{'msgstr'} ||
144 defined $stanza->{'msgstr[0]'} ||
145 defined $stanza->{'headers'}
146 ) {
147 15 $self->{callback}->($stanza);
148 }
149 3 };
150  
151 3 ▶ while (<PO>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline PO)) 78 3 81
152 78 chomp;
153 78 ▶ if (/^$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^$/) { } 15 63 78
elsif (/^#/) { } 12 51 63
elsif (/^"/) { } 21 30 51
elsif (/^([^ ]*) (".*)/) { } 30 0 30
154 15 $handle_stanza->();
155 15 $stanza = $empty_stanza->();
156 } elsif (/^#/) {
157 12 $stanza->{comments} .= $_ . "\n";
158 } elsif (/^"/) {
159 21 $stanza->{$continuing} .= $unstring->($_);
160 } elsif (/^([^ ]*) (".*)/) {
161 30 $stanza->{$1} = $unstring->($2);
162 30 $continuing = $1;
163 } else {
164 0 $self->{callback}->({other => $_, type => 'other'});
165 }
166 }
167 3 $handle_stanza->();
168 3 ▶ close PO or die "Couldn't close $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
close PO or die "Couldn't close ${filename}: $!" 3 0 3
169 }
170  
171 1 ▶sub create_empty {
Subroutine create_empty: called CC=3 SCAR=11.0
Pod documented
172 1 my ($self) = @_;
173  
174 1 my @fields = (
175 ['Project-Id-Version' => 'PACKAGE VERSION'],
176 ['PO-Revision-Date' => _today()],
177 # FIXME: take this from the environment,
178 # if it's available?
179 ['Last-Translator' => 'FULL NAME <EMAIL@ADDRESS>'],
180 ['Language-Team' => 'LANGUAGE <LL@li.org>'],
181 ['MIME-Version' => '1.0'],
182 ['Content-Type' => 'text/plain; charset=UTF-8'],
183 ['Content-Transfer-Encoding' => '8bit'],
184 );
185  
186 1 my @fieldnames;
187 my %fields;
188  
189 1 for (@fields) {
190 7 push @fieldnames, $_->[0];
191 }
192  
193 1 for (@fields) {
194 7 $fields{lc $_->[0]} = $_->[1];
195 }
196  
197 1 $self->{callback}->({type => 'header',
198 headers => \%fields,
199 header_order => \@fieldnames,
200 });
201 }
202  
203 2 ▶sub rebuilder {
Subroutine rebuilder: called CC=2 SCAR=7.7
Pod documented
204 2 my ($callback) = @_;
205  
206 0 ▶ $callback = sub { print $_[0]; } unless $callback;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless $callback 0 2 2
207  
208 11 ▶ return sub {
Subroutine __ANON__: called CC=12 SCAR=28.9
209 11 my ($stanza) = @_;
210  
211 32 ▶ my $output_line = sub {
Subroutine __ANON__: called CC=8 SCAR=32.4
212 32 my ($keyword) = @_;
213 32 my $text = $stanza->{$keyword};
214 32 my $max_width = 79;
215  
216 32 ▶ return '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 16 16 32
217  
218 16 $text =~ s/\\/\\\\/g;
219 16 $text =~ s/\t/\\t/g;
220 16 $text =~ s/\n/\\n/g;
221 16 $text =~ s/\"/\\"/g;
222  
223 # Test the simple case first
224 16 ▶ if (length($keyword) + 4 + length($text) <= $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($keyword) + 4 + length($text) <= $max_width) 16 0 16
225 16 return "$keyword \"$text\"\n";
226 }
227  
228 0 my $result = "$keyword \"\"\n";
229  
230 0 my @words;
231  
232 0 ▶ while ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 0 0 0
233 0 $text =~ s/^(\S*\s*)//;
234 0 push @words, $1;
235 }
236  
237 0 my $temp = '';
238
239 0 for (@words) {
240 0 ▶ if (length($temp . $_) >= $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $temp . $_ >= $max_width) { } 0 0 0
241 0 ▶ if ($temp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp) { } 0 0 0
242 0 $result .= "\"$temp\"\n";
243 0 $temp = $_;
244 } else {
245 0 $result .= "\"$_\"\n";
246 }
247 } else {
248 0 $temp .= $_;
249 }
250 }
251  
252 0 ▶ $result .= "\"$temp\"\n" if $temp;
Branch: 1 branch
BranchTrueFalseTotal
if $temp 0 0 0
253
254 0 return $result;
255 11 };
256  
257 11 my $result = '';
258  
259 11 ▶ if ($stanza->{'type'} eq 'translation') {
Branch: 3 branches
BranchTrueFalseTotal
if ($stanza->{'type'} eq 'translation') { } 8 3 11
elsif ($stanza->{'type'} eq 'header') { } 3 0 3
elsif ($stanza->{'type'} eq 'other') { } 0 0 0
260 8 ▶ $result .= $stanza->{'comments'}."\n" if $stanza->{'comments'};
Branch: 1 branch
BranchTrueFalseTotal
if $stanza->{"comments"} 0 8 8
261 8 for my $flag (keys %{$stanza->{'flags'}}) {
262 4 $result .= "#, $flag\n";
263 }
264 8 for my $location (@{$stanza->{'locations'}}) {
265 2 $result .= "#: $location->[0]:$location->[1]\n";
266 }
267 8 $result .= $output_line->('msgctxt');
268 8 $result .= $output_line->('msgid');
269 8 $result .= $output_line->('msgid_plural');
270 8 for my $msgstr (grep { /^msgstr/ } sort keys %$stanza) {
271 8 $result .= $output_line->($msgstr);
272 }
273 8 $result .= "\n";
274 } elsif ($stanza->{'type'} eq 'header') {
275 3 ▶ $result .= $stanza->{'comments'} if $stanza->{'comments'};
Branch: 1 branch
BranchTrueFalseTotal
if $stanza->{"comments"} 2 1 3
276 3 $result .= "msgid \"\"\n";
277 3 $result .= "msgstr \"\"\n";
278 3 my %seen;
279 3 for my $header (@{$stanza->{'header_order'}}) {
280 21 my $value = $stanza->{'headers'}->{lc $header};
281 21 $result .= "\"$header: $value\\n\"\n";
282 21 $seen{lc $header} = 1;
283 }
284 3 for my $header (keys %{$stanza->{'headers'}}) {
285 22 $header = lc $header;
286 22 ▶ next if defined $seen{$header};
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen{$header} 21 1 22
287 1 my $value = $stanza->{'headers'}->{$header};
288 1 $header =~ s/\b(.)/\u$1/g; # titlecase
289 1 $result .= "\"$header: $value\\n\"\n";
290 }
291 3 $result .= "\n";
292 } elsif ($stanza->{'type'} eq 'other') {
293 0 $result .= '# (other:) '.$stanza->{'other'}."\n";
294 } else {
295 0 die "Unknown type $stanza->{'type'}";
296 }
297  
298 11 $callback->($result);
299 2 };
300 }
301  
302 1 ▶sub _today {
Subroutine _today: called CC=1 SCAR=0.0
303 1 return strftime("%Y-%m-%d %H:%M %z", localtime);
304 }
305  
306 0 ▶sub set_date {
Subroutine set_date: not called CC=1 SCAR=6.9
Pod documented
307 0 my ($callback) = @_;
308  
309 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
310 0 my ($param) = @_;
311  
312 0 ▶ if ($param->{'type'} eq 'header') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"type"} eq "header") 0 0 0
313 0 my $date_found = defined $param->{'headers'}->{'po-revision-date'};
314  
315 0 $param->{'headers'}->{'po-revision-date'} = _today();
316  
317 0 ▶ push @{$param->{'header_order'}}, 'PO-Revision-Date'
Branch: 1 branch
BranchTrueFalseTotal
unless $date_found 0 0 0
318 unless $date_found;
319 }
320  
321 0 $callback->($param);
322 0 };
323 }
324  
325 6 1;
326  
327 =head1 NAME
328  
329 Locale::PO::Callback - parse gettext source files
330  
331 =head1 AUTHOR
332  
333 Thomas Thurman <thomas@thurman.org.uk>
334  
335 =head1 SYNOPSIS
336  
337 use Locale::PO::Callback;
338  
339 sub callback {
340 # ...
341 }
342  
343 my $lpc = Locale::PO::Callback->new(\&callback);
344 $lpc->read('test.po');
345  
346 =head1 DESCRIPTION
347  
348 This module parses the .po files used by GNU gettext
349 to hold translation catalogues. It takes one parameter,
350 a coderef, and calls it repeatedly with a description of
351 every item in the file. This enables chains of filters
352 to be produced, as is commonly done with XML processing.
353  
354 =head1 METHODS
355  
356 =head2 new(callback)
357  
358 Creates an object. The callback parameter is a coderef
359 which will be called with a description of every item
360 in the file.
361  
362 =head2 read_file(filename)
363  
364 Reads and parses a file.
365  
366 =head2 read_string(string)
367  
368 Parses a string.
369  
370 =head2 read(filename_or_string, is_string)
371  
372 Reads and parses a file or a string, depending on the is_string
373 argument.
374  
375 =head2 create_empty()
376  
377 Behaves as though we had just read in an empty file,
378 with default headers.
379  
380 =head1 OTHER THINGS
381  
382 =head2 rebuilder(coderef)
383  
384 Given a coderef, this function returns a function which
385 can be passed as a callback to this class's constructor.
386 The coderef will be called with strings which, if concatenated,
387 make a .po file equivalent to the source .po file.
388  
389 In pipeline terms, this function produces sinks.
390  
391 =head2 set_date(coderef)
392  
393 Given a coderef, this function returns a function which
394 can be passed as a callback to this class's constructor.
395 The function will pass its parameters through to the
396 coderef unchanged, except for headers, when the file date
397 will be changed to the current system date.
398  
399 In pipeline terms, this function produces filters.
400  
401 =head1 PARAMETERS TO THE CALLBACK
402  
403 =head2 type
404  
405 "header", "translation", or "other" (which last should never
406 appear in ordinary use).
407  
408 =head2 comments
409  
410 An arrayref of comments which appear before this item.
411  
412 =head2 flags
413  
414 A hashref of the flags of this item (such as "fuzzy").
415  
416 =head2 locations
417  
418 An arrayref of arrayrefs, the first item being a filename
419 and the second being a line number.
420  
421 =head2 msgid
422  
423 The source message, in its singular form.
424  
425 =head2 msgid_plural
426  
427 The source message, in its plural form.
428 This is usually empty.
429  
430 =head2 msgstr
431  
432 The translation, if any,
433 unless this translation has plural forms,
434 in which case see the next entry.
435  
436 =head2 msgstr[0] (etc)
437  
438 Variations on the translation for different plural forms.
439  
440 =head2 msgctxt
441  
442 The "context" of the translation.
443 Rarely filled in.
444  
445 =head2 headers
446  
447 A hashref of headers, mapping fieldnames to values.
448 The keys are lowercased.
449  
450 =head2 header_order
451  
452 An arrayref of the header fieldnames, in the casing and order
453 in which they were found.
454  
455 =head1 FUTURE EXPANSION
456  
457 We need to support encodings other than UTF-8.
458  
459 This documentation was written in a bit of a rush.
460  
461 =head1 COPYRIGHT
462  
463 This Perl module is copyright (C) Thomas Thurman, 2010.
464 This is free software, and can be used/modified under the same terms as
465 Perl itself.
466  
467  
↑ Summary