Aphra.pm

filter: Statementstmt 58.2% 78 / 134 Branchbran 9.3% 3 / 32 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 68.9% 20 / 29 Podpod 0.0% 0 / 6 totaltotal 46.6% 103 / 221 CC 25 SCAR 24.6
CC
25
Cov
46%
make_redirects
42.0
_build_uri
30.0
build
20.0
CRAP 11.7
« Aphra/File.pm ↑ Summary
1 =head1 NAME
2  
3 App::Aphra - A simple static sitebuilder in Perl.
4  
5 =head1 SYNOPSIS
6  
7 use App::Aphra;
8  
9 @ARGV = qw[build];
10  
11 my $app = App::Aphra->new;
12 $app->run;
13  
14 =head1 DESCRIPTION
15  
16 For now, you probably want to look at the command-line program L<aphra>
17 which does all you want and is far better documented.
18  
19 I'll improve this documentation in the future.
20  
21 =cut
22  
23 package App::Aphra;
24  
25 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use Template::Provider::Pandoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use FindBin '$Bin';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use YAML::XS 'LoadFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 7 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 7 ▶use App::Aphra::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 7 our $VERSION = '0.2.9';
43  
44 has commands => (
45 isa => 'HashRef',
46 is => 'ro',
47 2 ▶ default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 2 build => \&build,
49 serve => \&serve,
50 } },
51 7 );
52  
53 7 has config_defaults => (
54 isa => 'HashRef',
55 is => 'ro',
56 lazy_build => 1,
57 );
58  
59 2 ▶sub _build_config_defaults {
Subroutine _build_config_defaults: called CC=1 SCAR=0.0
60 return {
61 2 source => 'in',
62 fragments => 'fragments',
63 layouts => 'layouts',
64 wrapper => 'page',
65 target => 'docs',
66 extensions => {
67 tt => 'template',
68 md => 'markdown',
69 },
70 output => 'html',
71 };
72 }
73  
74 7 has config => (
75 isa => 'HashRef',
76 is => 'ro',
77 lazy_build => 1,
78 );
79  
80 2 ▶sub _build_config {
Subroutine _build_config: called CC=6 SCAR=19.9
81 2 my $self = shift;
82  
83 2 my %opts;
84 2 GetOptions(\%opts,
85 'source=s', 'fragments=s', 'layouts=s', 'wrapper=s',
86 'target=s', 'extensions=s%', 'output=s',
87 'version', 'help');
88  
89 2 for (qw[version help]) {
90 4 ▶ $self->$_ and exit if $opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{$_} 0 4 4
Condition20%1/5
$opts{$_} && ($self->$_ && exit)
!ll&&!rl&&r
400
$self->$_ and exit
!ll
00
MC/DC0%0/2
$opts{$_} && ($self->$_ && exit)
$opts{$_} $self->$_
Truth table33%1/3
A && B
A= $opts{$_}B= $self->$_
ABresult
0X0
100
111
91 }
92  
93 2 my %defaults = %{ $self->config_defaults };
94  
95 2 my %config;
96 2 for (keys %defaults) {
97 14 ▶ $config{$_} = $opts{$_} // $defaults{$_};
Condition33%1/3
$opts{$_} // $defaults{$_}
l!l&&r!l&&!r
0140
MC/DC0%0/2
$opts{$_} // $defaults{$_}
$opts{$_} $defaults{$_}
Truth table33%1/3
A // B
A= $opts{$_}B= $defaults{$_}
ABresult
1X1
011
000
98 }
99  
100 2 return \%config;
101 }
102  
103 7 has site_vars => (
104 isa => 'HashRef',
105 is => 'ro',
106 lazy_build => 1,
107 );
108  
109 1 ▶sub _build_site_vars {
Subroutine _build_site_vars: called CC=2 SCAR=7.4
110 1 my $self = shift;
111  
112 1 my $site_vars = {};
113  
114 1 ▶ if (-f 'site.yml') {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "site.yml") 0 1 1
115 0 $site_vars = LoadFile('site.yml');
116 }
117  
118 1 return $site_vars;
119 }
120  
121 7 has include_path => (
122 isa => 'ArrayRef',
123 is => 'ro',
124 lazy_build => 1,
125 );
126  
127 1 ▶sub _build_include_path {
Subroutine _build_include_path: called CC=3 SCAR=11.1
128 1 my $self = shift;
129  
130 1 my $include_path;
131 1 foreach (qw[source fragments layouts]) {
132 push @$include_path, $self->config->{$_}
133 3 ▶ if exists $self->config->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->config->{$_} 3 0 3
134 }
135  
136 1 return $include_path;
137 }
138  
139 7 has template => (
140 isa => 'Template',
141 is => 'ro',
142 lazy_build => 1,
143 );
144  
145 1 ▶sub _build_template {
Subroutine _build_template: called CC=1 SCAR=0.0
146 1 my $self = shift;
147  
148 1 my $exts = clone $self->config->{extensions};
149 1 delete $exts->{tt};
150  
151 return Template->new(
152 ENCODING => 'utf8',
153 LOAD_TEMPLATES => [
154 Template::Provider::Pandoc->new(
155 INCLUDE_PATH => $self->include_path,
156 EXTENSIONS => $exts,
157 OUTPUT_FORMAT => $self->config->{output},
158 STRIP_FRONT_MATTER => 1,
159 ),
160 ],
161 VARIABLES => {
162 site => $self->site_vars,
163 aphra => $self,
164 },
165 INCLUDE_PATH => $self->include_path,
166 OUTPUT_PATH => $self->config->{target},
167 WRAPPER => $self->config->{wrapper},
168 1 );
169 }
170  
171 7 has uri => (
172 isa => 'URI',
173 is => 'ro',
174 lazy_build => 1,
175 );
176  
177 0 ▶sub _build_uri {
Subroutine _build_uri: not called CC=5 SCAR=34.0
178 0 my $self = shift;
179  
180 0 ▶ return URI->new($self->site_vars->{uri}) if $self->site_vars->{uri};
Branch: 1 branch
BranchTrueFalseTotal
if $self->site_vars->{"uri"} 0 0 0
181  
182 0 ▶ my $host = $self->site_vars->{host} || hostname;
Condition0%0/3
$self->site_vars->{'host'} || hostname()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->site_vars->{'host'} || hostname()
$self->site_vars->{'host'} hostname()
Truth table0%0/3
A || B
A= $self->site_vars->{'host'}B= hostname()
ABresult
1X1
011
000
183 0 ▶ my $protocol = $self->site_vars->{protocol} || 'https';
Condition0%0/2
$self->site_vars->{'protocol'} || 'https'
l!l
00
MC/DC0%0/1
$self->site_vars->{'protocol'} || 'https'
$self->site_vars->{'protocol'}
Truth table0%0/2
A
A= $self->site_vars->{'protocol'}
Aresult
11
00
184  
185 0 my $uri = "$protocol://$host";
186 0 ▶ $uri .= ':' . $self->site_vars->{port} if $self->site_vars->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->site_vars->{"port"} 0 0 0
187 0 $uri .= '/';
188  
189 0 return URI->new($uri);
190 }
191  
192 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
193 0 my $self = shift;
194  
195 0 $self->config;
196  
197 0 ▶ @ARGV or die "Must give a command\n";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV or die "Must give a command\n" 0 0 0
198  
199 0 my $cmd = shift @ARGV;
200  
201 0 ▶ if (my $method = $self->commands->{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->commands->{$cmd}) { } 0 0 0
202 0 $self->$method;
203 } else {
204 0 die "$cmd is not a valid command\n";
205 }
206 }
207  
208 0 ▶sub build {
Subroutine build: not called CC=4 SCAR=30.0
Pod undocumented
209 0 my $self = shift;
210  
211 0 my $src = $self->config->{source};
212  
213 0 path($self->config->{target})->remove_tree;
214 0 path($self->config->{target})->mkpath;
215  
216 0 ▶ -e $src or die "Cannot find $src\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $src or die "Cannot find $src\n" 0 0 0
217 0 ▶ -d $src or die "$src is not a directory\n";
Branch: 1 branch
BranchTrueFalseTotal
-d $src or die "$src is not a directory\n" 0 0 0
218  
219 0 ▶ if ($self->site_vars->{redirects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->site_vars->{"redirects"}) 0 0 0
220 0 $self->make_redirects;
221 }
222  
223 find({ wanted => $self->_make_do_this, no_chdir => 1 },
224 0 $self->config->{source});
225 }
226  
227 0 ▶sub _make_do_this {
Subroutine _make_do_this: not called CC=1 SCAR=6.9
228 0 my $self = shift;
229  
230 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
231 0 ▶ return unless -f;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
232  
233 0 my $f = App::Aphra::File->new({
234 app => $self, filename => $_,
235 });
236  
237 0 $f->process;
238 0 };
239 }
240  
241 0 ▶sub make_redirects {
Subroutine make_redirects: not called CC=6 SCAR=37.4
Pod undocumented
242 0 my $self = shift;
243 0 my $redirects = $self->site_vars->{redirects};
244  
245 0 ▶ return if !$redirects;
Branch: 1 branch
BranchTrueFalseTotal
unless $redirects 0 0 0
246 0 ▶ return if !@$redirects;
Branch: 1 branch
BranchTrueFalseTotal
unless @$redirects 0 0 0
247  
248 0 my $target = $self->config->{target};
249  
250 0 for (@$redirects) {
251 0 my $from = $_->{from};
252 0 ▶ $from .= 'index.html' if $from =~ m|/$|;
Branch: 1 branch
BranchTrueFalseTotal
if $from =~ m[/$]u 0 0 0
253  
254 0 my $to = $_->{to};
255  
256 0 my $outdir = path("$target$from")->dirname;
257 0 path($outdir)->mkdir;
258  
259 0 ▶ open my $out_fh, '>', "$target$from"
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">", "$target$from" or die "Cannot open '$target${from}' for writing: $!\n" 0 0 0
260 or die "Cannot open '$target$from' for writing: $!\n";
261  
262 0 print $out_fh <<EOF;
263 <!DOCTYPE html>
264 <html>
265 <head>
266 <meta http-equiv="refresh" content="0; url=$to">
267 </head>
268 </html>
269 EOF
270  
271 0 close $out_fh;
272 }
273 }
274  
275 0 ▶sub serve {
Subroutine serve: not called CC=2 SCAR=17.9
Pod undocumented
276 0 my $self = shift;
277  
278 0 require App::HTTPThis;
279 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
280 0 croak "App::HTTPThis must be installed for 'serve' command";
281 }
282  
283 0 local @ARGV = $self->config->{target};
284 0 App::HTTPThis->new->run;
285 }
286  
287 7 has ver => (
288 is => 'ro',
289 default => $VERSION,
290 );
291  
292 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
293 0 my $me = path($0)->basename;
294 0 say "\n$me version: $VERSION\n";
295 }
296  
297 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my $self = shift;
299 0 my $me = path($0)->basename;
300 0 $self->version;
301  
302 0 say <<ENDOFHELP;
303 $me is a simple static sitebuilder which uses the Template Toolkit to
304 process input templates and turn them into a web site.
305 ENDOFHELP
306 }
307  
308 7 __PACKAGE__->meta->make_immutable;
309  
310 7 1;
311  
312 =head1 AUTHOR
313  
314 Dave Cross <dave@perlhacks.com>
315  
316 =head1 COPYRIGHT AND LICENCE
317  
318 Copyright (c) 2017-2024, Magnum Solutions Ltd. All Rights Reserved.
319  
320 This library is free software; you can redistribute it and/or modify it
321 under the same terms as Perl itself.
322  
323 =cut
« Aphra/File.pm ↑ Summary