QBit/Gettext/Extract/Lang.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 65 CC 5 SCAR 29.7
CC
5
Cov
0%
extract_from_file
30.0
_lines_count
2.0
BEGIN
2.0
CRAP 19.5
1 package QBit::Gettext::Extract::Lang;
2 $QBit::Gettext::Extract::Lang::VERSION = '0.005';
3 use qbit;
4  
5 use base qw(QBit::Class);
6  
7 __PACKAGE__->abstract_methods(qw(clean));
8  
9 our %FUNCS = (
10 gettext => [qw(message)],
11 ngettext => [qw(message plural)],
12 pgettext => [qw(context message)],
13 npgettext => [qw(context message plural)],
14 d_gettext => [qw(message)],
15 d_ngettext => [qw(message plural)],
16 d_pgettext => [qw(context message)],
17 d_npgettext => [qw(context message plural)],
18 );
19  
20 our $RE_eol = qr/\r\n|\n|\r/;
21  
22 my $RE_q_str = qr/(?:\')(?:[^\\\']*(?:\\.[^\\\']*)*)(?:\')/;
23 my $RE_qq_str = qr/(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")/;
24 my $RE_bq_str = qr/(?:\`)(?:[^\\\`]*(?:\\.[^\\\`]*)*)(?:\`)/;
25  
26 our $RE_quoted_str = qr/$RE_q_str|$RE_qq_str|$RE_bq_str/;
27  
28 sub extract_from_file {
29 my ($self, $filename, $po) = @_;
30  
31 my $text = $self->clean(readfile($filename));
32  
33 my $func_re_str = '(?:^|\W)(' . join('|', sort {length($b) <=> length($a)} keys(%FUNCS)) . ')(\W*?\()';
34  
35 my $line = 1;
36  
37 while ($text =~ /\G(.*?(?:$RE_quoted_str|$func_re_str))/sgc) {
38 my ($match, $func, $after_func) = ($1, $2, $3);
39  
40 $line += _lines_count($match);
41  
42 unless ($func) {
43 $line += _lines_count($after_func);
44 next;
45 }
46  
47 my $params_re_str = '\G(\s*' . join('\s*,\s*', map {"($RE_quoted_str)"} 1 .. @{$FUNCS{$func}}) . ')';
48 my ($params_match, @params) = $text =~ /$params_re_str/sgc;
49  
50 # Dequoting params
51 foreach (@params) {
52 if (s/^'|'$//sg) {
53 s/\\'/'/sg;
54 } elsif (s/^"|"$//sg) {
55 s/\\"/"/sg;
56 }
57 }
58  
59 if (@params) {
60 $po->add_message(
61 filename => $filename,
62 line => $line,
63 map {$FUNCS{$func}->[$_] => $params[$_]} 0 .. @{$FUNCS{$func}} - 1
64 );
65 } else {
66 print STDERR gettext('Cannot find valid parameters for function "%s" in file "%s" at line %s', $func,
67 $filename, $line), "\n";
68 }
69  
70 $line += _lines_count($after_func) + _lines_count($params_match);
71 }
72 }
73  
74 sub _lines_count {
75 return my $tmp = () = ($_[0] || '') =~ /\r\n|\r|\n/g;
76 }
77  
78 TRUE;