$HiD::Role::IsConverted::VERSION
=
'1.8'
;
use
5.014;
use
open
qw/ :std :utf8 /
;
requires
'get_default_layout'
;
has
content
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
required
=> 1 ,
);
has
converted_content
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1 ,
default
=>
sub
{
my
$self
=
shift
;
return
_convert_by_extension(
$self
->content ,
$self
->ext );
}
);
has
converted_excerpt
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1 ,
default
=>
sub
{
my
$self
=
shift
;
my
$converted_excerpt
= _convert_by_extension(
$self
->excerpt ,
$self
->ext );
if
(
$self
->excerpt ne
$self
->content ) {
$converted_excerpt
.=
$self
->readmore_link;
}
return
$converted_excerpt
;
},
);
has
hid
=> (
is
=>
'ro'
,
isa
=>
'HiD'
,
required
=> 1 ,
handles
=> [
qw/ get_config /
] ,
);
has
layouts
=> (
is
=>
'ro'
,
isa
=>
'HashRef[HiD::Layout]'
,
required
=> 1 ,
);
has
metadata
=> (
is
=>
'ro'
,
isa
=>
'HashRef'
,
default
=>
sub
{{}} ,
lazy
=> 1,
traits
=> [
'Hash'
] ,
handles
=> {
get_metadata
=>
'get'
,
},
);
has
readmore_link
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1 ,
default
=>
sub
{
my
$self
=
shift
;
if
(
defined
$self
->get_config(
'readmore_link'
)) {
my
$link
=
$self
->get_config(
'readmore_link'
);
my
$url
=
$self
->url;
$link
=~ s/__URL__/
$url
/;
return
$link
;
};
return
q{<p class="readmore"><a href="}
.
$self
->url
.
q{" class="readmore">read more</a></p>}
;
},
);
has
rendered_content
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1 ,
default
=>
sub
{
my
$self
=
shift
;
my
$layout_name
=
$self
->get_metadata(
'layout'
) //
$self
->get_default_layout;
my
$layout
=
$self
->layouts->{
$layout_name
} //
$self
->layouts->{
default
} //
die
"FIXME no default layout?"
;
my
$output
=
$layout
->render(
$self
->template_data );
return
$output
;
}
);
has
template_data
=> (
is
=>
'ro'
,
isa
=>
'HashRef'
,
lazy
=> 1 ,
default
=>
sub
{
my
$self
=
shift
;
my
$data
= {
baseurl
=>
$self
->hid->config->{baseurl} ,
content
=>
$self
->converted_content ,
page
=>
$self
->metadata ,
site
=>
$self
->hid ,
};
$data
->{post} =
$self
if
$self
->does(
'HiD::Role::IsPost'
);
$data
->{page}{url} =
$self
->url
if
$self
->can(
'url'
);
return
$data
;
},
);
around
BUILDARGS
=>
sub
{
my
$orig
=
shift
;
my
$class
=
shift
;
my
%args
= (
ref
$_
[0] and
ref
$_
[0] eq
'HASH'
) ? %{
$_
[0] } :
@_
;
unless
(
$args
{content} and
$args
{metadata} ) {
my
$file_content
= read_file(
$args
{input_filename},
binmode
=>
':utf8'
);
my
(
$metadata
,
$content
);
if
(
$file_content
=~ /^---/ ) {
(
$metadata
,
$content
) =
$file_content
=~ /^---\n?(.*?)---\n?(.*)$/ms;
}
elsif
(
$args
{input_filename} =~ /\.html?$/ ) {
die
"plain HTML file without YAML front matter"
}
else
{
$content
=
$file_content
;
$metadata
=
''
;
}
$args
{content} =
$content
;
$args
{metadata} = Load( encode(
'utf8'
,
$metadata
) ) // {};
}
return
$class
->
$orig
( \
%args
);
};
{
my
%conversion_extension_map
= (
markdown
=> [
'Text::Markdown'
,
'markdown'
] ,
mkdn
=> [
'Text::Markdown'
,
'markdown'
] ,
mk
=> [
'Text::Markdown'
,
'markdown'
] ,
md
=> [
'Text::Markdown'
,
'markdown'
] ,
mmd
=> [
'Text::MultiMarkdown'
,
'markdown'
] ,
textile
=> [
'Text::Textile'
,
'process'
] ,
);
sub
_convert_by_extension {
my
(
$content
,
$extension
) =
@_
;
return
$content
unless
exists
$conversion_extension_map
{
$extension
};
my
(
$module
,
$method
) = @{
$conversion_extension_map
{
$extension
}};
load_class(
$module
);
my
$converted
=
$module
->new->
$method
(
$content
);
return
$converted
;
}
}
no
Moose::Role;
1;