| 1 |
package Plagger::Plugin::Publish::Planet; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use File::Copy::Recursive qw[rcopy]; |
|---|
| 6 |
|
|---|
| 7 |
our $VERSION = '0.01'; |
|---|
| 8 |
|
|---|
| 9 |
sub register { |
|---|
| 10 |
my($self, $context) = @_; |
|---|
| 11 |
$context->register_hook( |
|---|
| 12 |
$self, |
|---|
| 13 |
'publish.feed' => \&add_feed, |
|---|
| 14 |
); |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
sub add_feed { |
|---|
| 18 |
my($self, $context, $args) = @_; |
|---|
| 19 |
my $feed = $args->{feed}; |
|---|
| 20 |
if ($feed->id ne 'smartfeed:all') { |
|---|
| 21 |
$context->error("Publish::Planet requires SmartFeed::All to run."); |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
$self->_write_index( |
|---|
| 25 |
$context, |
|---|
| 26 |
$self->templatize($context, $feed), |
|---|
| 27 |
$self->conf->{dir} . '/index.html', |
|---|
| 28 |
); |
|---|
| 29 |
|
|---|
| 30 |
$self->_apply_skin( |
|---|
| 31 |
$context, |
|---|
| 32 |
$self->conf->{skin}, |
|---|
| 33 |
$self->conf->{dir}, |
|---|
| 34 |
); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
sub templatize { |
|---|
| 39 |
my($self, $context, $feed) = @_; |
|---|
| 40 |
my $tt = $context->template(); |
|---|
| 41 |
my $skin = $self->conf->{skin} || 'default'; |
|---|
| 42 |
|
|---|
| 43 |
$tt->process("$skin/template/index.tt", { |
|---|
| 44 |
feed => $feed, |
|---|
| 45 |
title => $self->conf->{title}, |
|---|
| 46 |
}, \my $out) or $context->error($tt->error); |
|---|
| 47 |
$out; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
sub _write_index { |
|---|
| 51 |
my ($self, $context, $index, $file) = @_; |
|---|
| 52 |
|
|---|
| 53 |
open my $out, ">:utf8", $file or $context->error("$file: $!"); |
|---|
| 54 |
print $out $index; |
|---|
| 55 |
close $out; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
sub _apply_skin { |
|---|
| 59 |
my ($self, $context, $skin_name, $output_dir) = @_; |
|---|
| 60 |
|
|---|
| 61 |
$context->log(debug => "Assets Directory: " . $self->assets_dir); |
|---|
| 62 |
|
|---|
| 63 |
rcopy( |
|---|
| 64 |
join('/', $self->assets_dir, $skin_name, 'static'), |
|---|
| 65 |
$output_dir, |
|---|
| 66 |
) or $context->error("rcopy: $!"); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
1; |
|---|
| 70 |
|
|---|