Qgoda/Builder.pm

filter: Statementstmt 83.5% 127 / 152 Branchbran 44.8% 26 / 58 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 6 totaltotal 69.5% 171 / 246 CC 37 SCAR 26.8
CC
37
Cov
71%
saveArtefact
38.5
build
12.0
wrapAsset
10.4
CRAP 14.6
« Qgoda/HTMLFilter/AnchorTarget.pm ↑ Summary Qgoda/Command/Markdown.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::Builder;
20 6 $Qgoda::Builder::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 POSIX qw(setlocale);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Locale::Util qw(web_set_locale);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use Qgoda::Util qw(empty read_file read_body write_file blength);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Qgoda::Util::Translate qw(translate_body);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub new {
32 4 ▶ my $self = '';
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
33 4 bless \$self, shift;
34 }
35  
36 sub build {
37 2 ▶ my ($self, $site, %options) = @_;
Subroutine build: called CC=9 SCAR=24.8
Pod undocumented
38  
39 2 my $qgoda = Qgoda->new;
40 2 my $logger = $qgoda->logger;
41 2 my $config = $qgoda->config;
42  
43 2 my $site = $qgoda->getSite;
44 2 my $errors = $site->getErrors;
45  
46 # 1st pass, usually Markdown.
47 2 ASSET: foreach my $asset (sort { $b->{priority} <=> $a->{priority} }
48 $site->getAssets) {
49 3 my $saved_locale = setlocale(POSIX::LC_ALL());
50 3 eval {
51 local $SIG{__WARN__} = sub {
52 0 ▶ my ($msg) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 $logger->warning("$asset->{path}: $msg");
54 3 };
55 3 $logger->debug(__x("processing asset '/{relpath}'",
56 relpath => $asset->getRelpath));
57 3 $self->processAsset($asset, $site);
58 };
59 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
60 0 ++$errors;
61 0 my $path = $asset->getPath;
62 0 $logger->error("$path: $@");
63 }
64 3 setlocale(POSIX::LC_ALL(), $saved_locale);
65 }
66  
67 2 $site->computeRelations;
68  
69 # 2nd pass, usually HTML.
70 2 ASSET: foreach my $asset (sort { $a->{priority} <=> $b->{priority} }
71 $site->getAssets) {
72 3 ▶ if ($asset->{virtual}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asset->{"virtual"}) 0 3 3
73 0 $logger->debug(__x("not wrapping virtual asset '/{relpath}'",
74 relpath => $asset->getRelpath));
75 }
76  
77 3 my $saved_locale = setlocale(POSIX::LC_ALL());
78 3 eval {
79 local $SIG{__WARN__} = sub {
80 2 ▶ my ($msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 2 $logger->warning("$asset->{path}: $msg");
82 3 };
83 3 $logger->debug(__x("wrapping asset '/{relpath}'",
84 relpath => $asset->getRelpath));
85 3 $self->wrapAsset($asset, $site);
86  
87 3 ▶ if (!$options{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"dry_run"}) 3 0 3
88 3 $self->saveArtefact($asset, $site, $asset->{location});
89 }
90 $logger->debug(__x("successfully built '{location}'",
91 3 location => $asset->{location}));
92 };
93 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
94 0 ++$errors;
95 0 my $path = $asset->getPath;
96 0 $logger->error("$path: $@");
97 }
98 3 setlocale(POSIX::LC_ALL(), $saved_locale);
99 }
100
101 2 ▶ if ($errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) 0 2 2
102 0 $logger->error(">>>>>>>>>>>>>>>>>>>");
103 0 ▶ $logger->error(__nx("one artefact has not been built because of errors (see above)",
Branch: 1 branch
BranchTrueFalseTotal
if $errors 0 0 0
104 "{num} artefacts have not been built because of errors (see above)",
105 $errors, num => $errors)) if $errors;
106 0 $logger->error(">>>>>>>>>>>>>>>>>>>");
107 }
108  
109 2 return $self;
110 }
111  
112 sub readAssetContent {
113 3 ▶ my ($self, $asset, $site) = @_;
Subroutine readAssetContent: called CC=3 SCAR=11.2
Pod undocumented
114  
115 3 ▶ if ($asset->{raw}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($asset->{'raw'}) { } 0 3 3
elsif (not &empty($asset->{'master'})) { } 1 2 3
116 0 return read_file($asset->getPath);
117 } elsif (!empty $asset->{master}) {
118 1 return translate_body $asset;
119 } else {
120 2 my $placeholder = Qgoda->new->config->{front_matter_placeholder};
121 2 return read_body($asset->getPath, $placeholder);
122 }
123 }
124  
125 sub saveArtefact {
126 3 ▶ my ($self, $asset, $site, $permalink) = @_;
Subroutine saveArtefact: called CC=12 SCAR=36.5
Pod undocumented
127  
128 3 require Qgoda;
129 3 my $qgoda = Qgoda->new;
130 3 my $config = $qgoda->config;
131 3 ▶ $permalink = '/' . $asset->getRelpath if empty $permalink;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($permalink) 0 3 3
132 3 my $path = File::Spec->catdir($config->{paths}->{site}, $permalink);
133  
134 3 my $existing = $site->getArtefact($path);
135 3 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 0 3 3
136 0 my $origin = $existing->getAsset;
137 0 my $logger = $qgoda->logger;
138 0 ▶ $logger->warning(__x("Overwriting artefact at '{outpath}', "
Branch: 1 branch
BranchTrueFalseTotal
$origin ? : 0 0 0
139 . "origin: {origin}",
140 outpath => $path,
141 origin => $origin ? $origin->getOrigin : __"[unknown origin]"));
142 }
143  
144 3 my $write_file = 1;
145 3 ▶ if ($config->{compare_output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"compare_output"}) 3 0 3
146 3 my @stat = stat $path;
147 3 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) 0 3 3
148 0 ▶ if ($stat[7] == blength $asset->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[7] == blength($asset->{"content"})) 0 0 0
149 0 my $old = read_file $path;
150 0 ▶ Encode::_utf8_on($old) if Encode::is_utf8($asset->{content});
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($asset->{"content"}) 0 0 0
151 0 ▶ if (defined $old && $old eq $asset->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old and $old eq $asset->{"content"}) 0 0 0
Condition0%0/3
defined $old and $old eq $asset->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $old and $old eq $asset->{"content"}
defined $old $old eq $asset->{"content"}
Truth table0%0/3
A and B
A= defined $oldB= $old eq $asset->{"content"}
ABresult
0X0
100
111
152 0 undef $write_file;
153 0 $qgoda->logger->debug(__x("Skipping unchanged file"
154 . " '{output}'", output => $path));
155 }
156 }
157 }
158 }
159  
160 3 ▶ if ($write_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_file) 3 0 3
161 3 $site->addModified($path, $asset);
162 3 ▶ unless (write_file $path, $asset->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&write_file($path, $asset->{"content"})) 0 3 3
163 0 my $logger = $qgoda->logger;
164 0 $logger->error(__x("error writing '{filename}': {error}",
165 filename => $path, error => $!));
166 0 return;
167 }
168 }
169  
170 3 $site->addArtefact($path, $asset);
171 }
172  
173 sub processAsset {
174 3 ▶ my ($self, $asset, $site) = @_;
Subroutine processAsset: called CC=7 SCAR=20.0
Pod undocumented
175  
176 3 my $qgoda = Qgoda->new;
177 3 my $logger = $qgoda->logger;
178  
179 3 $logger->debug(__x("processing asset '/{relpath}'",
180 relpath => $asset->getRelpath));
181  
182 3 my $template_name = $asset->getRelpath;
183  
184 3 my $content = $self->readAssetContent($asset, $site);
185 3 $asset->{content} = $content;
186 3 ▶ Encode::_utf8_on($asset->{content}) if !$asset->{raw};
Branch: 1 branch
BranchTrueFalseTotal
unless $asset->{"raw"} 3 0 3
187 3 my @processors = $qgoda->getProcessors($asset);
188 3 foreach my $processor (@processors) {
189 6 my $short_name = ref $processor;
190 6 $short_name =~ s/^Qgoda::Processor:://;
191 6 $logger->debug(__x("processing with {processor}",
192 processor => $short_name));
193 $asset->{content} = $processor->process($asset->{content},
194 6 $asset, $template_name);
195 6 ▶ Encode::_utf8_on($asset->{content}) if !$asset->{raw};
Branch: 1 branch
BranchTrueFalseTotal
unless $asset->{"raw"} 6 0 6
196 }
197  
198 3 ▶ if (@processors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@processors) 3 0 3
199 3 my %postmeta = $processors[-1]->postMeta($asset->{content}, $asset,
200 $site);
201 3 ▶ $asset->{excerpt} = $postmeta{excerpt} if empty $asset->{excerpt};
Branch: 1 branch
BranchTrueFalseTotal
if &empty($asset->{"excerpt"}) 3 0 3
202 3 ▶ $asset->{links} ||= $postmeta{links};
Condition33%1/3
$asset->{"links"} ||= $postmeta{"links"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$asset->{"links"} ||= $postmeta{"links"}
$asset->{"links"} $postmeta{"links"}
Truth table33%1/3
A ||= B
A= $asset->{"links"}B= $postmeta{"links"}
ABresult
1X1
011
000
203 }
204  
205 3 return $self;
206 }
207  
208 sub wrapAsset {
209 3 ▶ my ($self, $asset) = @_;
Subroutine wrapAsset: called CC=10 SCAR=23.4
Pod undocumented
210  
211 3 my $qgoda = Qgoda->new;
212 3 my $site = $qgoda->getSite;
213  
214 3 my @processors = $qgoda->getWrapperProcessors($asset);
215 3 ▶ return $self if !@processors;
Branch: 1 branch
BranchTrueFalseTotal
unless @processors 0 3 3
216  
217 3 my $view = $asset->{view};
218 3 ▶ die __"no view specified.\n" if empty $view;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($view) 0 3 3
219  
220 3 my $logger = $qgoda->logger;
221
222 3 my $srcdir = $qgoda->config->{srcdir};
223 3 my $view_dir = $qgoda->config->{paths}->{views};
224 3 my $view_file = File::Spec->join($srcdir, $view_dir, $view);
225 3 ▶ if (!-e $view_file && 'default.html' eq $view) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $view_file and "default.html" eq $view) 2 1 3
Condition66%2/3
not -e $view_file and "default.html" eq $view
!ll&&!rl&&r
102
MC/DC50%1/2
not -e $view_file and "default.html" eq $view
not -e $view_file "default.html" eq $view
Truth table66%2/3
not(A) and B
A= not -e $view_fileB= "default.html" eq $view
ABresult
0X0
100
111
226 2 warn __x("no default view '{file}', creating one with defaults.\n",
227 file => $view_file);
228 2 my $msg = __x("This view has been automatically created. Please edit "
229 . " '{path}' to your needs!", path => $view_file);
230 2 my $html = <<EOF;
231 <!doctype html>
232 <html>
233 <head>
234 <meta charset="utf-8">
235 <title>[% asset.title | html %]</title>
236 </head>
237 <body>
238 <h1>[% asset.title | html %]</h1>
239 [% asset.content %]
240 <p>$msg</p>
241 </body>
242 </html>
243 EOF
244  
245 2 ▶ write_file $view_file, $html
Branch: 1 branch
BranchTrueFalseTotal
&write_file($view_file, $html) or die &__x("error writing view '{file}': {error}.\n", "file", $view_file, "error", $!) 0 2 2
246 or die __x("error writing view '{file}': {error}.\n",
247 file => $view_file, error => $!);
248 }
249 3 my $content = read_file $view_file;
250 3 ▶ die __x("error reading view '{file}': {error}.\n",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 3 3
251 file => $view_file, error => $!)
252 if !defined $content;
253 3 ▶ Encode::_utf8_on($content) if !$asset->{raw};
Branch: 1 branch
BranchTrueFalseTotal
unless $asset->{"raw"} 3 0 3
254 3 my $template_name = File::Spec->join($view_dir, $view);
255 3 foreach my $processor (@processors) {
256 6 my $short_name = ref $processor;
257 6 $short_name =~ s/^Qgoda::Processor:://;
258 6 $logger->debug(__x("wrapping with {processor}",
259 processor => $short_name));
260 6 $content = $processor->process($content, $asset, $template_name);
261 6 ▶ Encode::_utf8_on($content) if !$asset->{raw};
Branch: 1 branch
BranchTrueFalseTotal
unless $asset->{"raw"} 6 0 6
262 }
263  
264 3 $asset->{content} = $content;
265  
266 3 return $self;
267 }
268  
269 6 1;
270  
271 =head1 NAME
272  
273 Qgoda::Builder - Default builder for Qgoda posts.
« Qgoda/HTMLFilter/AnchorTarget.pm ↑ Summary Qgoda/Command/Markdown.pm »