lib/Locale/Simple/Scraper.pm

filter: Statementstmt 76.4% 143 / 187 Branchbran 63.5% 61 / 96 Conditioncond 12.5% 3 / 24 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 83.3% 15 / 18 Podpod 33.3% 2 / 6 totaltotal 65.7% 226 / 344 CC 67 SCAR 48.6
CC
67
Cov
67%
parse_params
462.0
parse_line
30.0
scrape
28.0
CRAP 128.4
↑ Summary lib/Locale/Simple/PO2JSON.pm »
1 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Locale::Simple::Scraper;
5 22 our $VERSION = '0.200';
6 # ABSTRACT: scraper to find translation tokens in a directory
7  
8 22 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Locale::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Locale::Simple::Scraper::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 22 our @EXPORT = qw(scrape);
17  
18 22 my %OUTPUT_FORMATS = map { $_ => 1 } qw( po perl json yaml );
19  
20 22 my $USAGE = <<'END';
21 Usage: locale-simple scrape [OPTIONS]
22  
23 Walks the current directory and writes a .pot template to STDOUT; progress and
24 parse problems go to STDERR.
25  
26 Options:
27 --js EXT --pl EXT --py EXT --php EXT --go EXT --rs EXT --tx EXT
28 extra file extensions for a language, comma-separated,
29 repeatable (defaults: js; pl pm t; py; php; go; rs; tx)
30 --ignore REGEX skip files whose path matches, repeatable
31 --only REGEX read only files whose path matches, repeatable
32 --output FORMAT po (default), perl, json or yaml (yaml needs the YAML module)
33 --md5 write MD5 hashes of the file names
34 --no_line_numbers leave out the #: location comments
35 -h, --help show this text
36 END
37  
38 6 ▶sub usage { return $USAGE }
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
39  
40 6 ▶sub _bad_call {
Subroutine _bad_call: called CC=2 SCAR=6.9
41 6 my ($message) = @_;
42 6 ▶ $message .= "\n" unless $message =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\n\z/ 3 3 6
43 6 die bless { message => $message }, 'Locale::Simple::Scraper::BadCall';
44 }
45  
46 70 ▶sub scrape {
Subroutine scrape: called CC=28 SCAR=33.3
Pod documented
47 70 @ARGV = @_;
48  
49 70 $| = 1;
50  
51 # Supported filetypes and the extensions read as them. --js, --pl, ...
52 # add more: repeatable, each a comma-separated list.
53 70 my %defaults = (
54 js => ['js'], # Javascript
55 pl => [ 'pl', 'pm', 't' ], # Perl
56 tx => ['tx'], # Text::Xslate (Kolon or Metakolon)
57 py => ['py'], # Python
58 php => ['php'], # PHP
59 go => ['go'], # Go
60 rs => ['rs'], # Rust
61 );
62 70 my @types = qw( js pl tx py php go rs );
63 70 my %extra = map { $_ => [] } @types;
64  
65 70 my @ignores;
66 my @only;
67  
68 70 my $output = 'po';
69 70 my ($md5, $no_line_numbers, $help);
70  
71 70 my @problems;
72 70 my $parsed = do {
73 70 ▶ local $SIG{__WARN__} = sub { push @problems, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 GetOptions(
75 70 ( map { ( "$_=s" => $extra{$_} ) } @types ),
76 "ignore=s" => \@ignores,
77 "only=s" => \@only,
78 "output=s" => \$output,
79 "md5" => \$md5,
80 "no_line_numbers" => \$no_line_numbers,
81 "help|h" => \$help,
82 );
83 };
84 70 ▶ _bad_call( join '', @problems ) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 3 67 70
85 67 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 6 61 67
86 6 print STDOUT $USAGE;
87 6 return;
88 }
89 61 ▶ _bad_call("Unexpected argument: $ARGV[0]") if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 1 60 61
90 _bad_call("Unknown output format: $output (use po, perl, json or yaml)")
91 60 ▶ unless $OUTPUT_FORMATS{$output};
Branch: 1 branch
BranchTrueFalseTotal
unless $OUTPUT_FORMATS{$output} 2 58 60
92  
93 # extension list
94 58 my %e;
95 58 for my $type ( @types ) {
96 406 $e{$_} = $type for ( map { split /,/ } @{ $extra{$type} } ), @{ $defaults{$type} };
97 }
98  
99 # functions with count of locale simple with function of parameter
100 #
101 # 1 = msgid
102 # 2 = msgid_plural
103 # 3 = msgctxt
104 # 4 = domain
105 #
106 58 my %f = (
107 l => [1],
108 ln => [ 1, 2 ],
109 ld => [ 4, 1 ],
110 lp => [ 3, 1 ],
111 lnp => [ 3, 1, 2 ],
112 ldn => [ 4, 1, 2 ],
113 ldp => [ 4, 3, 1 ],
114 ldnp => [ 4, 3, 1, 2 ],
115  
116 # Deferred-translation markers (see DEFERRED TRANSLATION in
117 # Locale::Simple). Same mapping as their l* twins — identical
118 # .pot output.
119 N_ => [1],
120 Nn_ => [ 1, 2 ],
121 Nd_ => [ 4, 1 ],
122 Np_ => [ 3, 1 ],
123 Nnp_ => [ 3, 1, 2 ],
124 Ndn_ => [ 4, 1, 2 ],
125 Ndp_ => [ 4, 3, 1 ],
126 Ndnp_ => [ 4, 3, 1, 2 ],
127 );
128  
129 58 my %files;
130  
131 58 my $dir = getcwd;
132  
133 finddepth(
134 14975 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
135 14975 my $filename = $File::Find::name;
136 14975 $filename =~ s/^\Q$dir\E\///;
137 14975 my $stored_filename = $filename;
138 14975 ▶ if ( $md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5) 6 14969 14975
139 6 eval {
140 6 require Digest::MD5;
141 6 Digest::MD5->import( 'md5_hex' );
142 };
143 6 ▶ die "This feature requires Digest::MD5" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
144 6 $stored_filename = md5_hex( $filename );
145 }
146 14975 for ( @ignores ) {
147 19758 ▶ return if $filename =~ /$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /$_/ 759 18999 19758
148 }
149 14216 ▶ if ( @only ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@only) 14025 191 14216
150 14025 my $found = 0;
151 14025 for ( @only ) {
152 14025 ▶ $found = 1 if $filename =~ /$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /$_/ 51 13974 14025
153 }
154 14025 ▶ return unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 13974 51 14025
155 }
156 242 my @fileparts = split( '\.', $File::Find::name );
157 242 my $ext = pop @fileparts;
158 242 ▶ $files{$File::Find::name} = [ $ext, $filename, $stored_filename ] if grep { $ext eq $_ } keys %e;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$ext eq $_;} keys %e 114 128 242
159 },
160 58 $dir
161 );
162  
163 58 my @found;
164 58 for my $file ( sort keys %files ) {
165 114 my ( $ext, $filename, $stored_filename ) = @{ $files{$file} };
166 114 my $type = $e{$ext};
167 114 print STDERR $type . " => " . $file . "\n";
168 114 ▶ next if -l $file and not -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -l $file and not -e $file 2 112 114
Condition100%3/3
-l $file and not -e $file
!ll&&!rl&&r
11112
MC/DC100%2/2
-l $file and not -e $file
-l $file not -e $file
Truth table100%3/3
A and not(B)
A= -l $fileB= not -e $file
ABresult
0X0
100
111
169 112 my $parses = Locale::Simple::Scraper::Parser->new( type => $type )->from_file( $file );
170 my @file_things = map {
171 {
172 693 %{ result_from_params( $_->{args}, $f{ $_->{func} } ) },
173 line => $_->{line},
174 693 file => $stored_filename,
175 type => $type,
176 }
177 112 } @{$parses};
178 112 push @found, @file_things;
179 }
180  
181 58 ▶ if ( $output eq 'po' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($output eq 'po') { } 24 34 58
elsif ($output eq 'perl') { } 30 4 34
elsif ($output eq 'json') { } 2 2 4
elsif ($output eq 'yaml') { } 2 0 2
182 24 my %files;
183 my %token;
184 24 for ( @found ) {
185 412 ▶ my $key .= defined $_->{domain} ? '"' . $_->{domain} . '"' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'domain'} ? : 157 255 412
186 412 ▶ $key .= defined $_->{msgctxt} ? '"' . $_->{msgctxt} . '"' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'msgctxt'} ? : 156 256 412
187 412 ▶ $key .= defined $_->{msgid} ? '"' . $_->{msgid} . '"' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'msgid'} ? : 412 0 412
188 412 ▶ $key .= defined $_->{msgid_plural} ? '"' . $_->{msgid_plural} . '"' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'msgid_plural'} ? : 179 233 412
189 412 ▶ $token{$key} = $_ unless defined $token{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token{$key} 289 123 412
190 412 ▶ $files{$key} = [] unless defined $files{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $files{$key} 289 123 412
191 412 push @{ $files{$key} }, $_->{file} . ':' . $_->{line};
192 }
193 24 for my $k ( sort { $a cmp $b } keys %files ) {
194 289 print "\n";
195 289 ▶ print "#: " . join( ' ', @{ $files{$k} } ) . "\n" if !$no_line_numbers;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_line_numbers 152 137 289
196 289 print "#, locale-simple-format";
197 289 ▶ print " " . $token{$k}{domain} if defined $token{$k}{domain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $token{$k}{"domain"} 116 173 289
198 289 print "\n";
199 289 for ( qw( msgctxt msgid msgid_plural ) ) {
200 print $_. ' "' . Locale::Simple::gettext_escape( $token{$k}{$_} ) . '"' . "\n"
201 867 ▶ if defined $token{$k}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $token{$k}{$_} 523 344 867
202 }
203 289 ▶ my $plural_marker = $token{$k}{msgid_plural} ? "[0]" : "";
Branch: 1 branch
BranchTrueFalseTotal
$token{$k}{'msgid_plural'} ? : 119 170 289
204 289 print qq[msgstr$plural_marker ""\n];
205  
206 }
207 }
208 elsif ( $output eq 'perl' ) {
209 30 local $Data::Dumper::Sortkeys = 1;
210 30 print Dumper \@found;
211 }
212 elsif ( $output eq 'json' ) {
213 2 require JSON::PP;
214 2 print JSON::PP->new->canonical->encode( \@found );
215 }
216 elsif ( $output eq 'yaml' ) {
217 2 ▶ eval { require YAML; 1 } or die "--output yaml needs the YAML module\n";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require YAML; 1 } } or die "--output yaml needs the YAML module\n" 1 1 2
218 1 print YAML::Dump( \@found );
219 }
220 }
221  
222 0 ▶sub parse_line {
Subroutine parse_line: not called CC=5 SCAR=34.0
Pod undocumented
223 0 my ( $line, $type, $f, @results ) = @_;
224 0 ▶ return if $line =~ /^\s*\#.*/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\#.*/ 0 0 0
225 0 for ( keys %{$f} ) {
226 0 my @args = @{ $f->{$_} };
227 0 my $params = get_func_params( $_, $line );
228 0 ▶ next if !$params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 0 0 0
229 0 my $argc = scalar @args;
230 0 my ( $remainder, @params ) = parse_params( $params, $type, $argc );
231 0 ▶ if ( scalar @params == $argc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == $argc) 0 0 0
232 0 push @results, result_from_params( \@params, \@args ), parse_line( $remainder, $type, $f );
233 }
234 }
235 0 return @results;
236 }
237  
238 693 ▶sub result_from_params {
Subroutine result_from_params: called CC=6 SCAR=17.9
Pod undocumented
239 693 my ( $params, $args ) = @_;
240 693 my %result;
241 693 my $pos = 0;
242 693 for ( @{$args} ) {
243 1350 ▶ $result{msgid} = $params->[$pos] if $_ eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 1 693 657 1350
244 1350 ▶ $result{msgid_plural} = $params->[$pos] if $_ eq 2;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 2 253 1097 1350
245 1350 ▶ $result{msgctxt} = $params->[$pos] if $_ eq 3;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 3 207 1143 1350
246 1350 ▶ $result{domain} = $params->[$pos] if $_ eq 4;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq 4 197 1153 1350
247 1350 $pos++;
248 }
249 693 return \%result;
250 }
251  
252 0 ▶sub get_func_params {
Subroutine get_func_params: not called CC=1 SCAR=6.9
Pod undocumented
253 0 my ( $func, $line ) = @_;
254 0 $line =~ /([^\w]|^)$func\((.*)/;
255 0 return $2;
256 }
257  
258  
259 0 ▶sub parse_params {
Subroutine parse_params: not called CC=21 SCAR=61.4
Pod undocumented
260 0 my ( $params, $type, $argc ) = @_;
261 0 my @chars = split( '', $params );
262 0 my @args;
263 0 my $arg = "";
264 0 my $q_state = 0; # 0 = code, 1 = qoute, 2 = double qoute
265 0 my $comma_state = 1;
266 0 ▶ while ( defined( my $c = shift @chars ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = shift @chars)) 0 0 0
267 0 ▶ next if $c =~ /\s/ and !$q_state;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\s/ and not $q_state 0 0 0
Condition0%0/3
$c =~ /\s/ and not $q_state
!ll&&!rl&&r
000
MC/DC0%0/2
$c =~ /\s/ and not $q_state
$c =~ /\s/ not $q_state
Truth table0%0/3
A and not(B)
A= $c =~ /\s/B= not $q_state
ABresult
0X0
100
111
268 0 ▶ if ( $q_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q_state) { } 0 0 0
269 0 ▶ if ( $c eq '\\' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '\\') { } 0 0 0
elsif ($c eq q['] and $q_state == 1 or $c eq '"' and $q_state == 2) { } 0 0 0
270 0 my $esc = shift @chars;
271 0 ▶ if ( $esc eq "'" or $esc eq '"' or $esc eq '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($esc eq q['] or $esc eq '"' or $esc eq '\\') { } 0 0 0
Condition0%0/6
$esc eq "'" or $esc eq "\"" or $esc eq "\\"
l!l&&r!l&&!r
000
$esc eq "'" or $esc eq "\""
l!l&&r!l&&!r
000
MC/DC0%0/3
$esc eq "'" or $esc eq "\"" or $esc eq "\\"
$esc eq "'" $esc eq "\"" $esc eq "\\"
Truth table0%0/4
A or B or C
A= $esc eq "'"B= $esc eq "\""C= $esc eq "\\"
ABCresult
1XX1
01X1
0011
0000
272 0 $arg .= $esc;
273 }
274 else {
275 0 warn "Unknown escape char '" . $esc . "'";
276 }
277 }
278 ▶ elsif ( ( $c eq "'" and $q_state == 1 ) or ( $c eq '"' and $q_state == 2 ) ) {
Condition0%0/9
$c eq "'" and $q_state == 1 or $c eq "\"" and $q_state == 2
l!l&&r!l&&!r
000
$c eq "'" and $q_state == 1
!ll&&!rl&&r
000
$c eq "\"" and $q_state == 2
!ll&&!rl&&r
000
MC/DC0%0/4
$c eq "'" and $q_state == 1 or $c eq "\"" and $q_state == 2
$c eq "'" $q_state == 1 $c eq "\"" $q_state == 2
Truth table0%0/7
A and B or C and D
A= $c eq "'"B= $q_state == 1C= $c eq "\""D= $q_state == 2
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
279 0 $q_state = 0;
280 0 $comma_state = 0;
281 0 push @args, $arg;
282 0 $arg = "";
283 0 ▶ last if scalar @args == $argc;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args == $argc 0 0 0
284 }
285 else {
286 0 $arg .= $c;
287 }
288 }
289 else {
290 0 ▶ if ( $c eq "'" or $c eq '"' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq q['] or $c eq '"') { } 0 0 0
elsif ($c eq ',') { } 0 0 0
elsif ($type eq 'js') { } 0 0 0
Condition0%0/3
$c eq "'" or $c eq "\""
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "'" or $c eq "\""
$c eq "'" $c eq "\""
Truth table0%0/3
A or B
A= $c eq "'"B= $c eq "\""
ABresult
1X1
011
000
291 0 ▶ die "quote found where comma expected: " . $params unless $comma_state;
Branch: 1 branch
BranchTrueFalseTotal
unless $comma_state 0 0 0
292 0 ▶ $q_state = $c eq "'" ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$c eq q['] ? : 0 0 0
293 }
294 elsif ( $c eq ',' ) {
295 0 ▶ die "comma found after comma in code: " . $params if $comma_state;
Branch: 1 branch
BranchTrueFalseTotal
if $comma_state 0 0 0
296 0 $comma_state = 1;
297 }
298 elsif ( $type eq 'js' ) {
299 0 last;
300 }
301 else {
302 0 last;
303 }
304 }
305 }
306 0 return join( '', @chars ), @args;
307 }
308  
309 22 1;
310  
311 __END__
↑ Summary lib/Locale/Simple/PO2JSON.pm »