lib/I18NFool/Extractor.pm

filter: Statementstmt 95.1% 97 / 102 Branchbran 73.3% 22 / 30 Conditioncond 48.3% 15 / 31 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 75.7% 144 / 190 CC 33 SCAR 33.2
CC
33
Cov
82%
_process
38.4
_extract_content_string
4.0
_canonicalize
2.0
CRAP 27.5
↑ Summary lib/I18NFool/ExtractorMerger.pm »
1 package I18NFool::Extractor;
2 5 ▶use MKDoc::XML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Locale::PO;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $Namespace = "http://xml.zope.org/namespaces/i18n";
8 5 our $Prefix = 'i18n';
9 5 our $Domain = 'default';
10 5 our $Results = {};
11  
12  
13 sub process
14 6 ▶{
Subroutine process: called CC=2 SCAR=6.9
Pod undocumented
15 6 my $class = shift;
16 6 my $data = shift;
17
18 6 local $Namespace = $Namespace;
19 6 local $Prefix = $Prefix;
20 6 local $Domain = $Domain;
21 6 local $Results = {};
22
23 6 my @nodes = MKDoc::XML::TreeBuilder->process_data ($data);
24 6 for (@nodes) { $class->_process ($_) }
25 6 return $Results;
26 }
27  
28  
29 sub _process
30 65 ▶{
Subroutine _process: called CC=28 SCAR=36.5
31 65 my $class = shift;
32 65 my $tree = shift;
33 65 ▶ return unless (ref $tree);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree 41 24 65
34  
35 24 local $Prefix = $Prefix;
36 24 local $Domain = $Domain;
37  
38 # process the I18N namespace
39 24 foreach my $key (keys %{$tree})
40 {
41 133 my $value = $tree->{$key};
42 133 ▶ if ($value eq $Namespace)
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $I18NFool::Extractor::Namespace) 9 124 133
43 {
44 9 ▶ next unless ($key =~ /^xmlns\:/);
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^xmlns\:/ 0 9 9
45 9 delete $tree->{$key};
46 9 $Prefix = $key;
47 9 $Prefix =~ s/^xmlns\://;
48 }
49 }
50  
51 # set the current i18n:domain
52 24 ▶ $Domain = delete $tree->{"$Prefix:domain"} || $Domain;
Condition66%2/3
delete $$tree{"$I18NFool::Extractor::Prefix:domain"} || $I18NFool::Extractor::Domain
l!l&&r!l&&!r
6180
MC/DC0%0/2
delete $$tree{"$I18NFool::Extractor::Prefix:domain"} || $I18NFool::Extractor::Domain
delete $$tree{"$I18NFool::Extractor::Prefix:domain"} $I18NFool::Extractor::Domain
Truth table66%2/3
A || B
A= delete $$tree{"$I18NFool::Extractor::Prefix:domain"}B= $I18NFool::Extractor::Domain
ABresult
1X1
011
000
53  
54 24 my $tag = $tree->{_tag};
55 24 ▶ my $attr = { map { /^_/ ? () : ( $_ => $tree->{$_} ) } keys %{$tree} };
Branch: 1 branch
BranchTrueFalseTotal
/^_/ ? : 95 23 118
56 24 ▶ return if ($tag eq '~comment' or $tag eq '~pi' or $tag eq '~declaration');
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "~comment" or $tag eq "~pi" or $tag eq "~declaration" 0 24 24
Condition33%2/6
$tag eq "~comment" or $tag eq "~pi" or $tag eq "~declaration"
l!l&&r!l&&!r
0024
$tag eq "~comment" or $tag eq "~pi"
l!l&&r!l&&!r
0024
MC/DC0%0/3
$tag eq "~comment" or $tag eq "~pi" or $tag eq "~declaration"
$tag eq "~comment" $tag eq "~pi" $tag eq "~declaration"
Truth table25%1/4
A or B or C
A= $tag eq "~comment"B= $tag eq "~pi"C= $tag eq "~declaration"
ABCresult
1XX1
01X1
0011
0000
57
58 # lookup for attributes...
59 24 ▶ $tree->{"$Prefix:attributes"} && do {
Branch: 1 branch
BranchTrueFalseTotal
$$tree{"$I18NFool::Extractor::Prefix:attributes"} and do { my $attributes = $$tree{"$I18NFool::Extractor::Prefix:attributes"}; $attributes =~ s/\s*;\s*$//; $attributes =~ s/^\s*//; my @attributes = split(/\s*\;\s*/, $attributes, 0); foreach my $attribute (@attributes) { my($attribute_name, $translate_id); if ($attribute =~ /\s/) { () = split(/\s+/, $attribute, 2); } else { $attribute_name = $attribute; $translate_id = _canonicalize($tree->{$attribute_name}); } ; $translate_id or next; $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain} ||= {}; my $existing_po = $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}{$translate_id}; my $new_po = "Locale::PO"->new("-msgid", $translate_id, "-msgstr", _canonicalize($tree->{$attribute_name}) || ""); if ($existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}) { print STDERR "String for '${translate_id}' doesn't match:\n" . qq[ old: $existing_po->{"msgstr"}\n] . qq[ new: $new_po->{"msgstr"}\n]; } ; $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}{$translate_id} = $new_po; } } 1 23 24
60 1 my $attributes = $tree->{"$Prefix:attributes"};
61 1 $attributes =~ s/\s*;\s*$//;
62 1 $attributes =~ s/^\s*//;
63 1 my @attributes = split /\s*\;\s*/, $attributes;
64 1 foreach my $attribute (@attributes)
65 {
66 # if we have i18n:attributes="alt alt_text", then the
67 # attribute name is 'alt' and the
68 # translate_id is 'alt_text'
69 4 my ($attribute_name, $translate_id);
70 4 ▶ if ($attribute =~ /\s/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute =~ /\s/) { } 4 0 4
71 {
72 4 ($attribute_name, $translate_id) = split /\s+/, $attribute, 2;
73 }
74  
75 # otherwise, if we have i18n:attributes="alt", then the
76 # attribute name is 'alt' and the
77 # translate_id is $tree->{'alt'}
78 else
79 {
80 0 $attribute_name = $attribute;
81 0 $translate_id = _canonicalize ( $tree->{$attribute_name} );
82 }
83  
84 4 ▶ $translate_id || next;
Branch: 1 branch
BranchTrueFalseTotal
$translate_id or next 4 0 4
85 4 ▶ $Results->{$Domain} ||= {};
Condition100%2/2
$I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain} ||= {}
l!l
31
MC/DC100%1/1
$I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain} ||= {}
$I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}
Truth table100%2/2
A
A= $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}
Aresult
11
00
86  
87 4 my $existing_po = $Results->{$Domain}->{$translate_id};
88 my $new_po = Locale::PO->new (
89 -msgid => $translate_id,
90 4 ▶ -msgstr => _canonicalize ( $tree->{$attribute_name} ) || '',
Condition50%1/2
_canonicalize($tree->{$attribute_name}) || ''
l!l
40
MC/DC0%0/1
_canonicalize($tree->{$attribute_name}) || ''
_canonicalize($tree->{$attribute_name})
Truth table50%1/2
A
A= _canonicalize($tree->{$attribute_name})
Aresult
11
00
91 );
92  
93 4 ▶ if ($existing_po && ($existing_po->{msgstr} ne $new_po->{msgstr}))
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}) 0 4 4
Condition33%1/3
$existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
!ll&&!rl&&r
400
MC/DC0%0/2
$existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
$existing_po $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
Truth table33%1/3
A and B
A= $existing_poB= $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
ABresult
0X0
100
111
94 {
95 0 print STDERR "String for '$translate_id' doesn't match:\n".
96 " old: $existing_po->{msgstr}\n".
97 " new: $new_po->{msgstr}\n"
98 }
99  
100 4 $Results->{$Domain}->{$translate_id} = $new_po;
101 }
102 };
103  
104 # lookup for content...
105 24 ▶ exists $tree->{"$Prefix:translate"} && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $$tree{"$I18NFool::Extractor::Prefix:translate"} and do { my $translate_id; if (defined $$tree{"$I18NFool::Extractor::Prefix:translate"} and $$tree{"$I18NFool::Extractor::Prefix:translate"} ne "") { $translate_id = $$tree{"$I18NFool::Extractor::Prefix:translate"}; } else { $translate_id = _canonicalize(_extract_content_string($tree)); } ; $translate_id or next; $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain} ||= {}; my $existing_po = $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}{$translate_id}; my $new_po = "Locale::PO"->new("-msgid", $translate_id, "-msgstr", _canonicalize(_extract_content_string($tree)) || ""); if ($existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}) { print STDERR "String for '${translate_id}' doesn't match:\n" . qq[ old: $existing_po->{"msgstr"}\n] . qq[ new: $new_po->{"msgstr"}\n]; } ; $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}{$translate_id} = $new_po } 12 12 24
106 12 my ($translate_id);
107  
108 # if we have $Domain:translate="something",
109 # then the translate_id is 'something'
110 12 ▶ if (defined $tree->{"$Prefix:translate"} and $tree->{"$Prefix:translate"} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$tree{"$I18NFool::Extractor::Prefix:translate"} and $$tree{"$I18NFool::Extractor::Prefix:translate"} ne '') { } 0 12 12
Condition33%1/3
defined $$tree{"$I18NFool::Extractor::Prefix:translate"} and $$tree{"$I18NFool::Extractor::Prefix:translate"} ne ""
!ll&&!rl&&r
0120
MC/DC0%0/2
defined $$tree{"$I18NFool::Extractor::Prefix:translate"} and $$tree{"$I18NFool::Extractor::Prefix:translate"} ne ""
defined $$tree{"$I18NFool::Extractor::Prefix:translate"} $$tree{"$I18NFool::Extractor::Prefix:translate"} ne ""
Truth table33%1/3
A and B
A= defined $$tree{"$I18NFool::Extractor::Prefix:translate"}B= $$tree{"$I18NFool::Extractor::Prefix:translate"} ne ""
ABresult
0X0
100
111
111 {
112 0 $translate_id = $tree->{"$Prefix:translate"};
113 }
114  
115 # otherwise, the translate_id has to be computed
116 # from the contents of this node, so that
117 # <div i18n:translate="">Hello, <span i18n:name="user">David</span>, how are you?</div>
118 # becomes 'Hello, ${user}, how are you?'
119 else
120 {
121 12 $translate_id = _canonicalize ( _extract_content_string ($tree) );
122 }
123  
124 12 ▶ $translate_id || next;
Branch: 1 branch
BranchTrueFalseTotal
$translate_id or next 12 0 12
125 12 ▶ $Results->{$Domain} ||= {};
Condition100%2/2
$I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain} ||= {}
l!l
111
MC/DC100%1/1
$I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain} ||= {}
$I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}
Truth table100%2/2
A
A= $I18NFool::Extractor::Results->{$I18NFool::Extractor::Domain}
Aresult
11
00
126  
127 12 my $existing_po = $Results->{$Domain}->{$translate_id};
128 12 ▶ my $new_po = Locale::PO->new (
Condition50%1/2
_canonicalize(_extract_content_string($tree)) || ''
l!l
120
MC/DC0%0/1
_canonicalize(_extract_content_string($tree)) || ''
_canonicalize(_extract_content_string($tree))
Truth table50%1/2
A
A= _canonicalize(_extract_content_string($tree))
Aresult
11
00
129 -msgid => $translate_id,
130 -msgstr => _canonicalize ( _extract_content_string ($tree) ) || '',
131 );
132  
133 12 ▶ if ($existing_po && ($existing_po->{msgstr} ne $new_po->{msgstr}))
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}) 0 12 12
Condition33%1/3
$existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$existing_po and $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
$existing_po $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
Truth table33%1/3
A and B
A= $existing_poB= $existing_po->{"msgstr"} ne $new_po->{"msgstr"}
ABresult
0X0
100
111
134 {
135 0 print STDERR "String for '$translate_id' doesn't match:\n".
136 " old: $existing_po->{msgstr}\n".
137 " new: $new_po->{msgstr}\n"
138 }
139  
140 12 $Results->{$Domain}->{$translate_id} = $new_po;
141 };
142  
143 # I know, I know, the I18N namespace processing is a bit broken...
144 # It should suffice for now
145 24 delete $tree->{"$Prefix:attributes"};
146 24 delete $tree->{"$Prefix:translate"};
147 24 delete $tree->{"$Prefix:name"};
148  
149 # Do the same i18n thing with child nodes, recursively.
150 # for some reason it always makes me think of roller coasters.
151 # Yeeeeeeee!
152 24 ▶ defined $tree->{_content} and do {
Branch: 1 branch
BranchTrueFalseTotal
defined $tree->{"_content"} and do { foreach $_ (@{$$tree{"_content"};}) { $class->_process($_); } } 23 1 24
153 23 for (@{$tree->{_content}}) { $class->_process ($_) }
154 };
155 }
156  
157  
158 sub _canonicalize
159 28 ▶{
Subroutine _canonicalize: called CC=2 SCAR=7.0
160 28 ▶ my $string = shift || '';
Condition50%1/2
shift() || ''
l!l
280
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
161 28 $string =~ s/\r/ /g;
162 28 $string =~ s/\n/ /g;
163 28 $string =~ s/\s+/ /gsm;
164 28 $string =~ s/^ //;
165 28 $string =~ s/ $//;
166 28 return $string;
167 }
168  
169  
170 sub _extract_content_string
171 24 ▶{
Subroutine _extract_content_string: called CC=4 SCAR=13.9
172 24 my $tree = shift;
173 24 my @res = ();
174  
175 24 my $count = 0;
176 24 foreach my $node (@{$tree->{_content}})
177 {
178 28 ▶ ref $node or do {
Branch: 1 branch
BranchTrueFalseTotal
ref $node or do { push @res, $node; next } 2 26 28
179 26 push @res, $node;
180 26 next;
181 };
182
183 2 $count++;
184 2 ▶ my $name = $node->{"$Prefix:name"} || $count;
Condition33%1/3
$$node{"$I18NFool::Extractor::Prefix:name"} || $count
l!l&&r!l&&!r
200
MC/DC0%0/2
$$node{"$I18NFool::Extractor::Prefix:name"} || $count
$$node{"$I18NFool::Extractor::Prefix:name"} $count
Truth table33%1/3
A || B
A= $$node{"$I18NFool::Extractor::Prefix:name"}B= $count
ABresult
1X1
011
000
185 2 push @res, '${' . $name . '}';
186 }
187
188 24 return join '', @res;
189 }
190  
191  
192 5 1;
↑ Summary lib/I18NFool/ExtractorMerger.pm »