HiD/Post.pm

filter: Statementstmt 95.4% 63 / 66 Branchbran 66.6% 8 / 12 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 15 / 15 Podpod 66.6% 2 / 3 totaltotal 89.1% 99 / 111 CC 11 SCAR 10.5
CC
11
Cov
90%
_build_basename
5.6
_build_url
5.0
BUILD
3.0
CRAP 2.9
« HiD/Layout.pm ↑ Summary HiD/Page.pm »
1 # ABSTRACT: Blog posts
2  
3  
4 package HiD::Post;
5 18 our $AUTHORITY = 'cpan:GENEHACK';
6 18 $HiD::Post::VERSION = '1.96';
7 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 with
9 'HiD::Role::IsConverted',
10 'HiD::Role::IsPost',
11 'HiD::Role::IsPublished';
12 18 with 'HiD::Role::DoesLogging'; # this one last b/c it needs method delegated
13 # by initial roles
14  
15 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 18 ▶use 5.014; # strict, unicode_strings
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use warnings qw/ FATAL utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use open qw/ :std :utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use charnames qw/ :full /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 18 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use String::Errf qw/ errf /;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 sub BUILD {
29 57 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
30  
31 57 ▶ if ( defined $self->get_metadata('published')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_metadata("published") and not $self->get_metadata("published")) 1 56 57
Condition66%2/3
defined $self->get_metadata("published") and not $self->get_metadata("published")
!ll&&!rl&&r
5601
MC/DC50%1/2
defined $self->get_metadata("published") and not $self->get_metadata("published")
defined $self->get_metadata("published") not $self->get_metadata("published")
Truth table66%2/3
A and not(B)
A= defined $self->get_metadata("published")B= not $self->get_metadata("published")
ABresult
0X0
100
111
32 and not $self->get_metadata('published')) {
33 1 $self->LOGWARN(
34 sprintf "Skipping %s because 'published' flag is false" , $self->input_filename
35 );
36 1 die;
37 }
38 }
39  
40  
41 11 ▶sub get_default_layout { 'post' }
Subroutine get_default_layout: called CC=1 SCAR=0.0
Pod documented
42  
43  
44 sub publish {
45 19 ▶ my $self = shift;
Subroutine publish: called CC=2 SCAR=6.9
Pod documented
46  
47 19 my $out = path( $self->output_filename );
48  
49 19 my $dir = $out->parent;
50 19 ▶ $dir->mkpath unless $dir->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->is_dir 9 10 19
51  
52 19 $out->spew_utf8( $self->rendered_content );
53 }
54  
55 # override
56 18 my $date_regex = qr|([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})|;
57  
58 sub _build_basename {
59 20 ▶ my $self = shift;
Subroutine _build_basename: called CC=4 SCAR=17.3
60 20 my $ext = '.' . $self->ext;
61 20 my $basename = path( $self->input_filename )->basename( $ext );
62  
63 20 ▶ if( $self->get_config( 'publish_drafts' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_config("publish_drafts")) 0 20 20
64 0 ▶ if ( $self->is_draft ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_draft) 0 0 0
65 # not fatal to lack a date if you're a draft, but okay to have one
66 0 $basename =~ s/^.*?$date_regex-//;
67 0 return $basename;
68 }
69 }
70  
71 20 ▶ $basename =~ s/^.*?$date_regex-// or die "no date in filename";
Branch: 1 branch
BranchTrueFalseTotal
$basename =~ s/^.*?$date_regex-//u or die "no date in filename" 0 20 20
72 20 return $basename;
73 }
74  
75 sub _build_url {
76 23 ▶ my $self = shift;
Subroutine _build_url: called CC=5 SCAR=16.1
77  
78 ### FIXME this is all horribly bigoted towards unix-ish file paths
79 23 my %formats = (
80 simple => '/posts/%{year}/%{month}/%{title}.html',
81 date => '/%{categories}s/%{year}s/%{month}s/%{day}s/%{title}s.html' ,
82 ii => '%{year}s/%{month}s/%{day}s/%{title}s.html' ,
83 pretty => '/%{categories}s/%{year}s/%{month}s/%{day}s/%{title}s/' ,
84 none => '/%{categories}s/%{title}s.html' ,
85 );
86  
87 23 ▶ my $permalink_format = $self->get_metadata( 'permalink' ) //
Condition80%4/5
($self->get_metadata("permalink") // $self->get_config("permalink")) // "date"
l!l
1112
$self->get_metadata("permalink") // $self->get_config("permalink")
l!l&&r!l&&!r
11012
MC/DC50%1/2
($self->get_metadata("permalink") // $self->get_config("permalink")) // "date"
$self->get_metadata("permalink") $self->get_config("permalink")
Truth table66%2/3
A // B
A= $self->get_metadata("permalink")B= $self->get_config("permalink")
ABresult
1X1
011
000
88 $self->get_config('permalink') // 'date';
89  
90 $permalink_format = $formats{$permalink_format}
91 23 ▶ if exists $formats{$permalink_format};
Branch: 1 branch
BranchTrueFalseTotal
if exists $formats{$permalink_format} 20 3 23
92  
93 23 ▶ my $categories = ( join '/' , @{ $self->categories } ) || '';
Condition100%2/2
join('/', @{$self->categories;}) || ''
l!l
419
MC/DC100%1/1
join('/', @{$self->categories;}) || ''
join('/', @{$self->categories;})
Truth table100%2/2
A
A= join('/', @{$self->categories;})
Aresult
11
00
94 23 my $day = $self->strftime( '%d' , $self->day );
95 19 my $month = $self->strftime( '%m' , $self->month );
96  
97 19 my $permalink = errf $permalink_format , {
98 categories => $categories ,
99 day => $day ,
100 i_day => $self->day,
101 i_month => $self->month,
102 month => $month ,
103 title => $self->basename ,
104 year => $self->year ,
105 };
106  
107 19 $permalink =~ s|//+|/|g;
108  
109 19 return $permalink;
110 }
111  
112 18 __PACKAGE__->meta->make_immutable;
113 18 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 HiD::Post - Blog posts
124  
125 =head1 SYNOPSIS
126  
127 my $post = HiD::Post->new({
128 dest_dir => 'path/to/output/dir' ,
129 hid => $master_hid_object ,
130 input_filename => 'path/to/file/for/this/post' ,
131 layouts => $hashref_of_hid_layout_objects,
132 });
133  
134 =head1 DESCRIPTION
135  
136 Class representing a "blog post" object.
137  
138 =head1 METHODS
139  
140 =head2 get_default_layout
141  
142 The default layout used when publishing a L<HiD::Post> object. (Defaults to 'post'.)
143  
144 =head2 publish
145  
146 Publish -- convert, render through any associated layouts, and write out to
147 disk -- this data from this object.
148  
149 =for Pod::Coverage BUILD
150  
151 =head1 VERSION
152  
153 version 1.96
154  
155 =head1 AUTHOR
156  
157 John SJ Anderson <genehack@genehack.org>
158  
159 =head1 COPYRIGHT AND LICENSE
160  
161 This software is copyright (c) 2015 by John SJ Anderson.
162  
163 This is free software; you can redistribute it and/or modify it under
164 the same terms as the Perl 5 programming language system itself.
165  
166 =cut
« HiD/Layout.pm ↑ Summary HiD/Page.pm »