HiD.pm

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