| 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 | use URI; | ||
| 8 | |||
| 9 | our $VERSION = '0.02'; | ||
| 10 | |||
| 11 | sub register { | ||
| 12 | my($self, $context) = @_; | ||
| 13 | $context->register_hook( | ||
| 14 | $self, | ||
| 15 | 'publish.feed' => \&add_feed, | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub add_feed { | ||
| 20 | my($self, $context, $args) = @_; | ||
| 21 | |||
| 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 | my $theme = $self->conf->{theme} || $self->conf->{skin} || 'default'; # 'skin' as backward compatible | ||
| 28 | my $file = File::Spec->catfile($theme, 'template', 'index.tt'); | ||
| 29 | |||
| 30 | my $stash = $self->build_stash; | ||
| 31 | |||
| 32 | my $vars = { | ||
| 33 | %$stash, | ||
| 34 | feed => $feed, | ||
| 35 | entries => [ grep is_http($_->link), $feed->entries ], | ||
| 36 | members => [ $context->subscription->feeds ], | ||
| 37 | }; | ||
| 38 | |||
| 39 | $self->_write_index( | ||
| 40 | $context, | ||
| 41 | $self->templatize($file, $vars), | ||
| 42 | File::Spec->catfile($self->conf->{dir}, 'index.html'), | ||
| 43 | ); | ||
| 44 | |||
| 45 | $self->_apply_theme( | ||
| 46 | $context, | ||
| 47 | $theme, | ||
| 48 | $self->conf->{dir}, | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub is_http { | ||
| 53 | my $uri = URI->new(shift); | ||
| 54 | my $scheme = $uri->scheme or return; | ||
| 55 | $scheme eq 'http' or $scheme eq 'https'; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub build_stash { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | my $stash = $self->conf->{template} || {}; | ||
| 62 | |||
| 63 | # backward compatible for non Bundle::Planet users | ||
| 64 | $stash->{url}->{base} ||= ''; | ||
| 65 | $stash->{url}->{atom} ||= "$stash->{url}->{base}/smartfeed_all.atom"; | ||
| 66 | $stash->{url}->{rss} ||= "$stash->{url}->{base}/smartfeed_all.rss"; | ||
| 67 | |||
| 68 | # make style_url as absolute URIs | ||
| 69 | if (my $stylesheet = $stash->{style_url} and $stash->{url}->{base}) { | ||
| 70 | $stylesheet = [ $stylesheet ] unless ref $stylesheet; | ||
| 71 | $stash->{style_url} = [ map URI->new_abs($_, $stash->{url}->{base})->as_string, @$stylesheet ]; | ||
| 72 | } | ||
| 73 | |||
| 74 | $stash; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _write_index { | ||
| 78 | my ($self, $context, $index, $file) = @_; | ||
| 79 | |||
| 80 | $context->log(info => "Save Planet HTML to $file"); | ||
| 81 | open my $out, ">:utf8", $file or $context->error("$file: $!"); | ||
| 82 | print $out $index; | ||
| 83 | close $out; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _apply_theme { | ||
| 87 | my ($self, $context, $theme_name, $output_dir) = @_; | ||
| 88 | $context->log(debug => "Assets Directory: " . $self->assets_dir); | ||
| 89 | |||
| 90 | my $static = File::Spec->catfile($self->assets_dir, $theme_name, 'static'); | ||
| 91 | if (-e $static) { | ||
| 92 | rcopy($static, $output_dir) or $context->log(error => "rcopy: $!"); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | __END__ |