File Coverage

blib/lib/App/Aphra.pm
Criterion Covered Total %
statement 53 82 64.6
branch 2 16 12.5
condition 1 6 16.6
subroutine 15 22 68.1
pod 0 5 0.0
total 71 131 54.2


line stmt bran cond sub pod time code
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 3     3   210937 use 5.014;
  3         32  
26              
27 3     3   1941 use Moose;
  3         1450360  
  3         24  
28 3     3   25944 use Template;
  3         62187  
  3         110  
29 3     3   1479 use Template::Provider::Pandoc;
  3         662933  
  3         192  
30 3     3   2055 use FindBin '$Bin';
  3         3521  
  3         467  
31 3     3   29 use File::Find;
  3         10  
  3         162  
32 3     3   25 use File::Basename;
  3         7  
  3         169  
33 3     3   2425 use Getopt::Long;
  3         33066  
  3         14  
34 3     3   378 use Carp;
  3         7  
  3         174  
35 3     3   1387 use Clone 'clone';
  3         8243  
  3         175  
36              
37 3     3   1467 use App::Aphra::File;
  3         10  
  3         3177  
38              
39             our $VERSION = '0.0.6';
40              
41             has commands => (
42             isa => 'HashRef',
43             is => 'ro',
44             default => sub { {
45             build => \&build,
46             serve => \&serve,
47             } },
48             );
49              
50             has config_defaults => (
51             isa => 'HashRef',
52             is => 'ro',
53             lazy_build => 1,
54             );
55              
56             sub _build_config_defaults {
57             return {
58 2     2   69 source => 'in',
59             fragments => 'fragments',
60             layouts => 'layouts',
61             wrapper => 'page',
62             target => 'docs',
63             extensions => {
64             tt => 'template',
65             md => 'markdown',
66             },
67             output => 'html',
68             };
69             }
70              
71             has config => (
72             isa => 'HashRef',
73             is => 'ro',
74             lazy_build => 1,
75             );
76              
77             sub _build_config {
78 2     2   4 my $self = shift;
79              
80 2         4 my %opts;
81 2         14 GetOptions(\%opts,
82             'source=s', 'fragments=s', 'layouts=s', 'wrapper=s',
83             'target=s', 'extensions=s%', 'output=s',
84             'version', 'help');
85              
86 2         1307 for (qw[version help]) {
87 4 50 0     15 $self->$_ and exit if $opts{$_};
88             }
89              
90 2         5 my %defaults = %{ $self->config_defaults };
  2         66  
91              
92 2         5 my %config;
93 2         10 for (keys %defaults) {
94 14   33     46 $config{$_} = $opts{$_} // $defaults{$_};
95             }
96 2         53 return \%config;
97             }
98              
99             has include_path => (
100             isa => 'ArrayRef',
101             is => 'ro',
102             lazy_build => 1,
103             );
104              
105             sub _build_include_path {
106 1     1   2 my $self = shift;
107              
108 1         2 my $include_path;
109 1         4 foreach (qw[source fragments layouts]) {
110             push @$include_path, $self->config->{$_}
111 3 50       64 if exists $self->config->{$_};
112             }
113              
114 1         23 return $include_path;
115             }
116              
117             has template => (
118             isa => 'Template',
119             is => 'ro',
120             lazy_build => 1,
121             );
122              
123             sub _build_template {
124 1     1   2 my $self = shift;
125              
126 1         31 my $exts = clone $self->config->{extensions};
127 1         3 delete $exts->{tt};
128              
129             return Template->new(
130             LOAD_TEMPLATES => [
131             Template::Provider::Pandoc->new(
132             INCLUDE_PATH => $self->include_path,
133             EXTENSIONS => $exts,
134             OUTPUT_FORMAT => $self->config->{output},
135             ),
136             ],
137             INCLUDE_PATH => $self->include_path,
138             OUTPUT_PATH => $self->config->{target},
139             WRAPPER => $self->config->{wrapper},
140 1         40 );
141             }
142              
143             sub run {
144 0     0 0   my $self = shift;
145              
146 0           $self->config;
147              
148 0 0         @ARGV or die "Must give a command\n";
149              
150 0           my $cmd = shift @ARGV;
151              
152 0 0         if (my $method = $self->commands->{$cmd}) {
153 0           $self->$method;
154             } else {
155 0           die "$cmd is not a valid command\n";
156             }
157             }
158              
159             sub build {
160 0     0 0   my $self = shift;
161              
162 0           my $src = $self->config->{source};
163              
164 0 0         -e $src or die "Cannot find $src\n";
165 0 0         -d $src or die "$src is not a directory\n";
166              
167             find({ wanted => $self->_make_do_this, no_chdir => 1 },
168 0           $self->config->{source});
169             }
170              
171             sub _make_do_this {
172 0     0     my $self = shift;
173              
174             return sub {
175 0 0   0     return unless -f;
176              
177 0           my $f = App::Aphra::File->new({
178             app => $self, filename => $_,
179             });
180              
181 0           $f->process;
182 0           };
183             }
184              
185             sub serve {
186 0     0 0   my $self = shift;
187              
188 0           require App::HTTPThis;
189 0 0         if ($@) {
190 0           croak "App::HTTPThis must be installed for 'serve' command";
191             }
192              
193 0           local @ARGV = $self->config->{target};
194 0           App::HTTPThis->new->run;
195             }
196              
197             sub version {
198 0     0 0   my $me = basename $0;
199 0           say "\n$me version: $VERSION\n";
200             }
201              
202             sub help {
203 0     0 0   my $self = shift;
204 0           my $me = basename $0;
205 0           $self->version;
206              
207 0           say <<ENDOFHELP;
208             $me is a simple static sitebuilder which uses the Template Toolkit to
209             process input templates and turn them into a web site.
210             ENDOFHELP
211             }
212              
213             __PACKAGE__->meta->make_immutable;
214              
215             1;
216              
217             =head1 AUTHOR
218              
219             Dave Cross <dave@perlhacks.com>
220              
221             =head1 COPYRIGHT AND LICENCE
222              
223             Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved.
224              
225             This library is free software; you can redistribute it and/or modify it
226             under the same terms as Perl itself.
227              
228             =cut