$HiD::Post::VERSION
=
'1.94'
;
with
'HiD::Role::IsConverted'
,
'HiD::Role::IsPost'
,
'HiD::Role::IsPublished'
;
use
5.014;
use
open
qw/ :std :utf8 /
;
sub
BUILD {
my
$self
=
shift
;
if
(
defined
$self
->get_metadata(
'published'
)
and not
$self
->get_metadata(
'published'
)) {
$self
->LOGWARN(
sprintf
"Skipping %s because 'published' flag is false"
,
$self
->input_filename
);
die
;
}
}
sub
get_default_layout {
'post'
}
sub
publish {
my
$self
=
shift
;
my
$out
= path(
$self
->output_filename );
my
$dir
=
$out
->parent;
$dir
->mkpath
unless
$dir
->is_dir;
$out
->spew_utf8(
$self
->rendered_content );
}
my
$date_regex
=
qr|([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})|
;
sub
_build_basename {
my
$self
=
shift
;
my
$ext
=
'.'
.
$self
->ext;
my
$basename
= path(
$self
->input_filename )->basename(
$ext
);
if
(
$self
->get_config(
'publish_drafts'
)) {
if
(
$self
->is_draft ) {
$basename
=~ s/^.*?
$date_regex
-//;
return
$basename
;
}
}
$basename
=~ s/^.*?
$date_regex
-// or
die
"no date in filename"
;
return
$basename
;
}
sub
_build_url {
my
$self
=
shift
;
my
%formats
= (
simple
=>
'/posts/%{year}/%{month}/%{title}.html'
,
date
=>
'/%{categories}s/%{year}s/%{month}s/%{day}s/%{title}s.html'
,
ii
=>
'%{year}s/%{month}s/%{day}s/%{title}s.html'
,
pretty
=>
'/%{categories}s/%{year}s/%{month}s/%{day}s/%{title}s/'
,
none
=>
'/%{categories}s/%{title}s.html'
,
);
my
$permalink_format
=
$self
->get_metadata(
'permalink'
) //
$self
->get_config(
'permalink'
) //
'date'
;
$permalink_format
=
$formats
{
$permalink_format
}
if
exists
$formats
{
$permalink_format
};
my
$categories
= (
join
'/'
, @{
$self
->categories } ) ||
''
;
my
$day
=
$self
->strftime(
'%d'
,
$self
->day );
my
$month
=
$self
->strftime(
'%m'
,
$self
->month );
my
$permalink
= errf
$permalink_format
, {
categories
=>
$categories
,
day
=>
$day
,
i_day
=>
$self
->day,
i_month
=>
$self
->month,
month
=>
$month
,
title
=>
$self
->basename ,
year
=>
$self
->year ,
};
$permalink
=~ s|//+|/|g;
return
$permalink
;
}
__PACKAGE__->meta->make_immutable;
1;