HiD.pm

filter: Statementstmt 79.1% 255 / 322 Branchbran 39.0% 32 / 82 Conditioncond 33.3% 14 / 42 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 90.3% 56 / 62 Podpod 100% 1 / 1 totaltotal 68.3% 362 / 530 CC 68 SCAR 30.0
CC
68
Cov
67%
publish
74.7
_load_plugin_or_warn
56.0
_build_plugins
46.6
CRAP 20.1
« HiD/App/Command/server.pm ↑ Summary HiD/Generator/AtomFeed.pm »
1 # ABSTRACT: Static website publishing framework
2  
3  
4 package HiD;
5 8 $HiD::VERSION = '1.5';
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # note: we also do 'with HiD::Role::DoesLogging', just later on because reasons.
9  
10 8 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use warnings qw/ FATAL utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use open qw/ :std :utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use charnames qw/ :full /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use feature qw/ unicode_strings /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 8 ▶use Class::Load qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use File::Path qw/ make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use File::Remove qw/ remove /;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use HiD::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use HiD::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use HiD::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use HiD::Pager;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 ▶use HiD::Post;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 ▶use HiD::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use Path::Class qw/ file /;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use YAML::XS qw/ LoadFile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 8 has categories => (
39 is => 'ro' ,
40 isa => 'Maybe[HashRef[ArrayRef[HiD::Post]]]' ,
41 lazy => 1 ,
42 builder => '_build_categories' ,
43 );
44  
45 sub _build_categories {
46 0 ▶ my $self = shift;
Subroutine _build_categories: not called CC=4 SCAR=30.0
47  
48 0 ▶ return undef unless $self->posts;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->posts 0 0 0
49  
50 0 my $categories_hash = {};
51 0 foreach my $post ( @{$self->posts} ) {
52 0 push @{ $categories_hash->{$_} }, $post for @{ $post->categories };
53 }
54  
55 0 return $categories_hash;
56 }
57  
58  
59 has cli_opts => (
60 is => 'ro' ,
61 isa => 'HashRef' ,
62 lazy => 1 ,
63 6 ▶ default => sub {{}} ,
Subroutine __ANON__: called CC=1 SCAR=0.0
64 8 );
65  
66  
67 8 has config => (
68 is => 'ro' ,
69 isa => 'HashRef' ,
70 traits => [ 'Hash' ] ,
71 lazy => 1 ,
72 builder => '_build_config' ,
73 handles => { get_config => 'get' } ,
74 );
75  
76 sub _build_config {
77 27 ▶ my $self = shift;
Subroutine _build_config: called CC=4 SCAR=13.9
78  
79 27 my( $config , $config_loaded );
80  
81 27 ▶ if ( my $file = $self->config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->config_file) 25 2 27
82 try {
83 25 ▶ $config = LoadFile( $file ) // {};
Subroutine __ANON__: called CC=3 SCAR=11.1
Condition50%1/2
LoadFile($file) // {}
l!l
210
MC/DC0%0/1
LoadFile($file) // {}
LoadFile($file)
Truth table50%1/2
A
A= LoadFile($file)
Aresult
11
00
84 21 ▶ ref $config eq 'HASH' or die $!;
Branch: 1 branch
BranchTrueFalseTotal
ref $config eq "HASH" or die $! 2 19 21
85 19 $config_loaded++;
86 25 };
87 }
88  
89 27 ▶ $config_loaded or $config = {}
Branch: 1 branch
BranchTrueFalseTotal
$config_loaded or $config = {} and warn "Could not read configuration. Using defaults (and options)." 8 19 27
Condition60%3/5
$config_loaded || ($config = {}) && warn('Could not read configuration. Using defaults (and options).')
l!l
1980
$config = {} and warn "Could not read configuration. Using defaults (and options)."
!ll
08
MC/DC0%0/2
$config_loaded || ($config = {}) && warn('Could not read configuration. Using defaults (and options).')
$config_loaded $config = {}
Truth table66%2/3
A || B
A= $config_loadedB= $config = {}
ABresult
1X1
011
000
90 and warn( 'Could not read configuration. Using defaults (and options).' );
91  
92 return {
93 27 %{ $self->default_config } ,
94 %$config ,
95 27 %{ $self->cli_opts } ,
96 };
97 }
98  
99 # this is down here so it will see the 'get_config' delegation...
100 8 with 'HiD::Role::DoesLogging';
101  
102  
103 8 has config_file => (
104 is => 'ro' ,
105 isa => 'Str' ,
106 );
107  
108  
109 has default_config => (
110 is => 'ro' ,
111 isa => 'HashRef' ,
112 traits => [ 'Hash' ] ,
113 init_arg => undef ,
114 default => sub{{
115 46 ▶ default_author => 'your name here!' ,
Subroutine __ANON__: called CC=1 SCAR=0.0
116 destination => '_site' ,
117 include_dir => '_includes',
118 layout_dir => '_layouts' ,
119 plugin_dir => '_plugins' ,
120 posts_dir => '_posts' ,
121 drafts_dir => '_drafts' ,
122 source => '.' ,
123 }},
124 8 );
125  
126  
127 has destination => (
128 is => 'ro' ,
129 isa => 'HiD_DirPath' ,
130 lazy => 1 ,
131 default => sub {
132 17 ▶ my $dest = shift->get_config( 'destination' );
Subroutine __ANON__: called CC=2 SCAR=6.9
133 17 ▶ make_path $dest unless -e -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless -e -d $dest 1 16 17
134 17 return $dest;
135 },
136 8 );
137  
138  
139 has draft_post_file_regex => (
140 is => 'ro' ,
141 isa => 'RegexpRef' ,
142 46 ▶ default => sub { qr/^(?:.+?)\.(?:mk|mkd|mkdn|markdown|md|mmd|text|textile|html)$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
143 8 );
144  
145  
146 has excerpt_separator => (
147 is => 'ro' ,
148 isa => 'Str' ,
149 lazy => 1 ,
150 10 ▶ default => sub { shift->get_config( 'excerpt_separator' ) // "\n\n" }
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
(shift())->get_config("excerpt_separator") // "\n\n"
l!l
010
MC/DC0%0/1
(shift())->get_config("excerpt_separator") // "\n\n"
(shift())->get_config("excerpt_separator")
Truth table50%1/2
A
A= (shift())->get_config("excerpt_separator")
Aresult
11
00
151 8 );
152  
153  
154 has include_dir => (
155 is => 'ro' ,
156 isa => 'Maybe[HiD_DirPath]' ,
157 lazy => 1 ,
158 default => sub {
159 17 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
160 17 my $dir = $self->get_config( 'include_dir' );
161 17 ▶ ( -e -d '_includes' ) ? $dir : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e -d '_includes' ? : 17 0 17
162 },
163 8 );
164  
165  
166 has inputs => (
167 is => 'ro' ,
168 isa => 'HashRef',
169 46 ▶ default => sub {{}} ,
Subroutine __ANON__: called CC=1 SCAR=0.0
170 8 traits => ['Hash'],
171 handles => {
172 add_input => 'set' ,
173 seen_input => 'exists' ,
174 },
175 );
176  
177  
178 has layout_dir => (
179 is => 'ro' ,
180 isa => 'HiD_DirPath' ,
181 lazy => 1 ,
182 17 ▶ default => sub { shift->get_config( 'layout_dir' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
183 8 );
184  
185  
186 8 has layouts => (
187 is => 'ro' ,
188 isa => 'HashRef[HiD::Layout]',
189 lazy => 1 ,
190 builder => '_build_layouts',
191 traits => ['Hash'] ,
192 handles => {
193 get_layout_by_name => 'get' ,
194 },
195 );
196  
197 sub _build_layouts {
198 17 ▶ my $self = shift;
Subroutine _build_layouts: called CC=5 SCAR=16.8
199  
200 17 $self->INFO( "Building layouts" );
201  
202 17 my @layout_files = File::Find::Rule->file
203 ->in( $self->layout_dir );
204  
205 17 my %layouts;
206 17 foreach my $layout_file ( @layout_files ) {
207 35 my $dir = $self->layout_dir;
208  
209 35 my( $layout_name , $extension ) = $layout_file
210 =~ m|^$dir/(.*)\.([^.]+)$|;
211  
212 35 $layouts{$layout_name} = HiD::Layout->new({
213 filename => $layout_file,
214 processor => $self->processor ,
215 });
216  
217 34 $self->add_input( $layout_file => 'layout' );
218  
219 34 $self->DEBUG( "* Added layout $layout_file" );
220 }
221  
222 16 foreach my $layout_name ( keys %layouts ) {
223 32 my $metadata = $layouts{$layout_name}->metadata;
224  
225 32 ▶ if ( my $embedded_layout = $metadata->{layout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $embedded_layout = $metadata->{"layout"}) 0 32 32
226 die "FIXME embedded layout fail $embedded_layout"
227 0 ▶ unless $layouts{$embedded_layout};
Branch: 1 branch
BranchTrueFalseTotal
unless $layouts{$embedded_layout} 0 0 0
228  
229 $layouts{$layout_name}->set_layout(
230 0 $layouts{$embedded_layout}
231 );
232 }
233 }
234  
235 16 return \%layouts;
236 }
237  
238  
239 8 has limit_posts => (
240 is => 'ro' ,
241 isa => 'HiD_PosInt' ,
242 );
243  
244  
245 has objects => (
246 is => 'ro' ,
247 isa => 'ArrayRef[Object]' ,
248 traits => [ 'Array' ] ,
249 46 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
250 8 handles => {
251 add_object => 'push' ,
252 all_objects => 'elements' ,
253 },
254 );
255  
256  
257 has page_file_regex => (
258 is => 'ro' ,
259 isa => 'RegexpRef',
260 46 ▶ default => sub { qr/\.(mk|mkd|mkdn|markdown|mmd|textile|html|htm|xml|xhtml|xhtm|shtm|shtml|rss)$/ } ,
Subroutine __ANON__: called CC=1 SCAR=0.0
261 8 );
262  
263  
264 8 has pages => (
265 is => 'ro',
266 isa => 'Maybe[ArrayRef[HiD::Page]]',
267 lazy => 1 ,
268 builder => '_build_pages' ,
269 );
270  
271 sub _build_pages {
272 16 ▶ my $self = shift;
Subroutine _build_pages: called CC=3 SCAR=11.0
273  
274 # build posts before pages
275 16 $self->posts;
276  
277 14 $self->INFO( "Posts built." );
278  
279 14 my @potential_pages = File::Find::Rule->file->
280 name( $self->page_file_regex )->in( '.' );
281  
282 92 my @pages = grep { $_ } map {
283 14 ▶ if ($self->seen_input( $_ ) or $_ =~ /^_/ ) { 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->seen_input($_) or $_ =~ /^_/u) { } 66 26 92
Condition100%3/3
$self->seen_input($_) or $_ =~ /^_/u
l!l&&r!l&&!r
382826
MC/DC100%2/2
$self->seen_input($_) or $_ =~ /^_/u
$self->seen_input($_) $_ =~ /^_/u
Truth table100%3/3
A or B
A= $self->seen_input($_)B= $_ =~ /^_/u
ABresult
1X1
011
000
284 else {
285 try {
286 26 ▶ my $page = HiD::Page->new({
Subroutine __ANON__: called CC=1 SCAR=0.0
287 dest_dir => $self->destination,
288 hid => $self ,
289 input_filename => $_ ,
290 layouts => $self->layouts ,
291 });
292 26 $page->content;
293 26 $self->add_input( $_ => 'page' );
294 26 $self->add_object( $page );
295 26 $page;
296 }
297 26 ▶ catch { 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
298 }
299 } @potential_pages;
300  
301 14 return \@pages;
302 }
303  
304  
305 has plugin_dir => (
306 is => 'ro',
307 isa => 'Maybe[HiD_DirPath]',
308 lazy => 1,
309 default => sub {
310 14 ▶ my $dir = shift->get_config('plugin_dir');
Subroutine __ANON__: called CC=2 SCAR=7.2
311 14 ▶ (-e -d $dir) ? $dir : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e -d $dir ? : 0 14 14
312 },
313 8 );
314  
315  
316 8 has plugins => (
317 is => 'ro' ,
318 isa => 'ArrayRef[Pluginish]' ,
319 lazy => 1 ,
320 builder => '_build_plugins' ,
321 );
322  
323 sub _build_plugins {
324 14 ▶ my $self = shift;
Subroutine _build_plugins: called CC=9 SCAR=38.4
325  
326 14 my @loaded_plugins;
327  
328 14 ▶ if ( my $plugin_list = $self->config->{plugins} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin_list = $self->config->{"plugins"}) 0 14 14
329 0 ▶ my @plugins = ( ref $plugin_list eq 'ARRAY' ) ? @$plugin_list
Branch: 1 branch
BranchTrueFalseTotal
ref $plugin_list eq 'ARRAY' ? : 0 0 0
330 : (split /\s+/ , $plugin_list );
331  
332 0 foreach ( @plugins ) {
333  
334 0 ▶ my $plugin_name = ( /^\+/ ) ? $_ : "HiD::Generator::$_";
Branch: 1 branch
BranchTrueFalseTotal
/^\+/u ? : 0 0 0
335 0 $self->INFO( "* Loading plugin $plugin_name" );
336 0 ▶ next unless _load_plugin_or_warn( $plugin_name );
Branch: 1 branch
BranchTrueFalseTotal
unless _load_plugin_or_warn($plugin_name) 0 0 0
337 0 push @loaded_plugins , $plugin_name->new;
338 }
339 }
340  
341 14 ▶ if ( my $plugin_dir = $self->plugin_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin_dir = $self->plugin_dir) 0 14 14
342 # plugin modules in plugin_dir
343 0 my @mods = File::Find::Rule->file->
344 name( '*.pm' )->in( $plugin_dir );
345  
346 0 push @INC , $plugin_dir;
347  
348 0 foreach my $m ( @mods ) {
349 0 $m =~ s|$plugin_dir/?||;
350 0 $m =~ s|.pm$||;
351 0 $self->INFO("* Loading plugin $m" );
352 0 ▶ next unless _load_plugin_or_warn( $m );
Branch: 1 branch
BranchTrueFalseTotal
unless _load_plugin_or_warn($m) 0 0 0
353 0 push @loaded_plugins, $m->new;
354 }
355 }
356  
357 14 return \@loaded_plugins;
358 }
359  
360 sub _load_plugin_or_warn {
361 0 ▶ my $plugin = shift;
Subroutine _load_plugin_or_warn: not called CC=7 SCAR=40.3
362  
363 0 my( $lrlt , $lerr ) = try_load_class( $plugin );
364  
365 0 ▶ warn "plugin $plugin cannot be loaded : $lerr \n" and return undef
Branch: 1 branch
BranchTrueFalseTotal
unless $lrlt 0 0 0
Condition0%0/5
$lrlt || warn("plugin $plugin cannot be loaded : $lerr \n") && (return undef)
l!l&&r!l&&!r
000
warn "plugin $plugin cannot be loaded : $lerr \n" and return undef
!ll
00
MC/DC0%0/2
$lrlt || warn("plugin $plugin cannot be loaded : $lerr \n") && (return undef)
$lrlt warn "plugin $plugin cannot be loaded : $lerr \n"
Truth table0%0/3
A || B
A= $lrltB= warn "plugin $plugin cannot be loaded : $lerr \n"
ABresult
1X1
011
000
366 unless $lrlt;
367  
368 0 ▶ ( $plugin->isa('HiD::Plugin') or
Branch: 1 branch
BranchTrueFalseTotal
$plugin->isa("HiD::Plugin") or $plugin->does("HiD::Plugin") or $plugin->does("HiD::Generator") or warn "plugin $plugin is not a valid plugin.\n" and return undef 0 0 0
Condition0%0/11
$plugin->isa('HiD::Plugin') || $plugin->does('HiD::Plugin') || $plugin->does('HiD::Generator') || warn("plugin $plugin is not a valid plugin.\n") && (return undef)
l!l&&r!l&&!r
000
$plugin->isa("HiD::Plugin") or $plugin->does("HiD::Plugin") or $plugin->does("HiD::Generator")
l!l&&r!l&&!r
000
$plugin->isa("HiD::Plugin") or $plugin->does("HiD::Plugin")
l!l&&r!l&&!r
000
warn "plugin $plugin is not a valid plugin.\n" and return undef
!ll
00
MC/DC0%0/4
$plugin->isa('HiD::Plugin') || $plugin->does('HiD::Plugin') || $plugin->does('HiD::Generator') || warn("plugin $plugin is not a valid plugin.\n") && (return undef)
$plugin->isa("HiD::Plugin") $plugin->does("HiD::Plugin") $plugin->does("HiD::Generator") warn "plugin $plugin is not a valid plugin.\n"
Truth table0%0/5
A or B or C || D
A= $plugin->isa("HiD::Plugin")B= $plugin->does("HiD::Plugin")C= $plugin->does("HiD::Generator")D= warn "plugin $plugin is not a valid plugin.\n"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
369 $plugin->does('HiD::Plugin') or
370 $plugin->does('HiD::Generator') )
371 or warn "plugin $plugin is not a valid plugin.\n"
372 and return undef;
373  
374 0 return 1;
375 }
376  
377  
378 has post_file_regex => (
379 is => 'ro' ,
380 isa => 'RegexpRef' ,
381 46 ▶ default => sub { qr/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}-(?:.+?)\.(?:mk|mkd|mkdn|markdown|md|mmd|text|textile|html)$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
382 8 );
383  
384  
385 has posts_dir => (
386 is => 'ro' ,
387 isa => 'HiD_DirPath' ,
388 lazy => 1 ,
389 0 ▶ default => sub { shift->get_config( 'posts_dir' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
390 8 );
391  
392  
393 8 has posts => (
394 is => 'ro' ,
395 isa => 'ArrayRef[HiD_Post]' ,
396 traits => [ qw/ Array / ] ,
397 handles => { posts_size => 'count' } ,
398 lazy => 1 ,
399 builder => '_build_posts' ,
400 );
401  
402 sub _build_posts {
403 17 ▶ my $self = shift;
Subroutine _build_posts: called CC=4 SCAR=14.3
404  
405 # build layouts before posts
406 17 $self->layouts;
407  
408 16 $self->INFO("Layouts built.");
409  
410 16 $self->INFO("Building posts." );
411  
412 16 my $rule = File::Find::Rule->new;
413  
414 16 my @posts_directories = $rule->or(
415 $rule->new->directory->name( $self->get_config( 'posts_dir' )) ,
416 $rule->new->directory->name( $self->get_config( 'destination' ))->prune->discard ,
417 )->in( $self->source );
418  
419 16 my @potential_posts = File::Find::Rule->file
420 ->name( $self->post_file_regex )->in( @posts_directories );
421  
422 16 ▶ if ( $self->get_config( 'publish_drafts' )){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_config("publish_drafts")) 0 16 16
423 0 push @potential_posts , $self->_build_potential_draft_posts_list ,
424 }
425  
426 44 my @posts = grep { $_ } map {
427 16 try {
428 44 ▶ $self->DEBUG( "* Trying to build post $_" );
Subroutine __ANON__: called CC=1 SCAR=0.0
429 44 my $post = HiD::Post->new({
430 dest_dir => $self->destination,
431 hid => $self ,
432 input_filename => $_ ,
433 layouts => $self->layouts ,
434 });
435 43 $self->add_input( $_ => 'post' );
436 43 $self->add_object( $post );
437 43 $self->DEBUG( "* Built post $_" );
438 43 $post;
439 }
440 44 ▶ catch { $self->ERROR( "ERROR: Post failed to build: $_" ) ; return 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
441 } @potential_posts;
442  
443 16 @posts = sort { $b->date <=> $a->date } @posts;
444  
445 14 ▶ if ( my $limit = $self->limit_posts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit = $self->limit_posts) 0 14 14
446 0 ▶ die "--limit_posts must be positive" if $limit < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $limit < 1 0 0 0
447 0 @posts = splice( @posts , -$limit , $limit );
448 }
449  
450 14 return \@posts;
451 }
452  
453 sub _build_potential_draft_posts_list {
454 0 ▶ my( $self ) = @_;
Subroutine _build_potential_draft_posts_list: not called CC=1 SCAR=6.9
455  
456 0 my $rule = File::Find::Rule->new;
457  
458 0 my @posts_directories = $rule->or(
459 $rule->new->directory->name( $self->get_config( 'drafts_dir' )) ,
460 $rule->new->directory->name( $self->get_config( 'destination' ))->prune->discard ,
461 )->in( $self->source );
462  
463 0 my @potential_posts = File::Find::Rule->file
464 ->name( $self->draft_post_file_regex )->in( @posts_directories );
465  
466 0 return @potential_posts;
467 }
468  
469  
470 has processor => (
471 is => 'ro' ,
472 isa => 'HiD::Processor' ,
473 lazy => 1 ,
474 default => sub {
475 17 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
476  
477 17 ▶ my $processor_name = $self->get_config( 'processor_name' ) // 'Handlebars';
Condition50%1/2
$self->get_config("processor_name") // "Handlebars"
l!l
017
MC/DC0%0/1
$self->get_config("processor_name") // "Handlebars"
$self->get_config("processor_name")
Truth table50%1/2
A
A= $self->get_config("processor_name")
Aresult
11
00
478  
479 17 ▶ my $processor_class = ( $processor_name =~ /^\+/ ) ? $processor_name
Branch: 1 branch
BranchTrueFalseTotal
$processor_name =~ /^\+/u ? : 0 17 17
480 : "HiD::Processor::$processor_name";
481  
482 17 try_load_class( $processor_class );
483  
484 17 return $processor_class->new( $self->processor_args );
485 },
486 8 );
487  
488  
489 has processor_args => (
490 is => 'ro' ,
491 isa => 'ArrayRef|HashRef' ,
492 lazy => 1 ,
493 default => sub {
494 17 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
495  
496 17 ▶ return $self->get_config( 'processor_args' ) if
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_config("processor_args") 0 17 17
497 defined $self->get_config( 'processor_args' );
498  
499 17 my @path = ( $self->layout_dir );
500 17 ▶ push @path , $self->include_dir
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->include_dir 17 0 17
501 if defined $self->include_dir;
502  
503 17 return { path => \@path };
504 },
505 8 );
506  
507  
508 8 has regular_files => (
509 is => 'ro',
510 isa => 'Maybe[ArrayRef[HiD::File]]',
511 lazy => 1 ,
512 builder => '_build_regular_files' ,
513 );
514  
515 sub _build_regular_files {
516 16 ▶ my $self = shift;
Subroutine _build_regular_files: called CC=6 SCAR=18.6
517  
518 # build pages before regular files
519 16 $self->pages;
520  
521 14 $self->INFO( "Pages built" );
522  
523 14 my @potential_files = File::Find::Rule->file->in( '.' );
524  
525 145 my @files = grep { $_ } map {
526 14 ▶ if ($self->seen_input( $_ ) or $_ =~ /^_/ ) { 0 }
Branch: 3 branches
BranchTrueFalseTotal
if ($self->seen_input($_) or $_ =~ /^_/u) { } 124 21 145
elsif ($_ =~ /^\.git/u) { } 0 21 21
elsif ($_ =~ /^\.svn/u or $_ =~ m[/\.svn/]u) { } 0 21 21
Condition100%3/3
$self->seen_input($_) or $_ =~ /^_/u
l!l&&r!l&&!r
646021
MC/DC100%2/2
$self->seen_input($_) or $_ =~ /^_/u
$self->seen_input($_) $_ =~ /^_/u
Truth table100%3/3
A or B
A= $self->seen_input($_)B= $_ =~ /^_/u
ABresult
1X1
011
000
527 0 elsif( $_ =~ /^\.git/ ) { 0 }
528 0 ▶ elsif( $_ =~ /^\.svn/ or $_ =~ /\/\.svn\// ) { 0 }
Condition33%1/3
$_ =~ /^\.svn/u or $_ =~ m[/\.svn/]u
l!l&&r!l&&!r
0021
MC/DC0%0/2
$_ =~ /^\.svn/u or $_ =~ m[/\.svn/]u
$_ =~ /^\.svn/u $_ =~ m[/\.svn/]u
Truth table33%1/3
A or B
A= $_ =~ /^\.svn/uB= $_ =~ m[/\.svn/]u
ABresult
1X1
011
000
529 else {
530 21 my $file = HiD::File->new({
531 dest_dir => $self->destination,
532 input_filename => $_ ,
533 });
534 21 $self->add_input( $_ => 'file' );
535 21 $self->add_object( $file );
536 21 $file;
537 }
538 } @potential_files;
539  
540 14 return \@files;
541 }
542  
543  
544 has remove_unwritten_files => (
545 is => 'ro' ,
546 isa => 'Bool' ,
547 lazy => 1 ,
548 default => sub {
549 14 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
550 14 ▶ return $self->get_config('remove_unwritten_files')
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_config("remove_unwritten_files") 0 14 14
551 if defined $self->get_config('remove_unwritten_files');
552  
553 14 return 1;
554 },
555 8 );
556  
557  
558 has source => (
559 is => 'ro' ,
560 isa => 'HiD_DirPath' ,
561 lazy => 1 ,
562 default => sub {
563 16 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
564 16 my $source = $self->get_config( 'source' );
565 16 ▶ chdir $source or die $!;
Branch: 1 branch
BranchTrueFalseTotal
&HiD::chdir($source) or die $! 0 16 16
566 16 return $source;
567 },
568 8 );
569  
570  
571 8 has 'tags' => (
572 is => 'ro',
573 isa => 'Maybe[HashRef[ArrayRef[HiD::Post]]]',
574 lazy => 1,
575 builder => '_build_tags',
576 );
577  
578 sub _build_tags {
579 0 ▶ my $self = shift;
Subroutine _build_tags: not called CC=4 SCAR=30.0
580  
581 0 ▶ return undef unless $self->posts;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->posts 0 0 0
582  
583 0 my $tags_hash = {};
584 0 foreach my $post (@{$self->posts}) {
585 0 push @{$tags_hash->{$_}}, $post for @{$post->tags};
586 }
587 0 return $tags_hash;
588 }
589  
590  
591 has time => (
592 is => 'ro',
593 isa => 'DateTime' ,
594 init_arg => undef ,
595 46 ▶ default => sub { DateTime->now() } ,
Subroutine __ANON__: called CC=1 SCAR=0.0
596 8 );
597  
598  
599  
600 has written_files => (
601 is => 'ro' ,
602 isa => 'HashRef' ,
603 traits => [ qw/ Hash / ] ,
604 46 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
605 8 handles => {
606 add_written_file => 'set' ,
607 all_written_files => 'keys' ,
608 wrote_file => 'defined' ,
609 },
610 );
611  
612  
613 sub publish {
614 16 ▶ my( $self ) = @_;
Subroutine publish: called CC=19 SCAR=43.1
Pod documented
615  
616 16 ▶ if ( -e $self->destination && $self->get_config( 'clean_destination' )){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->destination and $self->get_config("clean_destination")) 0 16 16
Condition33%1/3
-e $self->destination and $self->get_config("clean_destination")
!ll&&!rl&&r
0160
MC/DC0%0/2
-e $self->destination and $self->get_config("clean_destination")
-e $self->destination $self->get_config("clean_destination")
Truth table33%1/3
A and B
A= -e $self->destinationB= $self->get_config("clean_destination")
ABresult
0X0
100
111
617 0 remove( \1 , $self->destination );
618 0 $self->INFO( "cleaned destination directory" );
619 0 make_path $self->destination;
620 }
621  
622 16 $self->INFO( "publish" );
623  
624 # bootstrap data structures
625 # FIXME should have a more explicit way to do this
626 16 $self->regular_files;
627  
628 14 $self->INFO( "files bootstrapped" );
629  
630 14 $self->add_written_file( $self->destination => '_site_dir' );
631  
632 14 $self->INFO( "processing plugins for generate()" );
633  
634 14 foreach my $plugin ( @{ $self->plugins } ) {
635 0 ▶ if ( $plugin->does( 'HiD::Generator' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->does("HiD::Generator")) 0 0 0
636 0 $plugin->generate($self)
637 }
638 }
639  
640 14 ▶ if ( $self->config->{pagination} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"pagination"}) 0 14 14
641 my $entries_per_page = $self->config->{pagination}{entries}
642 0 ▶ or die "Must set 'pagination.entries' key in pagination config";
Branch: 1 branch
BranchTrueFalseTotal
my $entries_per_page = $self->config->{"pagination"}{"entries"} or die "Must set 'pagination.entries' key in pagination config" 0 0 0
643  
644 my $page_fstring = $self->config->{pagination}{page}
645 0 ▶ or die "Must set 'pagination.page' key in pagination config";
Branch: 1 branch
BranchTrueFalseTotal
my $page_fstring = $self->config->{"pagination"}{"page"} or die "Must set 'pagination.page' key in pagination config" 0 0 0
646  
647 my $template = $self->config->{pagination}{template}
648 0 ▶ or die "Must set 'pagination.template' key in pagination config";
Branch: 1 branch
BranchTrueFalseTotal
my $template = $self->config->{"pagination"}{"template"} or die "Must set 'pagination.template' key in pagination config" 0 0 0
649  
650 0 my $pager = HiD::Pager->new({
651 entries => $self->posts ,
652 entries_per_page => $entries_per_page ,
653 hid => $self ,
654 page_pattern => $page_fstring
655 });
656  
657 0 ▶ while ( my $page_data = $pager->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page_data = $pager->next) 0 0 0
658 my $page = HiD::Page->new({
659 dest_dir => $self->destination ,
660 hid => $self ,
661 input_filename => $template ,
662 layouts => $self->layouts ,
663 url => $page_data->{current_page_url} ,
664 0 });
665 0 $page->metadata->{page_data} = $page_data;
666  
667 0 $self->add_input( "Paged page $page_data->{page_number}" => 'page' );
668 0 $self->add_object( $page );
669 }
670 }
671  
672  
673 14 foreach my $file ( $self->all_objects ) {
674 57 $file->publish;
675  
676 57 $self->INFO( sprintf "* Published %s" , $file->output_filename );
677  
678 57 my $path;
679 57 foreach my $part ( split '/' , $file->output_filename ) {
680 162 $path = file( $path , $part )->stringify;
681 162 $self->add_written_file( $path => 1 );
682 }
683 }
684  
685 14 ▶ if ( $self->remove_unwritten_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->remove_unwritten_files) 14 0 14
686 14 foreach ( File::Find::Rule->in( $self->destination )) {
687 102 ▶ $self->wrote_file($_) or remove \1 , $_;
Branch: 1 branch
BranchTrueFalseTotal
$self->wrote_file($_) or &remove(\1, $_) 1 101 102
688 }
689 }
690  
691 14 ▶ return 1 unless $self->plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->plugins 0 14 14
692  
693 14 $self->INFO( "processing plugins for after_publish()" );
694  
695 14 foreach my $plugin ( @{ $self->plugins } ) {
696 0 ▶ if ( $plugin->does( 'HiD::Plugin' ) or
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->does("HiD::Plugin") or $plugin->isa("HiD::Plugin")) 0 0 0
Condition0%0/3
$plugin->does("HiD::Plugin") or $plugin->isa("HiD::Plugin")
l!l&&r!l&&!r
000
MC/DC0%0/2
$plugin->does("HiD::Plugin") or $plugin->isa("HiD::Plugin")
$plugin->does("HiD::Plugin") $plugin->isa("HiD::Plugin")
Truth table0%0/3
A or B
A= $plugin->does("HiD::Plugin")B= $plugin->isa("HiD::Plugin")
ABresult
1X1
011
000
697 ### FIXME remove after 13 Nov 2014
698 $plugin->isa( 'HiD::Plugin' )) {
699 0 $plugin->after_publish($self)
700 }
701 }
702  
703 14 1;
704 }
705  
706  
707  
708 8 __PACKAGE__->meta->make_immutable;
709 8 1;
710  
711 __END__
712  
713 =pod
714  
715 =encoding utf-8
716  
717 =head1 NAME
718  
719 HiD - Static website publishing framework
720  
721 =head1 SYNOPSIS
722  
723 HīD is a blog-aware, GitHub-friendly, static website generation system
724 inspired by Jekyll.
725  
726 =head1 DESCRIPTION
727  
728 HiD users probably want to look at the documentation for the L<hid> command.
729  
730 Subsequent documentation in this file describes internal details that are only
731 useful or interesting for people that are trying to modify or extend HiD.
732  
733 =head1 ATTRIBUTES
734  
735 =head2 categories
736  
737 Categories hash, contains (category, post) pairs
738  
739 =head2 cli_opts
740  
741 Hashref of command line options to integrate into the config.
742  
743 (L<HiD::App::Command>s should pass in the C<$opt> variable to this.)
744  
745 =head2 config
746  
747 Hashref of configuration information.
748  
749 =head2 config_file
750  
751 Path to a configuration file.
752  
753 =head2 default_config
754  
755 Hashref of standard configuration options. The default config is:
756  
757 destination => '_site' ,
758 include_dir => '_includes',
759 layout_dir => '_layouts' ,
760 plugin_dir => '_plugins' ,
761 posts_dir => '_posts' ,
762 drafts_dir => '_drafts' ,
763 source => '.' ,
764  
765 =head2 destination
766  
767 Directory to write output files into.
768  
769 B<N.B.:> If it doesn't exist and is needed, it will be created.
770  
771 =head2 draft_post_file_regex
772  
773 Regular expression for which files will be recognized as draft blog posts.
774  
775 FIXME should this be configurable?
776  
777 FIXME this and post_file_regex should probably be built based on a common
778 underlying "post_extensions_regex" attr...
779  
780 =head2 excerpt_separator
781  
782 String that distinguishes initial excerpt from "below the fold" content
783  
784 Defaults to "\n\n"
785  
786 =head2 include_dir
787  
788 Directory for template "include" files
789  
790 =head2 inputs
791  
792 Hashref of input files. Keys are file paths; values are what type of file the
793 system has classified that path as.
794  
795 =head2 layout_dir
796  
797 Directory where template files are located.
798  
799 =head2 layouts
800  
801 Hashref of L<HiD::Layout> objects, keyed by layout name.
802  
803 =head2 limit_posts
804  
805 If set, only this many blog post files will be processed during publishing.
806  
807 Setting this can significantly speed up publishing for sites with many blog posts.
808  
809 =head2 objects
810  
811 Array of objects (pages, posts, files) created during site processing.
812  
813 =head2 page_file_regex
814  
815 Regular expression for identifying "page" files.
816  
817 # FIXME should it be possible to set this from the config?
818  
819 =head2 pages
820  
821 Arrayref of L<HiD::Page> objects, populated during processing.
822  
823 =head2 plugin_dir
824  
825 Directory for plugins, which will be called after publish.
826  
827 =head2 plugins
828  
829 Plugins, which consume either of the L<HiD::Plugin> or L<HiD::Generator> roles.
830  
831 Plugins used to subclass L<HiD::Plugin>, but that behavior is deprecated and
832 will be removed on or after 13 Nov 2014.
833  
834 =head2 post_file_regex
835  
836 Regular expression for which files will be recognized as blog posts.
837  
838 FIXME should this be configurable?
839  
840 =head2 posts_dir
841  
842 Directory where blog posts are located.
843  
844 =head2 posts
845  
846 Arrayref of L<HiD::Post> objects, populated during processing.
847  
848 =head2 processor
849  
850 Slot to hold the L<HiD::Processor> object that will be used during the
851 publication process.
852  
853 =head2 processor_args
854  
855 Arguments to use when instantiating the L<processor> attribute.
856  
857 Can be an arrayref or a hashref.
858  
859 Defaults to appropriate Template Toolkit arguments.
860  
861 =head2 regular_files
862  
863 ArrayRef of L<HiD::File> objects, populated during processing.
864  
865 =head2 remove_unwritten_files ( Boolean )
866  
867 Boolean value controlling whether files found in the C<dest_dir> that weren't
868 produced by HiD should be removed. In other words, when this is true, after a
869 C<hid publish> run, only files produced by HiD will be found in the
870 C<dest_dir>.
871  
872 Defaults to true.
873  
874 =head2 source
875  
876 Base directory that all other paths are calculated relative to.
877  
878 =head2 tags
879  
880 Tags hash, contains (tag, posts) pairs
881  
882 =head2 time
883  
884 DateTime object from the start of the latest run of the system.
885  
886 Cannot be set via argument.
887  
888 =head2 written_files
889  
890 Hashref of files written out during the publishing process.
891  
892 =head1 METHODS
893  
894 =head2 get_config
895  
896 my $config_key_value = $self->get_config( $config_key_name );
897  
898 Given a config key name, returns a config key value.
899  
900 =head2 add_input
901  
902 $self->add_input( $input_file => $input_type );
903  
904 Record what input type a particular input file is.
905  
906 =head2 seen_input
907  
908 if( $self->seen_input( $input_file )) { ... }
909  
910 Check to see if a particular input file has been seen.
911  
912 =head2 get_layout_by_name
913  
914 my $hid_layout_obj = $self->get_layout_by_name( $name );
915  
916 Given a layout name (e.g., 'default') returns the corresponding L<HiD::Layout> object.
917  
918 =head2 add_object
919  
920 $self->add_object( $generated_object );
921  
922 Add an object to the set of objects generated during site processing.
923  
924 =head2 all_objects
925  
926 my @objects = $self->all_objects;
927  
928 Returns the list of all objects that have been generated.
929  
930 =head2 add_written_file
931  
932 $self->add_written_file( $file => 1 );
933  
934 Record that a file was written.
935  
936 =head2 all_written_files
937  
938 my @files = $self->all_written_files;
939  
940 Return the list of all files that were written out.
941  
942 =head2 wrote_file
943  
944 if( $self->wrote_file( $file )) { ... }
945  
946 Check to see if a particular file has been written out.
947  
948 =head2 publish
949  
950 $self->publish;
951  
952 Process files and generate output per the active configuration.
953  
954 =head1 CONTRIBUTORS
955  
956 =over 4
957  
958 =item *
959  
960 ChinaXing
961  
962 =item *
963  
964 reyjrar
965  
966 =back
967  
968 =head1 SEE ALSO
969  
970 =over 4
971  
972 =item *
973  
974 L<jekyll|http://jekyllrb.com/>
975  
976 =item *
977  
978 L<Papery>
979  
980 =item *
981  
982 L<StaticVolt>
983  
984 =back
985  
986 =head1 VERSION
987  
988 version 1.5
989  
990 =head1 AUTHOR
991  
992 John SJ Anderson <genehack@genehack.org>
993  
994 =head1 COPYRIGHT AND LICENSE
995  
996 This software is copyright (c) 2014 by John SJ Anderson.
997  
998 This is free software; you can redistribute it and/or modify it under
999 the same terms as the Perl 5 programming language system itself.
1000  
1001 =cut
« HiD/App/Command/server.pm ↑ Summary HiD/Generator/AtomFeed.pm »