File Coverage

blib/lib/App/Aphra.pm
Criterion Covered Total %
statement 53 76 69.7
branch 2 14 14.2
condition 1 6 16.6
subroutine 15 21 71.4
pod 0 4 0.0
total 71 121 58.6


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