Qgoda/Analyzer.pm

filter: Statementstmt 87.2% 103 / 118 Branchbran 50.0% 22 / 44 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 3 totaltotal 73.3% 143 / 195 CC 26 SCAR 21.9
CC
26
Cov
75%
analyze
13.7
__fillPathInformation
12.7
__fillMeta
9.9
CRAP 8.9
« Qgoda/Processor/HTMLFilter.pm ↑ Summary Qgoda/PluginLoader/TT2/Filter.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::Analyzer;
20 6 $Qgoda::Analyzer::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 Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 6 use Qgoda::Util qw(empty yaml_error front_matter lowercase collect_defaults
30 normalize_path strip_suffix merge_data slugify
31 6 ▶ safe_yaml_load);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use Qgoda::Util::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use Qgoda::Util::Translate qw(translate_front_matter);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub new {
36 2 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
37  
38 2 bless {}, $class;
39 }
40  
41 sub setup {
42 2 ▶ shift;
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
43 }
44  
45 sub analyze {
46 3 ▶ my ($self, $asset, $site, $precious) = @_;
Subroutine analyze: called CC=10 SCAR=26.2
Pod undocumented
47  
48 3 my $qgoda = Qgoda->new;
49  
50 3 my $path = $asset->getPath;
51 3 ▶ stat $path or die __x("cannot read '{filename}': {error}",
Branch: 1 branch
BranchTrueFalseTotal
stat $path or die &__x("cannot read '{filename}': {error}", "filename", $path, "error", $!) 0 3 3
52 filename => $path, error => $!);
53 3 my $config = $qgoda->config;
54 3 my $meta = collect_defaults $asset->getRelpath, $config->{defaults};
55  
56 3 my $front_matter = front_matter $path;
57 3 my $front_matter_data;
58 3 ▶ if (defined $front_matter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $front_matter) { } 3 0 3
59 3 $front_matter_data = safe_yaml_load $front_matter;
60 } else {
61 0 $front_matter_data->{raw} = 1;
62 }
63  
64 3 merge_data $meta, $front_matter_data;
65  
66 3 delete $meta->{path};
67 3 delete $meta->{relpath};
68 3 delete $meta->{reldir};
69  
70 3 merge_data $asset, $meta;
71 3 ▶ if (!empty $asset->{master}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&empty($asset->{"master"})) 1 2 3
72 1 translate_front_matter $asset, $front_matter_data;
73 }
74  
75 3 ▶ merge_data $asset, $precious if $precious;
Branch: 1 branch
BranchTrueFalseTotal
if $precious 0 3 3
76  
77 3 ▶ $self->__fillMeta($asset) if !$asset->{raw};
Branch: 1 branch
BranchTrueFalseTotal
unless $asset->{"raw"} 3 0 3
78  
79 3 my %build_options = $qgoda->buildOptions;
80 3 ▶ if ($asset->{draft} && !$build_options{drafts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asset->{"draft"} and not $build_options{"drafts"}) 0 3 3
Condition33%1/3
$asset->{"draft"} and not $build_options{"drafts"}
!ll&&!rl&&r
300
MC/DC0%0/2
$asset->{"draft"} and not $build_options{"drafts"}
$asset->{"draft"} not $build_options{"drafts"}
Truth table33%1/3
A and not(B)
A= $asset->{"draft"}B= not $build_options{"drafts"}
ABresult
0X0
100
111
81 0 my $logger = $qgoda->logger;
82 0 $logger->debug(__x("skipping draft '{relpath}'",
83 relpath => $asset->getRelpath));
84 0 $site->removeAsset($asset);
85 }
86  
87 3 my $now = time;
88 3 ▶ if ($asset->{date} > $now && !$build_options{future}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asset->{"date"} > $now and not $build_options{"future"}) 0 3 3
Condition33%1/3
$asset->{"date"} > $now and not $build_options{"future"}
!ll&&!rl&&r
300
MC/DC0%0/2
$asset->{"date"} > $now and not $build_options{"future"}
$asset->{"date"} > $now not $build_options{"future"}
Truth table33%1/3
A and not(B)
A= $asset->{"date"} > $nowB= not $build_options{"future"}
ABresult
0X0
100
111
89 0 my $logger = $qgoda->logger;
90 0 $logger->debug(__x("skipping future document '{relpath}'",
91 relpath => $asset->getRelpath));
92 0 $site->removeAsset($asset);
93 }
94  
95 3 return $self;
96 }
97  
98 sub __fillMeta {
99 3 ▶ my ($self, $asset) = @_;
Subroutine __fillMeta: called CC=7 SCAR=22.9
100  
101 3 my $qgoda = Qgoda->new;
102 3 my $logger = $qgoda->logger;
103 3 my $config = $qgoda->config;
104 3 my $site = $qgoda->getSite;
105  
106 3 my $date = $asset->{date};
107 3 ▶ if (defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date) 0 3 3
108 0 ▶ if ($date !~ /^-?[1-9][0-9]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date =~ /^-?[1-9][0-9]*$/) 0 0 0
109 0 $date = str2time $date;
110 0 ▶ if (!defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $date) 0 0 0
111 $logger->error(__x("{filename}: cannot parse date '{date}'",
112 0 date => $asset->{date}));
113 }
114 }
115 }
116  
117 3 ▶ if (!defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $date) 3 0 3
118 3 my @stat = stat $asset->getPath;
119 3 ▶ if (!@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @stat) { } 0 3 3
120 0 $logger->error(__x("cannot stat '{filename}': {error}",
121 filename => $asset->getPath, error => $!));
122 0 $date = time;
123 } else {
124 3 $date = $stat[9];
125 }
126 }
127  
128 3 $asset->{date} = Qgoda::Util::Date->newFromEpoch($date);
129  
130 3 $self->__fillPathInformation($asset, $site);
131  
132 3 ▶ $asset->{title} = $asset->{basename} if !exists $asset->{title};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $asset->{"title"} 0 3 3
133 3 $asset->{slug} = $self->__slug($asset);
134  
135 3 $asset->{view} = $site->getMetaValue(view => $asset);
136 3 $asset->{type} = $site->getMetaValue(type => $asset);
137  
138 3 return $self;
139 }
140  
141 sub __slug {
142 3 ▶ my ($self, $asset) = @_;
Subroutine __slug: called CC=1 SCAR=0.0
143  
144 3 return slugify $asset->{title};
145 }
146  
147 sub __fillPathInformation {
148 3 ▶ my ($self, $asset, $site) = @_;
Subroutine __fillPathInformation: called CC=11 SCAR=25.4
149  
150 3 my $relpath = $asset->getRelpath;
151 3 my ($filename, $directory) = fileparse $relpath;
152  
153 3 $asset->{filename} = $filename;
154  
155 3 $directory = normalize_path $directory;
156 3 ▶ $directory = '' if '.' eq $directory;
Branch: 1 branch
BranchTrueFalseTotal
if "." eq $directory 1 2 3
157 3 $asset->{directory} = $directory;
158  
159 3 my ($basename, @suffixes) = strip_suffix $filename;
160 3 $asset->{basename} = $basename;
161  
162 3 ▶ if (empty $asset->{chain}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&empty($asset->{"chain"})) 3 0 3
163 3 my $trigger = $site->getTrigger(@suffixes);
164 3 ▶ if (!empty $trigger) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&empty($trigger)) 3 0 3
165 3 my ($chain, $name) = $site->getChainByTrigger($trigger);
166 3 ▶ $asset->{chain} = $name if $chain;
Branch: 1 branch
BranchTrueFalseTotal
if $chain 3 0 3
167 3 ▶ if ($chain && exists $chain->{suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chain and exists $chain->{"suffix"}) 3 0 3
Condition33%1/3
$chain and exists $chain->{"suffix"}
!ll&&!rl&&r
003
MC/DC0%0/2
$chain and exists $chain->{"suffix"}
$chain exists $chain->{"suffix"}
Truth table33%1/3
A and B
A= $chainB= exists $chain->{"suffix"}
ABresult
0X0
100
111
168 3 ▶ for (my $i = $#suffixes; $i >= 0; --$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 3 0 3
169 3 ▶ if ($suffixes[$i] eq $trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffixes[$i] eq $trigger) 3 0 3
170 3 $suffixes[$i] = $chain->{suffix};
171 3 last;
172 }
173 }
174 }
175 }
176 }
177  
178 3 $asset->{suffixes} = \@suffixes;
179 3 ▶ if (@suffixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@suffixes) 3 0 3
180 3 $asset->{suffix} = '.' . join '.', @suffixes;
181 }
182  
183 3 $asset->{location} = $site->getMetaValue(location => $asset);
184 3 $asset->{permalink} = $site->getMetaValue(permalink => $asset);
185  
186 3 ▶ if (Qgoda->new->config->{'case-sensitive'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Qgoda"->new->config->{"case-sensitive"}) 0 3 3
187 0 $asset->{location} = lc $asset->{location};
188 0 $asset->{permalink} = lc $asset->{permalink};
189 }
190  
191 3 $asset->{index} = $site->getMetaValue(index => $asset);
192  
193 3 return $self;
194 }
195  
196 6 1;
« Qgoda/Processor/HTMLFilter.pm ↑ Summary Qgoda/PluginLoader/TT2/Filter.pm »