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