our
$VERSION
=
'0.02'
;
sub
register {
my
(
$self
,
$context
) =
@_
;
$context
->register_hook(
$self
,
'publish.feed'
=> \
&add_feed
,
);
}
sub
add_feed {
my
(
$self
,
$context
,
$args
) =
@_
;
my
$feed
=
$args
->{feed};
if
(
$feed
->id ne
'smartfeed:all'
) {
$context
->error(
"Publish::Planet requires SmartFeed::All to run."
);
}
my
$theme
=
$self
->conf->{theme} ||
$self
->conf->{skin} ||
'default'
;
my
$file
= File::Spec->catfile(
$theme
,
'template'
,
'index.tt'
);
my
$stash
=
$self
->build_stash;
my
$vars
= {
%$stash
,
feed
=>
$feed
,
entries
=> [
grep
is_http(
$_
->
link
),
$feed
->entries ],
members
=> [
$context
->subscription->feeds ],
};
$self
->_write_index(
$context
,
$self
->templatize(
$file
,
$vars
),
File::Spec->catfile(
$self
->conf->{dir},
'index.html'
),
);
$self
->_apply_theme(
$context
,
$theme
,
$self
->conf->{dir},
);
}
sub
is_http {
my
$uri
= URI->new(
shift
);
my
$scheme
=
$uri
->scheme or
return
;
$scheme
eq
'http'
or
$scheme
eq
'https'
;
}
sub
build_stash {
my
$self
=
shift
;
my
$stash
=
$self
->conf->{template} || {};
$stash
->{url}->{base} ||=
''
;
$stash
->{url}->{atom} ||=
"$stash->{url}->{base}/smartfeed_all.atom"
;
$stash
->{url}->{rss} ||=
"$stash->{url}->{base}/smartfeed_all.rss"
;
if
(
my
$stylesheet
=
$stash
->{style_url} and
$stash
->{url}->{base}) {
$stylesheet
= [
$stylesheet
]
unless
ref
$stylesheet
;
$stash
->{style_url} = [
map
URI->new_abs(
$_
,
$stash
->{url}->{base})->as_string,
@$stylesheet
];
}
$stash
;
}
sub
_write_index {
my
(
$self
,
$context
,
$index
,
$file
) =
@_
;
$context
->
log
(
info
=>
"Save Planet HTML to $file"
);
open
my
$out
,
">:utf8"
,
$file
or
$context
->error(
"$file: $!"
);
print
$out
$index
;
close
$out
;
}
sub
_apply_theme {
my
(
$self
,
$context
,
$theme_name
,
$output_dir
) =
@_
;
$context
->
log
(
debug
=>
"Assets Directory: "
.
$self
->assets_dir);
my
$static
= File::Spec->catfile(
$self
->assets_dir,
$theme_name
,
'static'
);
if
(-e
$static
) {
rcopy(
$static
,
$output_dir
) or
$context
->
log
(
error
=>
"rcopy: $!"
);
}
}
1;