lib/MKDoc/Core/Language.pm

filter: Statementstmt 10.8% 10 / 92 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 17.6% 3 / 17 Podpod 75.0% 9 / 12 totaltotal 11.6% 22 / 189 CC 26 SCAR 28.0
CC
26
Cov
7%
_mkd_core_languages
30.0
_mkd_core_languages_rtl
30.0
new
30.0
CRAP 16.4
« lib/MKDoc/Setup/Core.pm ↑ Summary lib/MKDoc/Core.pm »
1 =head1 NAME
2  
3 MKDoc::Core::Language - Language class for the L<MKDoc::Core> framework
4  
5  
6 =head1 SUMMARY
7  
8 The list of languages supported by L<MKDoc::Core> is defined in two config files.
9  
10 These two config files are loaded from (by order of preference):
11  
12 =over
13  
14 =item $ENV{SITE_DIR}/Language/
15  
16 =item $ENV{MKDOC_DIR}/Language/
17  
18 =item @INC/MKDoc/Core/Language/languages/
19  
20 =back
21  
22  
23 The two file are the following:
24  
25 =over
26  
27 =item languages.conf - All the languages to support, including those from right to left
28  
29 =item languages_rtl.conf - Just the languages which are written from right to left
30  
31 =back
32  
33 =cut
34 package MKDoc::Core::Language;
35 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 use overload
38 1 ▶ '""' => \&as_string,
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 'eq' => \&equals;
40  
41 sub as_string
42 0 ▶{
Subroutine as_string: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my $self = shift;
44 0 return $self->code();
45 }
46  
47 sub equals
48 0 ▶{
Subroutine equals: not called CC=1 SCAR=6.9
Pod undocumented
49 0 my $self = shift;
50 0 my $what = shift;
51 0 my $code = $self->code();
52 0 return $code eq $what;
53 }
54  
55 sub _mkd_core_languages
56 0 ▶{
Subroutine _mkd_core_languages: not called CC=5 SCAR=34.0
57 0 ▶ $::MKD_CORE_LANGUAGES ||= do {
Condition0%0/3
$main::MKD_CORE_LANGUAGES ||= do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
l!l&&r!l&&!r
000
MC/DC0%0/2
$main::MKD_CORE_LANGUAGES ||= do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
$main::MKD_CORE_LANGUAGES do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
Truth table0%0/3
A ||= B
A= $main::MKD_CORE_LANGUAGESB= do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
ABresult
1X1
011
000
58  
59 my ($conf) =
60 0 ▶ grep { -e $_ && -f $_ }
Condition0%0/3
-e $_ && -f $_
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_ && -f $_
-e $_ -f $_
Truth table0%0/3
A && B
A= -e $_B= -f $_
ABresult
0X0
100
111
61 0 ▶ map { defined $_ ? "$_/MKDoc/Core/Language/languages.conf" : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
62 0 ($ENV{SITE_DIR}, $ENV{MKDOC_DIR}, @main::INC);
63  
64 0 open FP, "<:utf8", $conf;
65 my %res =
66 map {
67 0 chomp ($_);
68 0 s/#.*//;
69 0 s/^\s+//;
70 0 s/\s+$//;
71 0 ▶ /\S\s+\S/ ? split /\s+/, $_, 2 : ()
Branch: 1 branch
BranchTrueFalseTotal
/\S\s+\S/ ? : 0 0 0
72 } <FP>;
73  
74 0 close FP;
75 0 \%res;
76 };
77  
78 0 $::MKD_CORE_LANGUAGES;
79 }
80  
81  
82  
83 sub _mkd_core_languages_rtl
84 0 ▶{
Subroutine _mkd_core_languages_rtl: not called CC=5 SCAR=34.0
85 0 ▶ $::MKD_CORE_LANGUAGES_RTL ||= do {
Condition0%0/3
$main::MKD_CORE_LANGUAGES_RTL ||= do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages_rtl.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
l!l&&r!l&&!r
000
MC/DC0%0/2
$main::MKD_CORE_LANGUAGES_RTL ||= do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages_rtl.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
$main::MKD_CORE_LANGUAGES_RTL do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages_rtl.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
Truth table0%0/3
A ||= B
A= $main::MKD_CORE_LANGUAGES_RTLB= do { my($conf) = grep({-e $_ and -f $_;} map({defined $_ ? "$_/MKDoc/Core/Language/languages_rtl.conf" : ();} $ENV{"SITE_DIR"}, $ENV{"MKDOC_DIR"}, @INC)); open FP, "<:utf8", $conf; my(%res) = map({chomp $_; s/#.*//; s/^\s+//; s/\s+$//; /\S\s+\S/ ? split(/\s+/, $_, 2) : ();} readline FP); close FP; \%res }
ABresult
1X1
011
000
86  
87 my ($conf) =
88 0 ▶ grep { -e $_ && -f $_ }
Condition0%0/3
-e $_ && -f $_
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_ && -f $_
-e $_ -f $_
Truth table0%0/3
A && B
A= -e $_B= -f $_
ABresult
0X0
100
111
89 0 ▶ map { defined $_ ? "$_/MKDoc/Core/Language/languages_rtl.conf" : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
90 0 ($ENV{SITE_DIR}, $ENV{MKDOC_DIR}, @main::INC);
91  
92 0 open FP, "<:utf8", $conf;
93 my %res =
94 map {
95 0 chomp ($_);
96 0 s/#.*//;
97 0 s/^\s+//;
98 0 s/\s+$//;
99 0 ▶ /\S\s+\S/ ? split /\s+/, $_, 2 : ()
Branch: 1 branch
BranchTrueFalseTotal
/\S\s+\S/ ? : 0 0 0
100 } <FP>;
101  
102 0 close FP;
103 0 \%res;
104 };
105  
106 0 $::MKD_CORE_LANGUAGES_RTL;
107 }
108  
109  
110  
111 =head1 Methods / API
112  
113 =head2 $class->new ($iso_code);
114  
115 Instantiates a new L<MKDoc::Core::Language> object for $iso_code.
116  
117 Returns undef if $iso_code is not defined in the languages.conf file.
118  
119 =cut
120 sub new
121 0 ▶{
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
122 0 my $class = shift;
123 0 my $lang = shift;
124 0 ▶ $::MKD_CORE_LANGUAGES || _mkd_core_languages();
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES or _mkd_core_languages() 0 0 0
125 0 ▶ $::MKD_CORE_LANGUAGES->{$lang} || return;
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES->{$lang} or return 0 0 0
126  
127 0 ▶ $::MKD_CORE_LANGUAGES_OBJ ||= {};
Condition0%0/2
$main::MKD_CORE_LANGUAGES_OBJ ||= {}
l!l
00
MC/DC0%0/1
$main::MKD_CORE_LANGUAGES_OBJ ||= {}
$main::MKD_CORE_LANGUAGES_OBJ
Truth table0%0/2
A
A= $main::MKD_CORE_LANGUAGES_OBJ
Aresult
11
00
128 0 ▶ $::MKD_CORE_LANGUAGES_OBJ->{$lang} ||= bless \$lang, $class;
Condition0%0/2
$main::MKD_CORE_LANGUAGES_OBJ->{$lang} ||= bless(\$lang, $class)
l!l
00
MC/DC0%0/1
$main::MKD_CORE_LANGUAGES_OBJ->{$lang} ||= bless(\$lang, $class)
$main::MKD_CORE_LANGUAGES_OBJ->{$lang}
Truth table0%0/2
A
A= $main::MKD_CORE_LANGUAGES_OBJ->{$lang}
Aresult
11
00
129 0 return $::MKD_CORE_LANGUAGES_OBJ->{$lang};
130 }
131  
132  
133 =head2 $class->as_hash();
134  
135 Returns a hash as follows:
136  
137 ( $iso_code_1 => $label_1,
138 $iso_code_2 => $label_2,
139 $iso_code_3 => $label_3,
140 ...
141 $iso_code_4 => $label_4 )
142  
143 For all languages.
144  
145 =cut
146 sub as_hash
147 0 ▶{
Subroutine as_hash: not called CC=3 SCAR=24.8
Pod documented
148 0 my $class = shift;
149 0 ▶ $::MKD_LANGUAGE_AS_HASH ||= do {
Condition0%0/3
$main::MKD_LANGUAGE_AS_HASH ||= do { my(@list) = $class->code_list; my(%res) = map({my $language = "MKDoc::Core::Language"->new($_); $language->code, $language->label;} @list); \%res }
l!l&&r!l&&!r
000
MC/DC0%0/2
$main::MKD_LANGUAGE_AS_HASH ||= do { my(@list) = $class->code_list; my(%res) = map({my $language = "MKDoc::Core::Language"->new($_); $language->code, $language->label;} @list); \%res }
$main::MKD_LANGUAGE_AS_HASH do { my(@list) = $class->code_list; my(%res) = map({my $language = "MKDoc::Core::Language"->new($_); $language->code, $language->label;} @list); \%res }
Truth table0%0/3
A ||= B
A= $main::MKD_LANGUAGE_AS_HASHB= do { my(@list) = $class->code_list; my(%res) = map({my $language = "MKDoc::Core::Language"->new($_); $language->code, $language->label;} @list); \%res }
ABresult
1X1
011
000
150 0 my @list = $class->code_list();
151 my %res = map {
152 0 my $language = MKDoc::Core::Language->new ($_);
153 0 ( $language->code() => $language->label() );
154 } @list;
155  
156 0 \%res;
157 };
158  
159 0 ▶ return wantarray ? %{$::MKD_LANGUAGE_AS_HASH} : $::MKD_LANGUAGE_AS_HASH;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
160 }
161  
162  
163 =head2 $class->as_hash_rtl();
164  
165 Returns a hash as follows:
166  
167 ( $iso_code_1 => $label_1,
168 $iso_code_2 => $label_2,
169 $iso_code_3 => $label_3,
170 ...
171 $iso_code_4 => $label_4 )
172  
173 For RTL languages.
174  
175 =cut
176 sub as_hash_rtl
177 0 ▶{
Subroutine as_hash_rtl: not called CC=3 SCAR=24.8
Pod documented
178 0 my $class = shift;
179 0 my @list = $class->code_list();
180 my %res = map {
181 0 my $language = MKDoc::Core::Language->new ($_);
182 0 ▶ $language->dir() eq 'rtl' ?
Branch: 1 branch
BranchTrueFalseTotal
$language->dir eq 'rtl' ? : 0 0 0
183 ( $language->code() => $language->label() ) :
184 ()
185 } @list;
186
187 0 ▶ return wantarray ? %res : \%res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
188 }
189  
190  
191 =head2 $self->code();
192  
193 Returns the iso code of a given language object, e.g:
194  
195 my $lang = new MKDoc::Core::Language ('en');
196 print $lang->code(); # should print 'en'
197  
198 =cut
199 sub code
200 0 ▶{
Subroutine code: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202 0 return $$self;
203 }
204  
205  
206  
207 =head2 $self->label();
208  
209 Returns the label associated with a given language object, e.g:
210  
211 my $lang = new MKDoc::Core::Language ('en');
212 print $lang->label(); # should print 'English'
213  
214 =cut
215 sub label
216 0 ▶{
Subroutine label: not called CC=2 SCAR=17.9
Pod documented
217 0 my $self = shift;
218 0 my $code = $self->code;
219 0 ▶ $::MKD_CORE_LANGUAGES || _mkd_core_languages();
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES or _mkd_core_languages() 0 0 0
220 0 $::MKD_CORE_LANGUAGES->{$code};
221 }
222  
223  
224  
225 =head2 $thing->code_list()
226  
227 Returns a list of ISO codes of all languages, sorted by the Unicode
228 value of their associated label.
229  
230 =cut
231 sub code_list
232 0 ▶{
Subroutine code_list: not called CC=3 SCAR=24.8
Pod documented
233 0 ▶ $::MKD_CORE_LANGUAGES || _mkd_core_languages();
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES or _mkd_core_languages() 0 0 0
234 $::MKD_CORE_LANGUAGE_CODE_LIST ||= [
235 0 sort { $::MKD_CORE_LANGUAGES->{$a} cmp $::MKD_CORE_LANGUAGES->{$b} }
236 0 ▶ keys %{$::MKD_CORE_LANGUAGES}
Condition0%0/2
$main::MKD_CORE_LANGUAGE_CODE_LIST ||= [(sort {$main::MKD_CORE_LANGUAGES->{$a} cmp $main::MKD_CORE_LANGUAGES->{$b};} keys %{$main::MKD_CORE_LANGUAGES;})]
l!l
00
MC/DC0%0/1
$main::MKD_CORE_LANGUAGE_CODE_LIST ||= [(sort {$main::MKD_CORE_LANGUAGES->{$a} cmp $main::MKD_CORE_LANGUAGES->{$b};} keys %{$main::MKD_CORE_LANGUAGES;})]
$main::MKD_CORE_LANGUAGE_CODE_LIST
Truth table0%0/2
A
A= $main::MKD_CORE_LANGUAGE_CODE_LIST
Aresult
11
00
237 ];
238  
239 0 return @{$::MKD_CORE_LANGUAGE_CODE_LIST};
240 }
241  
242  
243  
244 =head2 $self->align();
245  
246 In order to do proper multilingual HTML formatting, you need to have a
247 sensible value for the XHTML align="left|right" attribute.
248  
249 If the language is written left to right, this method returns 'left'.
250  
251 If the language is written right to left, this method returns 'right'.
252  
253 =cut
254 sub align
255 0 ▶{
Subroutine align: not called CC=3 SCAR=24.8
Pod documented
256 0 my $self = shift;
257 0 my $code = $self->code;
258 0 ▶ $::MKD_CORE_LANGUAGES_RTL || _mkd_core_languages_rtl();
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES_RTL or _mkd_core_languages_rtl() 0 0 0
259 0 ▶ return $::MKD_CORE_LANGUAGES_RTL->{$code} ? 'right' : 'left';
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES_RTL->{$code} ? : 0 0 0
260 }
261  
262  
263  
264 =head2 $self->align_opposite();
265  
266 In order to do proper multilingual HTML formatting, you need to have a
267 sensible value for the XHTML align="left|right" attribute.
268  
269 This method does the exact opposite as $self->align(), i.e.
270  
271 If the language is written left to right, this method returns 'right'.
272  
273 If the language is written right to left, this method returns 'left'.
274  
275 =cut
276 sub align_opposite
277 0 ▶{
Subroutine align_opposite: not called CC=3 SCAR=24.8
Pod documented
278 0 my $self = shift;
279 0 my $code = $self->code;
280 0 ▶ $::MKD_CORE_LANGUAGES_RTL || _mkd_core_languages_rtl();
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES_RTL or _mkd_core_languages_rtl() 0 0 0
281 0 ▶ return $::MKD_CORE_LANGUAGES_RTL->{$code} ? 'left' : 'right';
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES_RTL->{$code} ? : 0 0 0
282 }
283  
284  
285  
286 =head2 $self->dir()
287  
288 In order to do proper multilingual HTML formatting, you need to have a
289 sensible value for the XHTML dir="ltr|rtl" attribute.
290  
291 If the language is written left to right, this method returns 'ltr'.
292  
293 If the language is written right to left, this method returns 'rtl'.
294  
295 =cut
296 sub dir
297 0 ▶{
Subroutine dir: not called CC=3 SCAR=24.8
Pod documented
298 0 my $self = shift;
299 0 my $code = $self->code;
300 0 ▶ $::MKD_CORE_LANGUAGES_RTL || _mkd_core_languages_rtl();
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES_RTL or _mkd_core_languages_rtl() 0 0 0
301 0 ▶ return $::MKD_CORE_LANGUAGES_RTL->{$code} ? 'rtl' : 'ltr';
Branch: 1 branch
BranchTrueFalseTotal
$main::MKD_CORE_LANGUAGES_RTL->{$code} ? : 0 0 0
302 }
303  
304  
305 sub direction
306 0 ▶{
Subroutine direction: not called CC=1 SCAR=6.9
Pod undocumented
307 0 my $self = shift;
308 0 return $self->dir (@_);
309 }
310  
311  
312 1 1;
313  
314  
315 __END__
« lib/MKDoc/Setup/Core.pm ↑ Summary lib/MKDoc/Core.pm »