File Coverage

blib/lib/HiD/Post.pm
Criterion Covered Total %
statement 55 58 94.8
branch 8 12 66.6
condition 8 10 80.0
subroutine 15 15 100.0
pod 2 3 66.6
total 88 98 89.8


line stmt bran cond sub pod time code
1             # ABSTRACT: Blog posts
2              
3              
4             package HiD::Post;
5             our $AUTHORITY = 'cpan:GENEHACK';
6             $HiD::Post::VERSION = '1.991';
7 12     12   44495 use Moose;
  12         345698  
  12         82  
8             with
9             'HiD::Role::IsConverted',
10             'HiD::Role::IsPost',
11             'HiD::Role::IsPublished';
12             with 'HiD::Role::DoesLogging'; # this one last b/c it needs method delegated
13             # by initial roles
14              
15 12     12   76492 use namespace::autoclean;
  12         5451  
  12         97  
16              
17 12     12   1075 use 5.014; # strict, unicode_strings
  12         35  
18 12     12   376 use utf8;
  12         33  
  12         83  
19 12     12   591 use autodie;
  12         10053  
  12         77  
20 12     12   58995 use warnings qw/ FATAL utf8 /;
  12         25  
  12         564  
21 12     12   325 use open qw/ :std :utf8 /;
  12         894  
  12         88  
22 12     12   1966 use charnames qw/ :full /;
  12         21193  
  12         91  
23              
24 12     12   3130 use Path::Tiny;
  12         7544  
  12         720  
25 12     12   373 use String::Errf qw/ errf /;
  12         10726  
  12         97  
26              
27              
28             sub BUILD {
29 57     57 0 78 my $self = shift;
30              
31 57 100 66     1451 if ( defined $self->get_metadata('published')
32             and not $self->get_metadata('published')) {
33 1         24 $self->LOGWARN(
34             sprintf "Skipping %s because 'published' flag is false" , $self->input_filename
35             );
36 1         838 die;
37             }
38             }
39              
40              
41 27     27 1 113 sub get_default_layout { 'post' }
42              
43              
44             sub publish {
45 53     53 1 3834 my $self = shift;
46              
47 53         1294 my $out = path( $self->output_filename );
48              
49 53         1252 my $dir = $out->parent;
50 53 100       2628 $dir->mkpath unless $dir->is_dir;
51              
52 53         10152 $out->spew_utf8( $self->rendered_content );
53             }
54              
55             # override
56             my $date_regex = qr|([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})|;
57              
58             sub _build_basename {
59 53     53   95 my $self = shift;
60 53         1059 my $ext = '.' . $self->ext;
61 53         1074 my $basename = path( $self->input_filename )->basename( $ext );
62              
63 53 50       3997 if( $self->get_config( 'publish_drafts' )) {
64 0 0       0 if ( $self->is_draft ) {
65             # not fatal to lack a date if you're a draft, but okay to have one
66 0         0 $basename =~ s/^.*?$date_regex-//;
67 0         0 return $basename;
68             }
69             }
70              
71 53 50       563 $basename =~ s/^.*?$date_regex-// or die "no date in filename";
72 53         1088 return $basename;
73             }
74              
75             sub _build_url {
76 53     53   101 my $self = shift;
77              
78             ### FIXME this is all horribly bigoted towards unix-ish file paths
79 53         355 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 53   66     1468 my $permalink_format = $self->get_metadata( 'permalink' ) //
      100        
88             $self->get_config('permalink') // 'date';
89              
90             $permalink_format = $formats{$permalink_format}
91 53 100       191 if exists $formats{$permalink_format};
92              
93 53   100     93 my $categories = ( join '/' , @{ $self->categories } ) || '';
94 53         258 my $day = $self->strftime( '%d' , $self->day );
95 53         2411 my $month = $self->strftime( '%m' , $self->month );
96              
97 53         1618 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 53         13153 $permalink =~ s|//+|/|g;
108              
109 53         1305 return $permalink;
110             }
111              
112             __PACKAGE__->meta->make_immutable;
113             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.991
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