| 1 |
package Plagger::Plugin::Publish::Planet; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use File::Copy::Recursive qw[rcopy]; |
|---|
| 6 |
use File::Spec; |
|---|
| 7 |
|
|---|
| 8 |
use HTML::Scrubber; |
|---|
| 9 |
|
|---|
| 10 |
our $VERSION = '0.01'; |
|---|
| 11 |
|
|---|
| 12 |
sub register { |
|---|
| 13 |
my($self, $context) = @_; |
|---|
| 14 |
$context->register_hook( |
|---|
| 15 |
$self, |
|---|
| 16 |
'publish.feed' => \&add_feed, |
|---|
| 17 |
); |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
sub add_feed { |
|---|
| 21 |
my($self, $context, $args) = @_; |
|---|
| 22 |
my $feed = $args->{feed}; |
|---|
| 23 |
if ($feed->id ne 'smartfeed:all') { |
|---|
| 24 |
$context->error("Publish::Planet requires SmartFeed::All to run."); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
$self->_sanitize_entries( |
|---|
| 28 |
$context, |
|---|
| 29 |
$feed, |
|---|
| 30 |
|
|---|
| 31 |
undef, |
|---|
| 32 |
HTML::Scrubber->new( |
|---|
| 33 |
rules => [ |
|---|
| 34 |
style => 0, |
|---|
| 35 |
script => 0, |
|---|
| 36 |
], |
|---|
| 37 |
default => [ 1, { '*' => 1, style => 0 } ], |
|---|
| 38 |
), |
|---|
| 39 |
); |
|---|
| 40 |
|
|---|
| 41 |
$self->_write_index( |
|---|
| 42 |
$context, |
|---|
| 43 |
$self->templatize($context, $feed), |
|---|
| 44 |
$self->conf->{dir} . '/index.html', |
|---|
| 45 |
); |
|---|
| 46 |
|
|---|
| 47 |
$self->_apply_skin( |
|---|
| 48 |
$context, |
|---|
| 49 |
$self->conf->{skin}, |
|---|
| 50 |
$self->conf->{dir}, |
|---|
| 51 |
); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
sub templatize { |
|---|
| 56 |
my($self, $context, $feed) = @_; |
|---|
| 57 |
my $tt = $context->template(); |
|---|
| 58 |
my $skin = $self->conf->{skin} || 'default'; |
|---|
| 59 |
|
|---|
| 60 |
$tt->process("$skin/template/index.tt", { |
|---|
| 61 |
%{ $self->conf->{template} }, |
|---|
| 62 |
feed => $feed, |
|---|
| 63 |
members => [ $context->subscription->feeds ], |
|---|
| 64 |
context => $context, |
|---|
| 65 |
}, \my $out) or $context->error($tt->error); |
|---|
| 66 |
$out; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
sub _sanitize_entries { |
|---|
| 70 |
my ($self, $context, $feed, $tidy, $scrubber) = @_; |
|---|
| 71 |
|
|---|
| 72 |
foreach my $entry ($feed->entries) { |
|---|
| 73 |
|
|---|
| 74 |
$entry->{body} = $scrubber->scrub($entry->{body}) if $scrubber; |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
sub _write_index { |
|---|
| 79 |
my ($self, $context, $index, $file) = @_; |
|---|
| 80 |
|
|---|
| 81 |
open my $out, ">:utf8", $file or $context->error("$file: $!"); |
|---|
| 82 |
print $out $index; |
|---|
| 83 |
close $out; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
sub _apply_skin { |
|---|
| 87 |
my ($self, $context, $skin_name, $output_dir) = @_; |
|---|
| 88 |
$context->log(debug => "Assets Directory: " . $self->assets_dir); |
|---|
| 89 |
|
|---|
| 90 |
my $static = File::Spec->catfile($self->assets_dir, $skin_name, 'static'); |
|---|
| 91 |
if (-e $static) { |
|---|
| 92 |
rcopy($static, $output_dir) or $context->log(error => "rcopy: $!"); |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
1; |
|---|
| 97 |
|
|---|