lib/Locale/Simple/PO2JSON.pm

filter: Statementstmt 100% 117 / 117 Branchbran 90.2% 65 / 72 Conditioncond 75.0% 30 / 40 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 89.5% 240 / 268 CC 55 SCAR 29.4
CC
55
Cov
93%
po2data
32.5
__ANON__
11.1
_parse_header
6.1
CRAP 18.9
« lib/Locale/Simple/Scraper.pm ↑ Summary lib/Locale/Simple.pm »
1 package Locale::Simple::PO2JSON;
2 # ABSTRACT: Convert a gettext PO file to the JSON data Locale::Simple reads in the browser
3 24 our $VERSION = '0.200';
4  
5 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 24 our @EXPORT_OK = qw( po2data data2json );
13  
14 24 my %ESCAPE = (
15 n => "\n", t => "\t", r => "\r", a => "\a",
16 b => "\b", f => "\f", v => "\x0b", '\\' => '\\', '"' => '"',
17 );
18  
19 689 ▶sub _unquote {
Subroutine _unquote: called CC=4 SCAR=14.2
20 689 my ( $path, $n, $str ) = @_;
21 689 $str =~ s{\\(?:([ntrabfv\\"])|([0-7]{1,3})|x([0-9a-fA-F]{1,2})|(.))}{
22 170 ▶ defined $1 ? $ESCAPE{$1}
Branch: 3 branches
BranchTrueFalseTotal
defined $1 ? : 169 1 170
defined $2 ? : 0 1 1
defined $3 ? : 0 1 1
23 : defined $2 ? chr( oct $2 )
24 : defined $3 ? chr( hex $3 )
25 : die "$path line $n: unknown escape sequence \\$4\n"
26 }gse;
27 688 return $str;
28 }
29  
30 # One or more quoted strings on a line, as msgfmt reads them: "a" "b" is "ab".
31 24 my $STR = qr/"(?:[^"\\]|\\.)*"/;
32 24 my $STRS = qr/$STR(?:\s*$STR)*/;
33  
34 684 ▶sub _unquote_all {
Subroutine _unquote_all: called CC=2 SCAR=6.9
35 684 my ( $path, $n, $strings ) = @_;
36 684 my $val = '';
37 684 ▶ $val .= _unquote( $path, $n, $1 ) while $strings =~ /"((?:[^"\\]|\\.)*)"/g;
Branch: 1 branch
BranchTrueFalseTotal
$strings =~ /"((?:[^"\\]|\\.)*)"/g and do { $val .= _unquote($path, $n, $1) } 689 683 1372
38 683 return $val;
39 }
40  
41 34 ▶sub _read_file {
Subroutine _read_file: called CC=4 SCAR=13.9
42 34 my ($path) = @_;
43 34 ▶ open my $fh, '<:raw', $path or die "Cannot read PO file $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "Cannot read PO file ${path}: $!\n" 29 5 34
44 29 local $/;
45 29 my $raw = <$fh>;
46 29 close $fh;
47 29 ▶ $raw = '' unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 29 29
48 29 $raw =~ s/\A\xEF\xBB\xBF//;
49 29 my $text = eval { Encode::decode( 'UTF-8', $raw, Encode::FB_CROAK() ) };
50 29 ▶ die "Cannot read PO file $path: not valid UTF-8\n" unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 28 29
51 28 return $text;
52 }
53  
54 20 ▶sub _parse_header {
Subroutine _parse_header: called CC=6 SCAR=18.0
55 20 my ($msgstr) = @_;
56 20 my %h;
57 20 for my $line ( split /\n/, $msgstr ) {
58 179 ▶ next unless $line =~ /\A([^:]+):(.*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A([^:]+):(.*)\z/ 0 179 179
59 179 my ( $k, $v ) = ( $1, $2 );
60 179 s/\A\s+|\s+\z//g for $k, $v;
61 179 $h{$k} = $v;
62 }
63 20 ▶ $h{lang} = $h{Language} if defined $h{Language} && length $h{Language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{"Language"} and length $h{"Language"} 19 1 20
Condition66%2/3
defined $h{"Language"} and length $h{"Language"}
!ll&&!rl&&r
1019
MC/DC50%1/2
defined $h{"Language"} and length $h{"Language"}
defined $h{"Language"} length $h{"Language"}
Truth table66%2/3
A and B
A= defined $h{"Language"}B= length $h{"Language"}
ABresult
0X0
100
111
64 20 return \%h;
65 }
66  
67 34 ▶sub po2data {
Subroutine po2data: called CC=32 SCAR=34.8
Pod documented
68 34 my ( $path, %opt ) = @_;
69 34 my $text = _read_file($path);
70  
71 28 my ( %entries, $header, $has_plural );
72 28 my ( $cur, $field, $flags, $cur_flags ) = ( undef, undef, {} );
73  
74 170 ▶ my $finish = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
75 170 ▶ return unless $cur;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur 3 167 170
76 167 my $e = $cur;
77 167 undef $cur;
78 167 undef $field;
79 167 ▶ my @str = map { defined $_ ? $_ : '' } @{ $e->{msgstr} };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 238 0 238
80 167 ▶ if ( !defined $e->{msgctxt} && $e->{msgid} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $e->{"msgctxt"} and $e->{"msgid"} eq "") 20 147 167
Condition100%3/3
not defined $e->{"msgctxt"} and $e->{"msgid"} eq ""
!ll&&!rl&&r
1713020
MC/DC100%2/2
not defined $e->{"msgctxt"} and $e->{"msgid"} eq ""
not defined $e->{"msgctxt"} $e->{"msgid"} eq ""
Truth table100%3/3
not(A) and B
A= not defined $e->{"msgctxt"}B= $e->{"msgid"} eq ""
ABresult
0X0
100
111
81 20 ▶ $header = _parse_header( $str[0] // '' );
Condition50%1/2
$str[0] // ""
l!l
200
MC/DC0%0/1
$str[0] // ""
$str[0]
Truth table50%1/2
A
A= $str[0]
Aresult
11
00
82 20 return;
83 }
84 147 ▶ return if $cur_flags->{fuzzy};
Branch: 1 branch
BranchTrueFalseTotal
if $cur_flags->{"fuzzy"} 1 146 147
85 146 ▶ return unless @str && length $str[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @str and length $str[0] 1 145 146
Condition66%2/3
@str and length $str[0]
!ll&&!rl&&r
01145
MC/DC50%1/2
@str and length $str[0]
@str length $str[0]
Truth table66%2/3
A and B
A= @strB= length $str[0]
ABresult
0X0
100
111
86 145 ▶ $has_plural = 1 if defined $e->{msgid_plural};
Branch: 1 branch
BranchTrueFalseTotal
if defined $e->{"msgid_plural"} 70 75 145
87 145 ▶ my $key = defined $e->{msgctxt} ? "$e->{msgctxt}\x04$e->{msgid}" : $e->{msgid};
Branch: 1 branch
BranchTrueFalseTotal
defined $e->{'msgctxt'} ? : 17 128 145
88 145 $entries{$key} = [ $e->{msgid_plural}, @str ];
89 28 };
90  
91 28 my $n = 0;
92 28 for my $line ( split /\r?\n/, $text ) {
93 841 $n++;
94 841 ▶ if ( $line =~ /\A\s*\z/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /\A\s*\z/) { } 148 693 841
elsif ($line =~ /\A\s*#~/) { } 2 691 693
elsif ($line =~ /\A\s*#(.?)(.*)\z/) { } 5 686 691
elsif ($line =~ /\A\s*(msgctxt|msgid_plural|msgid|msgstr(?:\[(\d+)\])?)\s*($STRS)\s*\z/) { } 501 185 686
elsif ($line =~ /\A\s*($STRS)\s*\z/) { } 184 1 185
95 148 $finish->();
96 148 $flags = {};
97 }
98 elsif ( $line =~ /\A\s*#~/ ) {
99 # obsolete entry, including #~| previous msgids
100 2 $finish->();
101 2 $flags = {};
102 }
103 elsif ( $line =~ /\A\s*#(.?)(.*)\z/ ) {
104 5 my ( $kind, $rest ) = ( $1, $2 );
105 5 ▶ $finish->() if $cur && $field =~ /\Amsgstr/;
Branch: 1 branch
BranchTrueFalseTotal
if $cur and $field =~ /\Amsgstr/ 0 5 5
Condition33%1/3
$cur and $field =~ /\Amsgstr/
!ll&&!rl&&r
500
MC/DC0%0/2
$cur and $field =~ /\Amsgstr/
$cur $field =~ /\Amsgstr/
Truth table33%1/3
A and B
A= $curB= $field =~ /\Amsgstr/
ABresult
0X0
100
111
106 5 ▶ if ( $kind eq ',' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq ",") 2 3 5
107 2 $flags->{$_} = 1 for grep { length } split /[\s,]+/, $rest;
108 }
109 }
110 elsif ( $line =~ /\A\s*(msgctxt|msgid_plural|msgid|msgstr(?:\[(\d+)\])?)\s*($STRS)\s*\z/ ) {
111 501 my ( $kw, $idx, $val ) = ( $1, $2, _unquote_all( $path, $n, $3 ) );
112 500 ▶ if ( $kw eq 'msgctxt' || $kw eq 'msgid' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kw eq 'msgctxt' or $kw eq 'msgid') { } 189 311 500
elsif ($kw eq 'msgid_plural') { } 71 240 311
Condition100%3/3
$kw eq "msgctxt" or $kw eq "msgid"
l!l&&r!l&&!r
18171311
MC/DC100%2/2
$kw eq "msgctxt" or $kw eq "msgid"
$kw eq "msgctxt" $kw eq "msgid"
Truth table100%3/3
A or B
A= $kw eq "msgctxt"B= $kw eq "msgid"
ABresult
1X1
011
000
113 189 ▶ $finish->() if $cur && $field =~ /\Amsgstr/;
Branch: 1 branch
BranchTrueFalseTotal
if $cur and $field =~ /\Amsgstr/ 0 189 189
Condition66%2/3
$cur and $field =~ /\Amsgstr/
!ll&&!rl&&r
170190
MC/DC0%0/2
$cur and $field =~ /\Amsgstr/
$cur $field =~ /\Amsgstr/
Truth table66%2/3
A and B
A= $curB= $field =~ /\Amsgstr/
ABresult
0X0
100
111
114 189 ▶ if ( !$cur ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cur) 170 19 189
115 170 $cur = { msgstr => [] };
116 170 $cur_flags = $flags;
117 170 $flags = {};
118 }
119 die "$path line $n: $kw not allowed here\n"
120 if defined $cur->{$kw}
121 189 ▶ || ( $kw eq 'msgctxt' && defined $cur->{msgid} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $cur->{$kw} or $kw eq "msgctxt" and defined $cur->{"msgid"} 2 187 189
Condition100%6/6
defined $cur->{$kw} or $kw eq "msgctxt" and defined $cur->{"msgid"}
l!l&&r!l&&!r
11187
$kw eq "msgctxt" and defined $cur->{"msgid"}
!ll&&!rl&&r
170171
MC/DC100%3/3
defined $cur->{$kw} or $kw eq "msgctxt" and defined $cur->{"msgid"}
defined $cur->{$kw} $kw eq "msgctxt" defined $cur->{"msgid"}
Truth table100%4/4
A or B and C
A= defined $cur->{$kw}B= $kw eq "msgctxt"C= defined $cur->{"msgid"}
ABCresult
1XX1
0111
00X0
0100
122 187 $cur->{$kw} = $val;
123 187 $field = $kw;
124 }
125 elsif ( $kw eq 'msgid_plural' ) {
126 die "$path line $n: msgid_plural without msgid\n"
127 71 ▶ unless $cur && defined $cur->{msgid};
Branch: 1 branch
BranchTrueFalseTotal
unless $cur and defined $cur->{"msgid"} 1 70 71
Condition66%2/3
$cur and defined $cur->{"msgid"}
!ll&&!rl&&r
1070
MC/DC50%1/2
$cur and defined $cur->{"msgid"}
$cur defined $cur->{"msgid"}
Truth table66%2/3
A and B
A= $curB= defined $cur->{"msgid"}
ABresult
0X0
100
111
128 70 $cur->{msgid_plural} = $val;
129 70 $field = $kw;
130 }
131 else {
132 die "$path line $n: $kw without msgid\n"
133 240 ▶ unless $cur && defined $cur->{msgid};
Branch: 1 branch
BranchTrueFalseTotal
unless $cur and defined $cur->{"msgid"} 2 238 240
Condition66%2/3
$cur and defined $cur->{"msgid"}
!ll&&!rl&&r
20238
MC/DC50%1/2
$cur and defined $cur->{"msgid"}
$cur defined $cur->{"msgid"}
Truth table66%2/3
A and B
A= $curB= defined $cur->{"msgid"}
ABresult
0X0
100
111
134 238 ▶ $idx = 0 unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 97 141 238
135 238 $cur->{msgstr}[$idx] = $val;
136 238 $field = "msgstr[$idx]";
137 }
138 }
139 elsif ( $line =~ /\A\s*($STRS)\s*\z/ ) {
140 184 ▶ die "$path line $n: string without keyword\n" unless $cur && defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur and defined $field 1 183 184
Condition66%2/3
$cur and defined $field
!ll&&!rl&&r
10183
MC/DC50%1/2
$cur and defined $field
$cur defined $field
Truth table66%2/3
A and B
A= $curB= defined $field
ABresult
0X0
100
111
141 183 my $val = _unquote_all( $path, $n, $1 );
142 183 ▶ if ( $field =~ /\Amsgstr\[(\d+)\]\z/ ) { $cur->{msgstr}[$1] .= $val }
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /\Amsgstr\[(\d+)\]\z/) { } 182 1 183
143 1 else { $cur->{$field} .= $val }
144 }
145 else {
146 1 die "$path line $n: syntax error\n";
147 }
148 }
149 20 $finish->();
150  
151 20 ▶ $header ||= {};
Condition50%1/2
$header ||= {}
l!l
200
MC/DC0%0/1
$header ||= {}
$header
Truth table50%1/2
A
A= $header
Aresult
11
00
152 warn "$path: no Plural-Forms header, keeping the plural forms as found\n"
153 20 ▶ if $has_plural && !defined $header->{'Plural-Forms'};
Branch: 1 branch
BranchTrueFalseTotal
if $has_plural and not defined $header->{"Plural-Forms"} 1 19 20
Condition100%3/3
$has_plural and not defined $header->{"Plural-Forms"}
!ll&&!rl&&r
1181
MC/DC100%2/2
$has_plural and not defined $header->{"Plural-Forms"}
$has_plural not defined $header->{"Plural-Forms"}
Truth table100%3/3
A and not(B)
A= $has_pluralB= not defined $header->{"Plural-Forms"}
ABresult
0X0
100
111
154  
155 20 my $data = { '' => $header, %entries };
156 20 ▶ $data = { $opt{domain} => $data } if defined $opt{domain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"domain"} 12 8 20
157 20 return $data;
158 }
159  
160 21 ▶sub data2json {
Subroutine data2json: called CC=2 SCAR=6.9
Pod documented
161 21 my ( $data, %opt ) = @_;
162 21 my $json = JSON::PP->new->canonical->utf8;
163 21 ▶ $json->pretty if $opt{pretty};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"pretty"} 9 12 21
164 21 my $out = $json->encode($data);
165 21 $out =~ s/\n\z//;
166 21 return $out;
167 }
168  
169 24 1;
170  
171 __END__
« lib/Locale/Simple/Scraper.pm ↑ Summary lib/Locale/Simple.pm »