line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
68728
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
2
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Locale::Simple::Scraper; |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
24
|
$Locale::Simple::Scraper::AUTHORITY = 'cpan:GETTY'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
$Locale::Simple::Scraper::VERSION = '0.017'; |
9
|
|
|
|
|
|
|
# ABSTRACT: scraper to find translation tokens in a directory |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
4
|
use Exporter 'import'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
12
|
1
|
|
|
1
|
|
1035
|
use Getopt::Long; |
|
1
|
|
|
|
|
10825
|
|
|
1
|
|
|
|
|
6
|
|
13
|
1
|
|
|
1
|
|
145
|
use File::Find; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
14
|
1
|
|
|
1
|
|
5
|
use Cwd; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
52
|
|
15
|
1
|
|
|
1
|
|
545
|
use Locale::Simple; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
132
|
|
16
|
1
|
|
|
1
|
|
13239
|
use Data::Dumper; |
|
1
|
|
|
|
|
10127
|
|
|
1
|
|
|
|
|
90
|
|
17
|
1
|
|
|
1
|
|
803
|
use Locale::Simple::Scraper::Parser; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
2268
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @EXPORT = qw(scrape); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub scrape { |
22
|
3
|
|
|
3
|
0
|
17891
|
@ARGV = @_; |
23
|
|
|
|
|
|
|
|
24
|
3
|
|
|
|
|
13
|
$| = 1; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Supported filetypes: |
27
|
3
|
|
|
|
|
7
|
my $js_ext = ""; # Javascript |
28
|
3
|
|
|
|
|
9
|
my $pl_ext = ""; # Perl |
29
|
3
|
|
|
|
|
7
|
my $py_ext = ""; # Python |
30
|
3
|
|
|
|
|
9
|
my $tx_ext = ""; # Text::Xslate (Kolon or Metakolon) |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
6
|
my @ignores; |
33
|
|
|
|
|
|
|
my @only; |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
|
|
6
|
my $output = 'po'; |
36
|
3
|
|
|
|
|
6
|
my ($md5, $no_line_numbers); |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
25
|
GetOptions( |
39
|
|
|
|
|
|
|
"js=s" => \$js_ext, |
40
|
|
|
|
|
|
|
"pl=s" => \$pl_ext, |
41
|
|
|
|
|
|
|
"py=s" => \$py_ext, |
42
|
|
|
|
|
|
|
"tx=s" => \$tx_ext, |
43
|
|
|
|
|
|
|
"ignore=s" => \@ignores, |
44
|
|
|
|
|
|
|
"only=s" => \@only, |
45
|
|
|
|
|
|
|
"output=s" => \$output, |
46
|
|
|
|
|
|
|
"md5" => \$md5, |
47
|
|
|
|
|
|
|
"no_line_numbers" => \$no_line_numbers, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# could add Getopt::Long here for override |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
4012
|
my @js = split( ",", $js_ext ); |
53
|
3
|
|
|
|
|
10
|
push @js, 'js'; |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
12
|
my @pl = split( ",", $pl_ext ); |
56
|
3
|
|
|
|
|
8
|
push @pl, 'pl', 'pm', 't'; |
57
|
|
|
|
|
|
|
|
58
|
3
|
|
|
|
|
9
|
my @tx = split( ",", $tx_ext ); |
59
|
3
|
|
|
|
|
6
|
push @tx, 'tx'; |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
9
|
my @py = split( ",", $py_ext ); |
62
|
3
|
|
|
|
|
6
|
push @py, 'py'; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# extension list |
65
|
3
|
|
|
|
|
11
|
my %e = ( |
66
|
11
|
|
|
|
|
94
|
( map { $_ => 'js' } @js ), |
67
|
3
|
|
|
|
|
9
|
( map { $_ => 'pl' } @pl ), |
68
|
3
|
|
|
|
|
23
|
( map { $_ => 'tx' } @tx ), |
69
|
3
|
|
|
|
|
8
|
( map { $_ => 'py' } @py ), |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# functions with count of locale simple with function of parameter |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
# 1 = msgid |
75
|
|
|
|
|
|
|
# 2 = msgid_plural |
76
|
|
|
|
|
|
|
# 3 = msgctxt |
77
|
|
|
|
|
|
|
# 4 = domain |
78
|
|
|
|
|
|
|
# |
79
|
3
|
|
|
|
|
45
|
my %f = ( |
80
|
|
|
|
|
|
|
l => [1], |
81
|
|
|
|
|
|
|
ln => [ 1, 2 ], |
82
|
|
|
|
|
|
|
ld => [ 4, 1 ], |
83
|
|
|
|
|
|
|
lp => [ 3, 1 ], |
84
|
|
|
|
|
|
|
lnp => [ 3, 1, 2 ], |
85
|
|
|
|
|
|
|
ldn => [ 4, 1, 2 ], |
86
|
|
|
|
|
|
|
ldp => [ 4, 3, 1 ], |
87
|
|
|
|
|
|
|
ldnp => [ 4, 3, 1, 2 ], |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
|
90
|
3
|
|
|
|
|
10
|
my %files; |
91
|
|
|
|
|
|
|
|
92
|
3
|
|
|
|
|
27
|
my $dir = getcwd; |
93
|
3
|
|
|
|
|
7
|
my $re_dir = $dir; |
94
|
3
|
|
|
|
|
18
|
$re_dir =~ s/\./\\./g; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
finddepth( |
97
|
|
|
|
|
|
|
sub { |
98
|
390
|
|
|
390
|
|
787
|
my $filename = $File::Find::name; |
99
|
390
|
|
|
|
|
479
|
my $stored_filename = $filename; |
100
|
390
|
50
|
|
|
|
869
|
if ( $md5 ) { |
101
|
0
|
|
|
|
|
0
|
eval { |
102
|
0
|
|
|
|
|
0
|
require Digest::MD5; |
103
|
0
|
|
|
|
|
0
|
Digest::MD5->import( 'md5_hex' ); |
104
|
|
|
|
|
|
|
}; |
105
|
0
|
0
|
|
|
|
0
|
die "This feature requires Digest::MD5" if $@; |
106
|
0
|
|
|
|
|
0
|
$stored_filename = md5_hex( $filename ); |
107
|
|
|
|
|
|
|
} |
108
|
390
|
|
|
|
|
2520
|
$filename =~ s/^$dir\///g; |
109
|
390
|
|
|
|
|
826
|
for ( @ignores ) { |
110
|
1737
|
100
|
|
|
|
28929
|
return if $filename =~ /$_/; |
111
|
|
|
|
|
|
|
} |
112
|
264
|
100
|
|
|
|
640
|
if ( @only ) { |
113
|
176
|
|
|
|
|
221
|
my $found = 0; |
114
|
176
|
|
|
|
|
286
|
for ( @only ) { |
115
|
176
|
100
|
|
|
|
1078
|
$found = 1 if $filename =~ /$_/; |
116
|
|
|
|
|
|
|
} |
117
|
176
|
100
|
|
|
|
7331
|
return unless $found; |
118
|
|
|
|
|
|
|
} |
119
|
90
|
|
|
|
|
336
|
my @fileparts = split( '\.', $File::Find::name ); |
120
|
90
|
|
|
|
|
175
|
my $ext = pop @fileparts; |
121
|
90
|
100
|
|
|
|
223
|
$files{$File::Find::name} = [ $ext, $filename, $stored_filename ] if grep { $ext eq $_ } keys %e; |
|
542
|
|
|
|
|
4752
|
|
122
|
|
|
|
|
|
|
}, |
123
|
3
|
|
|
|
|
829
|
$dir |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
40
|
my @found; |
127
|
3
|
|
|
|
|
29
|
for my $file ( sort keys %files ) { |
128
|
21
|
|
|
|
|
39
|
my ( $ext, $filename, $stored_filename ) = @{ $files{$file} }; |
|
21
|
|
|
|
|
78
|
|
129
|
21
|
|
|
|
|
81
|
my $type = $e{$ext}; |
130
|
21
|
|
|
|
|
720
|
print STDERR $type . " => " . $file . "\n"; |
131
|
21
|
50
|
33
|
|
|
861
|
return if -l $file and not -e readlink( $file ); |
132
|
21
|
|
|
|
|
1299
|
my $parses = Locale::Simple::Scraper::Parser->new( type => $type )->from_file( $file ); |
133
|
57
|
|
|
|
|
248
|
my @file_things = map { |
134
|
21
|
|
|
|
|
50
|
{ |
135
|
57
|
|
|
|
|
70
|
%{ result_from_params( $_->{args}, $f{ $_->{func} } ) }, |
136
|
|
|
|
|
|
|
line => $_->{line}, |
137
|
|
|
|
|
|
|
file => $stored_filename, |
138
|
|
|
|
|
|
|
type => $type, |
139
|
|
|
|
|
|
|
} |
140
|
21
|
|
|
|
|
848
|
} @{$parses}; |
141
|
21
|
|
|
|
|
166
|
push @found, @file_things; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
3
|
50
|
|
|
|
20
|
if ( $output eq 'po' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
145
|
3
|
|
|
|
|
9
|
my %files; |
146
|
|
|
|
|
|
|
my %token; |
147
|
3
|
|
|
|
|
7
|
for ( @found ) { |
148
|
57
|
100
|
|
|
|
335
|
my $key .= defined $_->{domain} ? '"' . $_->{domain} . '"' : 'undef'; |
149
|
57
|
100
|
|
|
|
117
|
$key .= defined $_->{msgctxt} ? '"' . $_->{msgctxt} . '"' : 'undef'; |
150
|
57
|
50
|
|
|
|
165
|
$key .= defined $_->{msgid} ? '"' . $_->{msgid} . '"' : 'undef'; |
151
|
57
|
100
|
|
|
|
136
|
$key .= defined $_->{msgid_plural} ? '"' . $_->{msgid_plural} . '"' : 'undef'; |
152
|
57
|
100
|
|
|
|
187
|
$token{$key} = $_ unless defined $token{$key}; |
153
|
57
|
100
|
|
|
|
219
|
$files{$key} = [] unless defined $files{$key}; |
154
|
57
|
|
|
|
|
70
|
push @{ $files{$key} }, $_->{file} . ':' . $_->{line}; |
|
57
|
|
|
|
|
266
|
|
155
|
|
|
|
|
|
|
} |
156
|
3
|
|
|
|
|
22
|
for my $k ( sort { $a cmp $b } keys %files ) { |
|
100
|
|
|
|
|
114
|
|
157
|
29
|
|
|
|
|
551
|
print "\n"; |
158
|
29
|
50
|
|
|
|
140
|
print "#: " . join( ' ', @{ $files{$k} } ) . "\n" if !$no_line_numbers; |
|
29
|
|
|
|
|
379
|
|
159
|
29
|
|
|
|
|
293
|
print "#, locale-simple-format"; |
160
|
29
|
100
|
|
|
|
99
|
print " " . $token{$k}{domain} if defined $token{$k}{domain}; |
161
|
29
|
|
|
|
|
293
|
print "\n"; |
162
|
29
|
|
|
|
|
56
|
for ( qw( msgctxt msgid msgid_plural ) ) { |
163
|
87
|
100
|
|
|
|
430
|
print $_. ' "' . Locale::Simple::gettext_escape( $token{$k}{$_} ) . '"' . "\n" |
164
|
|
|
|
|
|
|
if defined $token{$k}{$_}; |
165
|
|
|
|
|
|
|
} |
166
|
29
|
100
|
|
|
|
77
|
my $plural_marker = $token{$k}{msgid_plural} ? "[0]" : ""; |
167
|
29
|
|
|
|
|
517
|
print qq[msgstr$plural_marker ""\n]; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
elsif ( $output eq 'perl' ) { |
172
|
0
|
|
|
|
|
0
|
print Dumper \@found; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
elsif ( $output eq 'json' ) { |
175
|
|
|
|
|
|
|
eval { |
176
|
0
|
|
|
|
|
0
|
require JSON; |
177
|
0
|
|
|
|
|
0
|
JSON->import; |
178
|
0
|
|
|
|
|
0
|
print encode_json( \@found ); |
179
|
0
|
0
|
|
|
|
0
|
} or do { |
180
|
0
|
|
|
|
|
0
|
die "You require the module JSON for this output"; |
181
|
|
|
|
|
|
|
}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
elsif ( $output eq 'yaml' ) { |
184
|
|
|
|
|
|
|
eval { |
185
|
0
|
|
|
|
|
0
|
require YAML; |
186
|
0
|
|
|
|
|
0
|
YAML->import; |
187
|
0
|
|
|
|
|
0
|
print Dump( \@found ); |
188
|
0
|
0
|
|
|
|
0
|
} or do { |
189
|
0
|
|
|
|
|
0
|
die "You require the module YAML for this output"; |
190
|
|
|
|
|
|
|
}; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub parse_line { |
195
|
0
|
|
|
0
|
0
|
0
|
my ( $line, $type, $f, @results ) = @_; |
196
|
0
|
0
|
|
|
|
0
|
return if $line =~ /^\s*\#.*/; |
197
|
0
|
|
|
|
|
0
|
for ( keys %{$f} ) { |
|
0
|
|
|
|
|
0
|
|
198
|
0
|
|
|
|
|
0
|
my @args = @{ $f->{$_} }; |
|
0
|
|
|
|
|
0
|
|
199
|
0
|
|
|
|
|
0
|
my $params = get_func_params( $_, $line ); |
200
|
0
|
0
|
|
|
|
0
|
next if !$params; |
201
|
0
|
|
|
|
|
0
|
my $argc = scalar @args; |
202
|
0
|
|
|
|
|
0
|
my ( $remainder, @params ) = parse_params( $params, $type, $argc ); |
203
|
0
|
0
|
|
|
|
0
|
if ( scalar @params == $argc ) { |
204
|
0
|
|
|
|
|
0
|
push @results, result_from_params( \@params, \@args ), parse_line( $remainder, $type, $f ); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
} |
207
|
0
|
|
|
|
|
0
|
return @results; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub result_from_params { |
211
|
57
|
|
|
57
|
0
|
79
|
my ( $params, $args ) = @_; |
212
|
57
|
|
|
|
|
58
|
my %result; |
213
|
57
|
|
|
|
|
69
|
my $pos = 0; |
214
|
57
|
|
|
|
|
55
|
for ( @{$args} ) { |
|
57
|
|
|
|
|
119
|
|
215
|
80
|
100
|
|
|
|
473
|
$result{msgid} = $params->[$pos] if $_ eq 1; |
216
|
80
|
100
|
|
|
|
169
|
$result{msgid_plural} = $params->[$pos] if $_ eq 2; |
217
|
80
|
100
|
|
|
|
141
|
$result{msgctxt} = $params->[$pos] if $_ eq 3; |
218
|
80
|
100
|
|
|
|
138
|
$result{domain} = $params->[$pos] if $_ eq 4; |
219
|
80
|
|
|
|
|
276
|
$pos++; |
220
|
|
|
|
|
|
|
} |
221
|
57
|
|
|
|
|
524
|
return \%result; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub get_func_params { |
225
|
0
|
|
|
0
|
0
|
|
my ( $func, $line ) = @_; |
226
|
0
|
|
|
|
|
|
$line =~ /([^\w]|^)$func\((.*)/; |
227
|
0
|
|
|
|
|
|
return $2; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub parse_params { |
231
|
0
|
|
|
0
|
0
|
|
my ( $params, $type, $argc ) = @_; |
232
|
0
|
|
|
|
|
|
my @chars = split( '', $params ); |
233
|
0
|
|
|
|
|
|
my @args; |
234
|
0
|
|
|
|
|
|
my $arg = ""; |
235
|
0
|
|
|
|
|
|
my $q_state = 0; # 0 = code, 1 = qoute, 2 = double qoute |
236
|
0
|
|
|
|
|
|
my $comma_state = 1; |
237
|
0
|
|
|
|
|
|
while ( defined( my $c = shift @chars ) ) { |
238
|
0
|
0
|
0
|
|
|
|
next if $c =~ /\s/ and !$q_state; |
239
|
0
|
0
|
|
|
|
|
if ( $q_state ) { |
240
|
0
|
0
|
0
|
|
|
|
if ( $c eq '\\' ) { |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
241
|
0
|
|
|
|
|
|
my $esc = shift @chars; |
242
|
0
|
0
|
0
|
|
|
|
if ( $esc eq "'" or $esc eq '"' or $esc eq '\\' ) { |
|
|
|
0
|
|
|
|
|
243
|
0
|
|
|
|
|
|
$arg .= $esc; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
else { |
246
|
0
|
|
|
|
|
|
warn "Unknown escape char '" . $esc . "'"; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
elsif ( ( $c eq "'" and $q_state == 1 ) or ( $c eq '"' and $q_state == 2 ) ) { |
250
|
0
|
|
|
|
|
|
$q_state = 0; |
251
|
0
|
|
|
|
|
|
$comma_state = 0; |
252
|
0
|
|
|
|
|
|
push @args, $arg; |
253
|
0
|
|
|
|
|
|
$arg = ""; |
254
|
0
|
0
|
|
|
|
|
last if scalar @args == $argc; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
else { |
257
|
0
|
|
|
|
|
|
$arg .= $c; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
else { |
261
|
0
|
0
|
0
|
|
|
|
if ( $c eq "'" or $c eq '"' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
262
|
0
|
0
|
|
|
|
|
die "quote found where comma expected: " . $params unless $comma_state; |
263
|
0
|
0
|
|
|
|
|
$q_state = $c eq "'" ? 1 : 2; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
elsif ( $c eq ',' ) { |
266
|
0
|
0
|
|
|
|
|
die "comma found after comma in code: " . $params if $comma_state; |
267
|
0
|
|
|
|
|
|
$comma_state = 1; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
elsif ( $type eq 'js' ) { |
270
|
0
|
|
|
|
|
|
last; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
else { |
273
|
0
|
|
|
|
|
|
last; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
} |
277
|
0
|
|
|
|
|
|
return join( '', @chars ), @args; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
1; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
__END__ |