Qgoda/Util.pm

filter: Statementstmt 72.5% 254 / 350 Branchbran 57.7% 104 / 180 Conditioncond 33.3% 11 / 33 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 75.9% 41 / 54 Podpod 0.0% 0 / 31 totaltotal 61.9% 413 / 667 CC 106 SCAR 36.1
CC
106
Cov
66%
tokenize
194.4
__ANON__
20.0
__ANON__
13.0
CRAP 37.1
« Qgoda/Init.pm ↑ Summary Qgoda/Migrator/Jekyll.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;
20 14 $Qgoda::Util::VERSION = 'v0.9.2';
21 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 14 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 14 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 14 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶use Locale::TextDomain qw(qgoda);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 14 ▶use Scalar::Util qw(reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 14 ▶use Encode 2.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 14 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 14 ▶use Data::Walk 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 14 ▶use Storable qw(freeze);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 14 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 14 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 14 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 14 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 14 @EXPORT_OK = qw(empty read_file write_file yaml_error front_matter lowercase
38 expand_perl_format read_body merge_data interpolate
39 normalize_path strip_suffix
40 perl_identifier perl_class class2module
41 slugify html_escape unmarkup globstar trim
42 flatten2hash is_archive archive_extender collect_defaults
43 canonical purify safe_yaml_load
44 escape_link blength);
45  
46 sub js_unescape($);
47 sub tokenize($$);
48 sub evaluate($$);
49 sub lookup($$);
50 sub _globstar($;$);
51  
52 14 my $unsafe_for_links = "^A-Za-z0-9\-\._~/";
53  
54 sub empty(;$) {
55 173 ▶ my ($what) = @_;
Subroutine empty: called CC=4 SCAR=13.9
Pod undocumented
56  
57 173 ▶ $what = $_ if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 24 149 173
58  
59 173 ▶ return if defined $what && length $what;
Branch: 1 branch
BranchTrueFalseTotal
if defined $what and length $what 113 60 173
Condition100%3/3
defined $what and length $what
!ll&&!rl&&r
4911113
MC/DC100%2/2
defined $what and length $what
defined $what length $what
Truth table100%3/3
A and B
A= defined $whatB= length $what
ABresult
0X0
100
111
60  
61 60 return 1;
62 }
63  
64 sub read_file($) {
65 5 ▶ my ($filename) = @_;
Subroutine read_file: called CC=2 SCAR=7.0
Pod undocumented
66  
67 5 my $fh = IO::File->new;
68 5 ▶ open $fh, "<", $filename or return;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or return 0 5 5
69  
70 5 local $/;
71 5 my $data = <$fh>;
72 5 $fh->close;
73  
74 5 return $data;
75 }
76  
77 sub front_matter($) {
78 7 ▶ my ($filename) = @_;
Subroutine front_matter: called CC=6 SCAR=18.4
Pod undocumented
79  
80 7 my $fh = IO::File->new;
81 7 ▶ open $fh, "<", $filename or return;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or return 0 7 7
82  
83 7 undef $!;
84 7 my $first_line = <$fh>;
85 7 ▶ return if empty $first_line;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($first_line) 0 7 7
86 7 ▶ return if $first_line !~ /---[ \t]*\n$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_line =~ /---[ \t]*\n$/o 0 7 7
87  
88 7 my $front_matter = '';
89 7 while (1) {
90 34 my $line = <$fh>;
91 34 ▶ return if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 34 34
92 34 ▶ return $front_matter if $line =~ /---[ \t]*\n$/o;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /---[ \t]*\n$/o 7 27 34
93 27 $front_matter .= $line;
94 }
95  
96 0 return;
97 }
98  
99 sub read_body($$) {
100 4 ▶ my ($filename, $placeholder) = @_;
Subroutine read_body: called CC=6 SCAR=18.1
Pod undocumented
101  
102 4 my $fh = IO::File->new;
103 4 ▶ open $fh, "<", $filename or return;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or return 0 4 4
104  
105 4 undef $!;
106 4 my $first_line = <$fh>;
107 4 ▶ return if empty $first_line;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($first_line) 0 4 4
108 4 ▶ return if $first_line !~ /---[ \t]*\n$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_line =~ /---[ \t]*\n$/o 0 4 4
109 4 my $lines = 1;
110  
111 4 while (1) {
112 17 ++$lines;
113 17 my $line = <$fh>;
114 17 ▶ return if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 17 17
115 17 ▶ last if $line =~ /---[ \t]*\n$/o;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /---[ \t]*\n$/o 4 13 17
116 }
117  
118 4 local $/;
119  
120 4 my $front_matter = "$placeholder" x $lines;
121  
122 4 return $front_matter . <$fh>;
123 }
124  
125 sub write_file($$) {
126 11 ▶ my ($path, $data) = @_;
Subroutine write_file: called CC=6 SCAR=18.7
Pod undocumented
127  
128 11 my (undef, $directory) = fileparse $path;
129 11 ▶ make_path $directory unless -e $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $directory 7 4 11
130  
131 11 my $octets;
132 11 ▶ if (Encode::is_utf8($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($data)) { } 3 8 11
133 my $handle_malformed = sub {
134 0 ▶ my $replacement = sprintf "{{+%04X}}", shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 warn "malformed multi-byte sequence, search for '$replacement' in output file\n";
136  
137 0 return $replacement;
138 3 };
139 3 $octets = Encode::encode('UTF-8', $data, $handle_malformed);
140 } else {
141 8 $octets = $data;
142 }
143  
144 11 ▶ open my $fh, ">", $path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or return 0 11 11
145  
146 11 ▶ $fh->print($octets) or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($octets) or return 0 11 11
147 11 ▶ $fh->close or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or return 0 11 11
148  
149 11 return 1;
150 }
151  
152 sub yaml_error {
153 0 ▶ my ($filename, $error) = @_;
Subroutine yaml_error: not called CC=1 SCAR=6.9
Pod undocumented
154  
155 0 my @lines = split /\n/, $error;
156 0 pop @lines;
157 0 return "$filename: " . join "\n", @lines;
158 }
159  
160 sub lowercase($) {
161 0 ▶ my ($str) = @_;
Subroutine lowercase: not called CC=1 SCAR=6.9
Pod undocumented
162  
163 0 return lc $str;
164 }
165  
166 sub merge_data {
167 12 ▶ my ($data, $overlay) = @_;
Subroutine merge_data: called CC=3 SCAR=11.0
Pod undocumented
168  
169 # Return $overlay if it is of a different type than $data.
170 my $equal_ref = sub {
171 25 ▶ my ($x, $y) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
172  
173 25 ▶ return if !ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 10 15 25
174 15 ▶ return if !ref $y;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 15 15
175  
176 15 my $ref_x = reftype $x;
177 15 my $ref_y = reftype $y;
178  
179 15 return $ref_x eq $ref_y;
180 12 };
181  
182 12 ▶ return $overlay if !$equal_ref->($overlay, $data);
Branch: 1 branch
BranchTrueFalseTotal
unless &$equal_ref($overlay, $data) 0 12 12
183 12 ▶ return $overlay if 'ARRAY' eq reftype $overlay;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq builtin::reftype($overlay) 0 12 12
184  
185 12 my $merger;
186 $merger = sub {
187 13 ▶ my ($d, $o) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.5
188  
189 13 foreach my $key (keys %$d) {
190 58 ▶ if (exists $o->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{$key}) 13 45 58
191 13 ▶ if (!$equal_ref->($d->{$key}, $o->{$key})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not &$equal_ref($d->{$key}, $o->{$key})) { } 10 3 13
elsif (UNIVERSAL::isa($d->{$key}, 'HASH')) { } 1 2 3
192 10 eval { $d->{$key} = $o->{$key}; };
193 } elsif (UNIVERSAL::isa($d->{$key}, 'HASH')) {
194 1 $merger->($d->{$key}, $o->{$key});
195 } else {
196 2 $d->{$key} = $o->{$key};
197 }
198 }
199 }
200 13 foreach my $key (keys %$o) {
201 47 ▶ if (!exists $d->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $d->{$key}) 34 13 47
202 34 $d->{$key} = $o->{$key};
203 }
204 }
205 12 };
206  
207 12 $merger->($data, $overlay);
208  
209 12 return $data;
210 }
211  
212 sub interpolate($$) {
213 9 ▶ my ($string, $data) = @_;
Subroutine interpolate: called CC=7 SCAR=21.0
Pod undocumented
214  
215 9 ▶ $data ||= {};
Condition50%1/2
$data ||= {}
l!l
90
MC/DC0%0/1
$data ||= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
216  
217 9 my $type = reftype $data;
218 9 ▶ if ($type ne 'ARRAY' && $type ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "ARRAY" and $type ne "HASH") 0 9 9
Condition33%1/3
$type ne "ARRAY" and $type ne "HASH"
!ll&&!rl&&r
090
MC/DC0%0/2
$type ne "ARRAY" and $type ne "HASH"
$type ne "ARRAY" $type ne "HASH"
Truth table33%1/3
A and B
A= $type ne "ARRAY"B= $type ne "HASH"
ABresult
0X0
100
111
219 0 $type = 'HASH';
220 0 $data = {};
221 }
222  
223 9 my $result = '';
224 9 ▶ while ($string =~ s/^([^\{]*)\{//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^([^\{]*)\{//) 6 9 15
225 6 $result .= $1;
226  
227 6 my ($remainder, @tokens) = tokenize $string, $type;
228  
229 # Syntax errors can be handled in different ways.
230 # You can handle it gracefully and either leave
231 # everything uninterpolated, or you could replace the
232 # faulty string with the emtpy string or you can throw an
233 # exception. We just throw an exception.
234 6 ▶ die "syntax error before: '$remainder'\n" if !@tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 6 6
235  
236 6 my $value = evaluate \@tokens, $data;
237 6 ▶ $result .= $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 6 0 6
238 6 $string = $remainder;
239 }
240  
241 9 return $result . $string;
242 }
243  
244 sub normalize_path($;$) {
245 9 ▶ my ($dir, $trailing_slash) = @_;
Subroutine normalize_path: called CC=2 SCAR=6.9
Pod undocumented
246  
247 9 $dir =~ s{[\\/]+}{/}g;
248 9 ▶ $dir =~ s{/$}{} unless $trailing_slash;
Branch: 1 branch
BranchTrueFalseTotal
unless $trailing_slash 6 3 9
249  
250 9 return $dir;
251 }
252  
253 sub strip_suffix($) {
254 16 ▶ my ($filename) = @_;
Subroutine strip_suffix: called CC=3 SCAR=11.0
Pod undocumented
255  
256 16 my @parts = split /\./, $filename;
257 16 my @suffixes;
258  
259 16 ▶ while (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 32 13 45
260 32 ▶ last if $parts[-1] =~ /[^a-zA-Z0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $parts[-1] =~ /[^a-zA-Z0-9]/ 3 29 32
261 29 unshift @suffixes, pop @parts
262 }
263  
264 16 my $basename = join '.', @parts;
265  
266 16 return $basename, grep { /./ } @suffixes;
267 }
268  
269 ##############################################################################
270 # The methods below are not exported.
271 ##############################################################################
272  
273 sub tokenize($$) {
274 6 ▶ my ($string, $type) = @_;
Subroutine tokenize: called CC=26 SCAR=52.7
Pod undocumented
275  
276 6 my @tokens;
277  
278 6 my $depth = 0;
279 6 while (1) {
280 12 $string =~ s/^[ \t\r\n]+//;
281 12 ▶ last if !length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 0 12 12
282 12 ▶ last if $string =~ s/^\}//;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/^\}// 6 6 12
283  
284 6 ▶ my $last = @tokens ? $tokens[-1]->[0] : '[';
Branch: 1 branch
BranchTrueFalseTotal
@tokens ? : 0 6 6
285  
286 6 ▶ if ($last eq '.') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last eq '.') { } 0 6 6
elsif ($last eq 'v' or $last eq ']') { } 0 6 6
elsif ($last eq '[') { } 6 0 6
287 # Only variables are allowed but they are interpreted as
288 # a quoted string. We will repair that later, however.
289 0 ▶ return $string unless $string =~ s/^([^\[\]\}\.]+)//;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ s/^([^\[\]\}\.]+)// 0 0 0
290 0 push @tokens, ['v', $1];
291 ▶ } elsif ($last eq 'v' || $last eq ']') {
Condition33%1/3
$last eq "v" or $last eq "]"
l!l&&r!l&&!r
006
MC/DC0%0/2
$last eq "v" or $last eq "]"
$last eq "v" $last eq "]"
Truth table33%1/3
A or B
A= $last eq "v"B= $last eq "]"
ABresult
1X1
011
000
292 # Only brackets or a dot are allowed. Everything else is a
293 # syntax error.
294 0 ▶ return $string unless $string =~ s/^([\[\]\.])//;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ s/^([\[\]\.])// 0 0 0
295  
296 0 ▶ if ('[' eq $1) {
Branch: 2 branches
BranchTrueFalseTotal
if ('[' eq $1) { } 0 0 0
elsif (']' eq $1) { } 0 0 0
297 0 ++$depth;
298 0 push @tokens, ['[' => ''];
299 } elsif (']' eq $1) {
300 0 --$depth;
301 0 ▶ return "]$string" if $depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth < 0 0 0 0
302 0 push @tokens, [']' => ''];
303 } else {
304 # A dot.
305 0 push @tokens, ['.', ''];
306 }
307 } elsif ($last eq '[') {
308 # At the beginning or after an opening bracket only quoted
309 # strings are allowed. Everything but a quoted string is
310 # treated as a variable.
311 6 ▶ if ($string =~ s/^(["'])([^\\\1]*(?:\\.[^\\\1]*)*)\1//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($string =~ s/^(["'])([^\\\1]*(?:\\.[^\\\1]*)*)\1//) { } 0 6 6
elsif ($string =~ s/^([^\[\]\}\.]+)//) { } 6 0 6
elsif (not @tokens and $string =~ s/^\[//) { } 0 0 0
312 0 push @tokens, ['q', $2];
313 } elsif ($string =~ s/^([^\[\]\}\.]+)//) {
314 6 push @tokens, ['v', $1];
315 ▶ } elsif (!@tokens && $string =~ s/^\[//) {
Condition0%0/3
not @tokens and $string =~ s/^\[//
!ll&&!rl&&r
000
MC/DC0%0/2
not @tokens and $string =~ s/^\[//
not @tokens $string =~ s/^\[//
Truth table0%0/3
not(A) and B
A= not @tokensB= $string =~ s/^\[//
ABresult
0X0
100
111
316 # Special case. We want to allow starting an expression
317 # with an opening bracket so that you can write something
318 # like ["key with special characters"].
319 0 push @tokens, ['[', ''];
320 } else {
321 0 return $string;
322 }
323 } else {
324 # The last token was a quoted string (because all other
325 # possibilities are handled above. The only legal token after
326 # a quoted string is the closing bracket.
327 0 ▶ return $string unless $string =~ s/^]//;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ s/^]// 0 0 0
328 0 push @tokens, [']', ''];
329 }
330 }
331  
332 # Bracket not closed.
333 6 ▶ return '}' if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 6 6
334  
335 # We may have a trailing dot in our expression. We check that now
336 # and change the type of "variables" following a dot to a quoted
337 # string.
338 #
339 # We also must repair the type for "variables" that look like numbers
340 # and are enclosed in angle brackets. Only in this case they are
341 # treated like numbers. And numbers are the same as quoted strings
342 # for our purposes.
343 # If they are exactly between two brackets they are numbers, otherwise
344 # we try them as variables.
345 6 ▶ for (my $i = 0; $i < @tokens; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tokens) 6 6 12
346 6 ▶ if ('.' eq $tokens[$i]->[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if ('.' eq $tokens[$i][0]) { } 0 6 6
elsif ('[' eq $tokens[$i][0] and 'v' eq $tokens[$i + 1][0] and ']' eq $tokens[$i + 2][0] and $tokens[$i + 1][1] =~ /^[-+]?(?:0|[1-9][0-9]*)$/) { } 0 12 12
347 0 ▶ return $string if $i >= $#tokens;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $#tokens 0 0 0
348 0 $tokens[++$i]->[0] = 'q';
349 ▶ } elsif ('[' eq $tokens[$i]->[0]
Condition33%3/9
"[" eq $tokens[$i][0] and "v" eq $tokens[$i + 1][0] and "]" eq $tokens[$i + 2][0] and $tokens[$i + 1][1] =~ /^[-+]?(?:0|[1-9][0-9]*)$/
!ll&&!rl&&r
600
"[" eq $tokens[$i][0] and "v" eq $tokens[$i + 1][0] and "]" eq $tokens[$i + 2][0]
!ll&&!rl&&r
600
"[" eq $tokens[$i][0] and "v" eq $tokens[$i + 1][0]
!ll&&!rl&&r
600
MC/DC0%0/4
"[" eq $tokens[$i][0] and "v" eq $tokens[$i + 1][0] and "]" eq $tokens[$i + 2][0] and $tokens[$i + 1][1] =~ /^[-+]?(?:0|[1-9][0-9]*)$/
"[" eq $tokens[$i][0] "v" eq $tokens[$i + 1][0] "]" eq $tokens[$i + 2][0] $tokens[$i + 1][1] =~ /^[-+]?(?:0|[1-9][0-9]*)$/
Truth table20%1/5
A and B and C and D
A= "[" eq $tokens[$i][0]B= "v" eq $tokens[$i + 1][0]C= "]" eq $tokens[$i + 2][0]D= $tokens[$i + 1][1] =~ /^[-+]?(?:0|[1-9][0-9]*)$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
350 && 'v' eq $tokens[$i + 1]->[0]
351 && ']' eq $tokens[$i + 2]->[0]
352 && $tokens[$i + 1]->[1] =~ /^[-+]?(?:0|[1-9][0-9]*)$/) {
353 # Change the type to a quoted string.
354 0 $tokens[$i + 1]->[0] = 'q';
355  
356 # And shorten the loop again.
357 0 $i = $i + 2;
358 }
359 }
360  
361 6 return $string, @tokens;
362 }
363  
364 sub evaluate($$) {
365 6 ▶ my ($tokens, $data) = @_;
Subroutine evaluate: called CC=7 SCAR=25.2
Pod undocumented
366  
367 6 my $cursor = $data;
368  
369 6 ▶ while (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 6 6 12
370 6 my $token = shift @$tokens;
371 6 my ($toktype, $value) = @$token;
372  
373 6 ▶ if ('[' eq $toktype) {
Branch: 5 branches
BranchTrueFalseTotal
if ('[' eq $toktype) { } 0 6 6
elsif (']' eq $toktype) { } 0 6 6
elsif ('.' eq $toktype) { } 0 6 6
elsif ('v' eq $toktype) { } 6 0 6
elsif ('q' eq $toktype) { } 0 0 0
374 # We have to recurse.
375 0 my $key = evaluate $tokens, $data;
376 0 $cursor = lookup $cursor, $key;
377 } elsif (']' eq $toktype) {
378 0 return $cursor;
379 } elsif ('.' eq $toktype) {
380 0 $token = shift @$tokens;
381 0 $cursor = lookup $cursor, $token->[1];
382 } elsif ('v' eq $toktype) {
383 6 $cursor = lookup $cursor, $value;
384 } elsif ('q' eq $toktype) {
385 0 $cursor = $value;
386 } else {
387 0 die "unknown token type '$toktype'";
388 }
389 }
390  
391 6 return $cursor;
392 }
393  
394 sub lookup($$) {
395 6 ▶ my ($data, $key) = @_;
Subroutine lookup: called CC=3 SCAR=14.2
Pod undocumented
396  
397 6 my $type = reftype $data;
398 6 ▶ if ('HASH' eq $type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq $type) { } 6 0 6
elsif ('ARRAY' eq $type) { } 0 0 0
399 6 return $data->{$key};
400 } elsif ('ARRAY' eq $type) {
401 0 return $data->[$key];
402 } else {
403 0 return;
404 }
405 }
406  
407 sub js_unescape($) {
408 10 ▶ my ($string) = @_;
Subroutine js_unescape: called CC=7 SCAR=21.3
Pod undocumented
409  
410 10 my %escapes = (
411 "\n" => '',
412 0 => "\000", # Note that octal escapes are not supported!
413 b => "\x08",
414 f => "\x0c",
415 n => "\x0a",
416 r => "\x0d",
417 t => "\x09",
418 v => "\x0b",
419 "'" => "'",
420 '"' => '"',
421 '\\' => '\\',
422 );
423  
424 10 $string =~ s/
425 \\
426 (
427 x[0-9a-fA-F]{2}
428 |
429 u[0-9a-fA-F]{4}
430 |
431 u\{[0-9a-fA-F]+\}
432 |
433 .
434 )
435 /
436 15 ▶ if (exists $escapes{$1}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $escapes{$1}) { } 8 7 15
elsif (1 == length $1) { } 3 4 7
elsif ('x' eq substr($1, 0, 1)) { } 3 1 4
elsif ('u' eq substr($1, 0, 1)) { } 1 0 1
437 8 $escapes{$1}
438 } elsif (1 == length $1) {
439 3 $1;
440 } elsif ('x' eq substr $1, 0, 1) {
441 3 chr oct '0' . $1;
442 } elsif ('u' eq substr $1, 0, 1) {
443 1 ▶ if ('u{' eq substr $1, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ('u{' eq substr($1, 0, 2)) { } 0 1 1
444 0 my $code = substr $1, 0, 2;
445 0 $code =~ s{^0+}{};
446 0 ▶ $code ||= '0';
Condition0%0/2
$code ||= 0
l!l
00
MC/DC0%0/1
$code ||= 0
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
447 0 chr oct '0x' . $code;
448 } else {
449 1 chr oct '0x' . substr $1, 1;
450 }
451 }
452 /xegs;
453  
454 10 return $string;
455 }
456  
457 sub perl_identifier($) {
458 5 ▶ my ($name) = @_;
Subroutine perl_identifier: called CC=1 SCAR=0.0
Pod undocumented
459  
460 5 return $name =~ /^[_a-zA-Z][_0-9a-zA-Z]*$/o;
461 }
462  
463 sub perl_class($) {
464 28 ▶ my ($name) = @_;
Subroutine perl_class: called CC=1 SCAR=0.0
Pod undocumented
465  
466 28 return $name =~ /^[_a-zA-Z][_0-9a-zA-Z]*(?:::[_a-zA-Z][_0-9a-zA-Z]*)*$/o;
467 }
468  
469 sub class2module($) {
470 16 ▶ my ($class) = @_;
Subroutine class2module: called CC=1 SCAR=0.0
Pod undocumented
471  
472 16 $class =~ s{(?:::|')}{/}g;
473  
474 16 return $class . '.pm';
475 }
476  
477 sub slugify($;$) {
478 3 ▶ my ($string, $locale) = @_;
Subroutine slugify: called CC=2 SCAR=7.0
Pod undocumented
479  
480 3 ▶ return '' if !defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 3 3
481  
482 3 Encode::_utf8_on($string);
483  
484 3 require Text::Unidecode;
485 3 my $slug = lc Text::Unidecode::unidecode($string);
486  
487 # We only allow alphanumerical characters, the dot, the hyphen and the underscore.
488 # Everything else gets converted into hyphens, and sequences of hyphens
489 # are condensed into one.
490 3 $slug =~ s/[\x00-\x2c\x2f\x3a-\x5e\x60\x7b-\x7f]/-/g;
491 3 $slug =~ s/--+/-/g;
492  
493 3 return $slug;
494 }
495  
496 sub html_escape($) {
497 0 ▶ my ($string) = @_;
Subroutine html_escape: not called CC=2 SCAR=17.9
Pod undocumented
498  
499 0 ▶ return '' if !defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
500  
501 0 my %escapes = (
502 '"' => '&#34;',
503 "&" => '&#38;',
504 "'" => '&#39;',
505 "<" => '&#60;',
506 ">" => '&#62;',
507 );
508  
509 0 $string =~ s/(["&'<>])/$escapes{$1}/gs;
510  
511 0 return $string;
512 }
513  
514 sub unmarkup($) {
515 4 ▶ my ($string) = @_;
Subroutine unmarkup: called CC=2 SCAR=6.9
Pod undocumented
516  
517 4 ▶ return '' if !defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 4 4
518  
519 4 require HTML::Parser;
520  
521 4 my $escaped = '';
522 my $text_handler = sub {
523 10 ▶ my ($string) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
524  
525 10 $escaped .= $string;
526  
527 4 };
528  
529 4 my $parser = HTML::Parser->new(api_version => 3,
530 text_h => [$text_handler, 'text'],
531 marked_sections => 1);
532  
533 4 $parser->parse($string);
534 4 $parser->eof;
535  
536 4 return $escaped;
537 }
538  
539 sub trim($) {
540 0 ▶ my ($string) = @_;
Subroutine trim: not called CC=1 SCAR=6.9
Pod undocumented
541  
542 0 $string =~ s{^[ \x09-\x0d]+}{};
543 0 $string =~ s{[ \x09-\x0d]+$}{};
544  
545 0 return $string;
546 }
547  
548 sub flatten2hash {
549 4 ▶ my ($data) = @_;
Subroutine flatten2hash: called CC=1 SCAR=0.0
Pod undocumented
550  
551 4 my @path;
552 my @types;
553 4 my %flat;
554  
555 my $postprocess = sub {
556 # Remove the last path component.
557 17 ▶ pop @path;
Subroutine __ANON__: called CC=1 SCAR=0.0
558 17 pop @types;
559 4 };
560  
561 # The wanted function for Data::Walk.
562 my $wanted = sub {
563 56 ▶ ++$path[-1] if 'a' eq $types[-1];
Subroutine __ANON__: called CC=13 SCAR=25.7
Branch: 1 branch
BranchTrueFalseTotal
if "a" eq $types[-1] 8 48 56
564  
565 56 ▶ my $reftype = reftype $_ || '';
Condition100%2/2
builtin::reftype($_) || ''
l!l
1739
MC/DC100%1/1
builtin::reftype($_) || ''
builtin::reftype($_)
Truth table100%2/2
A
A= builtin::reftype($_)
Aresult
11
00
566  
567 56 ▶ if ('HASH' eq $reftype) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq $reftype) { } 12 44 56
elsif ('ARRAY' eq $reftype) { } 5 39 44
568 12 ▶ if (!keys %$_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$_) 2 10 12
569 2 $flat{join '.', @path} = $_;
570 }
571 12 push @types, 'h';
572 12 push @path, '';
573 } elsif ('ARRAY' eq $reftype) {
574 5 ▶ if (!@$_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$_) 2 3 5
575 2 $flat{join '.', @path} = $_;
576 }
577 5 push @types, 'a';
578 5 push @path, -1;
579 } else {
580 39 $reftype = '';
581 }
582  
583 56 ▶ if ('HASH' eq $Data::Walk::type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq $Data::Walk::type) { } 47 9 56
elsif (not $reftype) { } 7 2 9
584 47 ▶ if (defined $Data::Walk::key) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Data::Walk::key) { } 23 24 47
elsif (not ref $_) { } 22 2 24
585 # Value.
586 23 ▶ if (!ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_) 10 13 23
587 10 $flat{join '.', @path} = $_;
588 }
589 } elsif (!ref $_) {
590 # Key.
591 22 ▶ if (/\./) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\./) { } 0 22 22
592 0 $path[-1] = 'INVALID';
593 } else {
594 22 $path[-1] = $_;
595 }
596 }
597 } elsif (!$reftype) {
598 7 $flat{join '.', @path} = $_;
599 }
600 4 };
601  
602 4 walk {
603 wanted => $wanted,
604 postprocess => $postprocess,
605 }, $data;
606  
607 4 return \%flat;
608 }
609  
610 # Should better be called looks_like_archive.
611 14 my @archive_types = (
612 'tar',
613 'tar.gz', 'tgz',
614 'zip',
615 'tar.bz2', 'tbz',
616 'tar.xz', 'txz'
617 );
618 14 my $archive_re = join '|', map { quotemeta } @archive_types;
619  
620 sub is_archive($) {
621 3 ▶ my ($path) = @_;
Subroutine is_archive: called CC=2 SCAR=6.9
Pod undocumented
622  
623 3 ▶ return if $path !~ /\.(?:$archive_re)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /\.(?:$archive_re)$/i 2 1 3
624  
625 1 return 1;
626 }
627  
628 sub archive_extender($) {
629 0 ▶ my ($path) = @_;
Subroutine archive_extender: not called CC=2 SCAR=17.9
Pod undocumented
630  
631 0 ▶ return if $path !~ /(\.(?:$archive_re))/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /(\.(?:$archive_re))/i 0 0 0
632  
633 0 return lc $1;
634 }
635  
636 sub collect_defaults($$) {
637 6 ▶ my ($path, $rules) = @_;
Subroutine collect_defaults: called CC=3 SCAR=11.0
Pod undocumented
638  
639 6 my $vars = {};
640 6 foreach my $rule (@$rules) {
641 3 my ($matcher, $values) = @$rule;
642  
643 3 ▶ merge_data $vars, $values if $matcher->matchInclude($path);
Branch: 1 branch
BranchTrueFalseTotal
if $matcher->matchInclude($path) 2 1 3
644 }
645  
646 6 return $vars;
647 }
648  
649 sub canonical {
650 0 ▶ my ($obj) = @_;
Subroutine canonical: not called CC=1 SCAR=6.9
Pod undocumented
651  
652 0 local $Storable::canonical = 1;
653  
654 0 return freeze $obj;
655 }
656  
657 sub purify {
658 0 ▶ my ($data) = @_;
Subroutine purify: not called CC=3 SCAR=24.8
Pod undocumented
659  
660 0 my $type = reftype $data;
661 0 ▶ die "only hashes and arrays supported"
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "HASH" and $type ne "ARRAY" 0 0 0
Condition0%0/3
$type ne "HASH" and $type ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$type ne "HASH" and $type ne "ARRAY"
$type ne "HASH" $type ne "ARRAY"
Truth table0%0/3
A and B
A= $type ne "HASH"B= $type ne "ARRAY"
ABresult
0X0
100
111
662 if ($type ne 'HASH' && $type ne 'ARRAY');
663 0 my @stack = ([$type, []]);
664  
665 my $preprocess = sub {
666 0 ▶ if ('HASH' eq $Data::Walk::type) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq $Data::Walk::type) { } 0 0 0
667 0 push @stack, [HASH => []];
668 } else {
669 0 push @stack, [ARRAY => []];
670 }
671  
672 0 return @_;
673 0 };
674  
675 my $postprocess = sub {
676 0 ▶ my $item = pop @stack;
Subroutine __ANON__: not called CC=2 SCAR=17.9
677 0 my ($type, $store) = @$item;
678 0 ▶ if ('HASH' eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq $type) 0 0 0
679 0 $store = {@$store};
680 }
681 0 my $current = $stack[-1]->[1];
682 0 push @$current, $store;
683 0 };
684  
685 my $wanted = sub {
686 0 ▶ if (ref $_) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 0 0
687 0 my $reftype = reftype $_;
688 0 ▶ if ('HASH' eq $reftype || 'ARRAY' eq $reftype) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq $reftype or "ARRAY" eq $reftype) 0 0 0
Condition0%0/3
"HASH" eq $reftype or "ARRAY" eq $reftype
l!l&&r!l&&!r
000
MC/DC0%0/2
"HASH" eq $reftype or "ARRAY" eq $reftype
"HASH" eq $reftype "ARRAY" eq $reftype
Truth table0%0/3
A or B
A= "HASH" eq $reftypeB= "ARRAY" eq $reftype
ABresult
1X1
011
000
689 0 return;
690 }
691 }
692  
693 0 my $store = $stack[-1]->[1];
694 0 push @$store, "$_";
695 0 };
696  
697 0 walk { wanted => $wanted, preprocess => $preprocess,
698 postprocess => $postprocess }, $data;
699  
700 0 my $item = pop @stack;
701 0 $type = $item->[0];
702  
703 0 return $item->[1]->[0];
704 }
705  
706 # Maybe we want to remove the utf-8 flag from the returned yaml data again ...
707 sub safe_yaml_load {
708 14 ▶ my ($yaml) = @_;
Subroutine safe_yaml_load: called CC=1 SCAR=0.0
Pod undocumented
709  
710 14 return YAML::XS::Load($yaml);
711 }
712  
713 sub escape_link {
714 0 ▶ my $link = shift;
Subroutine escape_link: not called CC=2 SCAR=17.9
Pod undocumented
715 0 ▶ $link = '' if empty $link;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($link) 0 0 0
716  
717 0 return uri_escape_utf8 $link, $unsafe_for_links;
718 }
719  
720 sub blength {
721 0 ▶ my ($scalar) = @_;
Subroutine blength: not called CC=2 SCAR=17.9
Pod undocumented
722  
723 0 ▶ return length $scalar if !Encode::is_utf8($scalar);
Branch: 1 branch
BranchTrueFalseTotal
unless &Encode::is_utf8($scalar) 0 0 0
724  
725 0 Encode::_utf8_off($scalar);
726 0 my $blength = length $scalar;
727 0 Encode::_utf8_on($scalar);
728  
729 0 return $blength;
730 }
731  
732 14 1;
« Qgoda/Init.pm ↑ Summary Qgoda/Migrator/Jekyll.pm »