Qgoda/Util/Translate.pm

filter: Statementstmt 66.4% 95 / 143 Branchbran 30.0% 9 / 30 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 3 totaltotal 59.2% 122 / 206 CC 26 SCAR 38.7
CC
26
Cov
59%
get_masters
156.0
translate_front_matter
8.4
__translate_property
8.1
CRAP 47.8
« Template/Plugin/Qgoda.pm ↑ Summary Qgoda/Command/Po.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Util::Translate;
20 6 $Qgoda::Util::Translate::VERSION = 'v0.9.2';
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 6 ▶use Locale::TextDomain qw(qgoda);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use Locale::gettext_dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Template::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use File::Globstar qw(globstar);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 6 ▶use Qgoda::Util qw(empty merge_data flatten2hash front_matter safe_yaml_load);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use Qgoda::Splitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 @EXPORT_OK = qw(translate_front_matter translate_body get_masters);
37  
38 sub __translate_property {
39 2 ▶ my ($property, $value, $textdomain) = @_;
Subroutine __translate_property: called CC=5 SCAR=20.9
40  
41 # This only works on the top-level for one level!
42 2 ▶ if (ref $value && 'ARRAY' eq reftype $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and "ARRAY" eq builtin::reftype($value)) 0 2 2
Condition33%1/3
ref $value and "ARRAY" eq builtin::reftype($value)
!ll&&!rl&&r
200
MC/DC0%0/2
ref $value and "ARRAY" eq builtin::reftype($value)
ref $value "ARRAY" eq builtin::reftype($value)
Truth table33%1/3
A and B
A= ref $valueB= "ARRAY" eq builtin::reftype($value)
ABresult
0X0
100
111
43 0 my @trans;
44 0 ▶ for (my $i = 0; $i < @$value; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$value) 0 0 0
45 0 my $ctx = "$property.$i";
46 0 push @trans, Locale::gettext_dumb::dpgettext($textdomain, $ctx,
47 $value->[$i]);
48 0 Encode::_utf8_on($trans[$i]);
49 }
50 0 return \@trans;
51 }
52  
53 2 my $hash = flatten2hash {$property => $value};
54  
55 2 my $stash = Template::Stash->new({});
56 2 foreach my $key (keys %$hash) {
57 $stash->set($key,
58 Locale::gettext_dumb::dpgettext($textdomain, $key,
59 2 $hash->{$key}));
60 }
61  
62 2 my $translated = $stash->get($property);
63  
64 2 Encode::_utf8_on($translated);
65  
66 2 return $translated;
67 }
68  
69 sub translate_front_matter {
70 1 ▶ my ($asset, $preserve) = @_;
Subroutine translate_front_matter: called CC=8 SCAR=21.3
Pod undocumented
71  
72 1 my $qgoda = Qgoda->new;
73 1 my $config = $qgoda->config;
74 1 my $relpath = $asset->{relpath};
75  
76 1 my $lingua = $asset->{lingua};
77 1 ▶ die __"lingua not set" if empty $lingua;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($lingua) 0 1 1
78  
79 1 my $master_relpath = $asset->{master};
80 1 $master_relpath =~ s{^/}{};
81 1 ▶ $master_relpath = 'index.html' if empty $master_relpath;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($master_relpath) 0 1 1
82  
83 1 my $front_matter = front_matter $master_relpath;
84 1 ▶ die __x("cannot read front matter from master '{path}': {error}",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $front_matter 0 1 1
85 path => $master_relpath, error => $!)
86 if !defined $front_matter;
87
88 1 my $master = dclone safe_yaml_load $front_matter;
89  
90 1 my @translate;
91 1 ▶ if (!empty $asset->{translate}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&empty($asset->{"translate"})) 1 0 1
92 1 ▶ if (ref $asset->{translate} && 'ARRAY' eq reftype $asset->{translate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $asset->{'translate'} and 'ARRAY' eq builtin::reftype($asset->{'translate'})) { } 1 0 1
Condition33%1/3
ref $asset->{"translate"} and "ARRAY" eq builtin::reftype($asset->{"translate"})
!ll&&!rl&&r
001
MC/DC0%0/2
ref $asset->{"translate"} and "ARRAY" eq builtin::reftype($asset->{"translate"})
ref $asset->{"translate"} "ARRAY" eq builtin::reftype($asset->{"translate"})
Truth table33%1/3
A and B
A= ref $asset->{"translate"}B= "ARRAY" eq builtin::reftype($asset->{"translate"})
ABresult
0X0
100
111
93 1 @translate = @{$asset->{translate}};
94 } else {
95 0 @translate = ($asset->{translate});
96 }
97 }
98  
99 1 my $textdomain = $config->{po}->{textdomain};
100  
101 # Merge the master data unconditionally into the asset.
102 1 merge_data $asset, $master;
103  
104 # And now translate the translatable properties.
105 1 local %ENV = %ENV;
106 1 $ENV{LANGUAGE} = $lingua;
107 1 foreach my $prop (@translate) {
108 2 $asset->{$prop} = __translate_property($prop, $master->{$prop},
109 $textdomain);
110 }
111 1 merge_data $master, $asset;
112  
113 # Now overwrite the explicitely set properties.
114 1 merge_data $master, $preserve;
115  
116 1 %{$_[0]} = %$master;
117  
118 1 return 1;
119 }
120  
121 sub translate_body {
122 1 ▶ my ($asset) = @_;
Subroutine translate_body: called CC=4 SCAR=13.9
Pod undocumented
123  
124 1 my $qgoda = Qgoda->new;
125 1 my $config = $qgoda->config;
126 1 my $relpath = $asset->{relpath};
127  
128 1 my $lingua = $asset->{lingua};
129 1 ▶ die __"lingua not set" if empty $lingua;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($lingua) 0 1 1
130  
131 1 my $master_relpath = $asset->{master};
132 1 $master_relpath =~ s{^/}{};
133 1 ▶ $master_relpath = 'index.html' if empty $master_relpath;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($master_relpath) 0 1 1
134  
135 1 my $front_matter = front_matter $master_relpath;
136 1 ▶ die __x("cannot read front matter from master '{path}': {error}")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $front_matter 0 1 1
137 if !defined $front_matter;
138
139 1 my $splitter = Qgoda::Splitter->new($master_relpath);
140  
141 1 local %ENV = %ENV;
142 1 $ENV{LANGUAGE} = $lingua;
143  
144 1 my $textdomain = $config->{po}->{textdomain};
145  
146 my $translate = sub {
147 5 ▶ my ($msgid) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
148  
149 # FIXME! Strip off comment!
150 5 return Locale::gettext_dumb::dgettext($textdomain, $msgid);
151 1 };
152  
153 1 return $splitter->reassemble($translate);
154 }
155  
156 sub get_masters {
157 0 ▶ my ($self) = @_;
Subroutine get_masters: not called CC=12 SCAR=50.5
Pod undocumented
158  
159 0 my $qgoda = Qgoda->new;
160 0 $qgoda->initPlugins;
161 0 $qgoda->initAnalyzers;
162 0 my $config = $qgoda->config;
163 0 my $logger = $qgoda->logger;
164 0 my $site = Qgoda::Site->new($config);
165 0 $qgoda->setSite($site);
166 0 $qgoda->scan($site, 'just find');
167  
168 0 my %mdextra;
169 my %mddelete;
170  
171 0 ▶ my $mdextra = $config->{po}->{mdextra} || [];
Condition0%0/2
$config->{'po'}{'mdextra'} || []
l!l
00
MC/DC0%0/1
$config->{'po'}{'mdextra'} || []
$config->{'po'}{'mdextra'}
Truth table0%0/2
A
A= $config->{'po'}{'mdextra'}
Aresult
11
00
172 0 foreach my $pattern (@$mdextra) {
173 0 my $negated = $pattern =~ s/^!//;
174 # Force path to be relative.
175 0 $pattern =~ s{^/+}{};
176 0 my @files = globstar $pattern;
177 0 foreach my $found (@files) {
178 0 ▶ if (-d $found) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $found) { } 0 0 0
elsif ($negated) { } 0 0 0
179 # Skip directory.
180 } elsif ($negated) {
181 0 $logger->debug(__x("removing markdown file '{filename}'",
182 filename => $found));
183 0 delete $mdextra{$found};
184 0 $mddelete{$found} = 1;
185 } else {
186 0 $logger->debug(__x("adding markdown file '{filename}'",
187 filename => $found));
188 0 $mdextra{$found} = 1;
189 0 delete $mddelete{$found};
190 }
191 }
192 }
193  
194 0 foreach my $delete (keys %mddelete) {
195 0 my $path = File::Spec->rel2abs($delete);
196 0 ▶ my $asset = $site->{assets}->{$path} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $asset = $site->{"assets"}{$path} or next 0 0 0
197 0 $site->removeAsset($asset);
198 }
199  
200 0 foreach my $relpath (keys %mdextra) {
201 0 my $path = File::Spec->abs2rel($relpath);
202 0 ▶ next if $site->{assets}->{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $site->{"assets"}{$path} 0 0 0
203  
204 0 $logger->debug(__x("creating asset object for '{filename}'",
205 filename => $relpath));
206 0 $site->addAsset(Qgoda::Asset->new($path, $relpath));
207 }
208  
209 0 $qgoda->analyze($site);
210  
211 0 my %masters;
212 0 foreach my $asset (values %{$site->{assets}}) {
213 0 ▶ next if empty $asset->{master};
Branch: 1 branch
BranchTrueFalseTotal
if &empty($asset->{"master"}) 0 0 0
214  
215 0 my $master = $asset->{master};
216 0 $master =~ s{^/+}{};
217 0 $masters{$master}->{$asset->getPath} = $asset;
218 }
219  
220 0 return %masters;
221 }
222  
223  
224 6 1;
« Template/Plugin/Qgoda.pm ↑ Summary Qgoda/Command/Po.pm »