| 1 | package Plagger::Plugin::Publish::JavaScript; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use File::Spec; | ||
| 6 | use Template::Plugin::JavaScript; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my($self, $context) = @_; | ||
| 10 | $context->register_hook( | ||
| 11 | $self, | ||
| 12 | 'publish.feed' => \&feed, | ||
| 13 | ); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub init { | ||
| 17 | my $self = shift; | ||
| 18 | $self->SUPER::init(@_); | ||
| 19 | |||
| 20 | my $dir = $self->conf->{dir}; | ||
| 21 | unless (-e $dir && -d _) { | ||
| 22 | mkdir $dir, 0755 or Plagger->context->error("mkdir $dir: $!"); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | sub feed { | ||
| 27 | my($self, $context, $args) = @_; | ||
| 28 | |||
| 29 | my $file = Plagger::Util::filename_for($args->{feed}, $self->conf->{filename} || '%i.js'); | ||
| 30 | my $path = File::Spec->catfile($self->conf->{dir}, $file); | ||
| 31 | $context->log(info => "writing output to $path"); | ||
| 32 | |||
| 33 | my $body = $self->templatize('javascript.tt', { feed => $args->{feed} }); | ||
| 34 | |||
| 35 | open my $out, ">:utf8", $path or $context->error("$path: $!"); | ||
| 36 | print $out $body; | ||
| 37 | close $out; | ||
| 38 | } | ||
| 39 | |||
| 40 | 1; | ||
| 41 | |||
| 42 | __END__ |