| 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__ |
|---|
| 43 |
|
|---|
| 44 |
=head1 NAME |
|---|
| 45 |
|
|---|
| 46 |
Plagger::Plugin::Publish::JavaScript - publish links to entries as JavaScript |
|---|
| 47 |
|
|---|
| 48 |
=head1 SYNOPSIS |
|---|
| 49 |
|
|---|
| 50 |
- module: Publish::JavaScript |
|---|
| 51 |
config: |
|---|
| 52 |
dir: /path/to/www/js |
|---|
| 53 |
filename: %t.js |
|---|
| 54 |
|
|---|
| 55 |
=head1 DESCRIPTION |
|---|
| 56 |
|
|---|
| 57 |
This plugin publishes links to feed entries as an HTML embeddable |
|---|
| 58 |
JavaScript file. The JS file contains document.write() calls, and can |
|---|
| 59 |
be easily included in any HTML page using: |
|---|
| 60 |
|
|---|
| 61 |
<script src="/path/to/file.js"></script> |
|---|
| 62 |
|
|---|
| 63 |
in any place, like Blog sidebar widgets. |
|---|
| 64 |
|
|---|
| 65 |
The HTML emitted by the JavaScript code has exactly the same structure |
|---|
| 66 |
with Movable Type's standard sidebar module, so you can easily style |
|---|
| 67 |
using CSS. |
|---|
| 68 |
|
|---|
| 69 |
=head1 CONFIG |
|---|
| 70 |
|
|---|
| 71 |
=over 4 |
|---|
| 72 |
|
|---|
| 73 |
=item dir |
|---|
| 74 |
|
|---|
| 75 |
Directory to save JS files in. |
|---|
| 76 |
|
|---|
| 77 |
=item filename |
|---|
| 78 |
|
|---|
| 79 |
Filename to be used to create JS files. It defaults to C<%i.js>, but |
|---|
| 80 |
could be configured using the following formats like strftime: |
|---|
| 81 |
|
|---|
| 82 |
=over 8 |
|---|
| 83 |
|
|---|
| 84 |
=item * %u url |
|---|
| 85 |
|
|---|
| 86 |
=item * %l link |
|---|
| 87 |
|
|---|
| 88 |
=item * %t title |
|---|
| 89 |
|
|---|
| 90 |
=item * %i id |
|---|
| 91 |
|
|---|
| 92 |
=back |
|---|
| 93 |
|
|---|
| 94 |
=back |
|---|
| 95 |
|
|---|
| 96 |
=head1 AUTHOR |
|---|
| 97 |
|
|---|
| 98 |
Tatsuhiko Miyagawa |
|---|
| 99 |
|
|---|
| 100 |
=head1 SEE ALSO |
|---|
| 101 |
|
|---|
| 102 |
L<Plagger>, L<Plagger::Plugin::Publish::MTWidget> |
|---|
| 103 |
|
|---|
| 104 |
=cut |
|---|