| 1 |
package Plagger::Plugin::Publish::JSON; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use File::Spec; |
|---|
| 6 |
use JSON::Syck; |
|---|
| 7 |
use Plagger::Walker; |
|---|
| 8 |
use Plagger::Util; |
|---|
| 9 |
|
|---|
| 10 |
sub init { |
|---|
| 11 |
my $self = shift; |
|---|
| 12 |
$self->SUPER::init(@_); |
|---|
| 13 |
|
|---|
| 14 |
my $dir = $self->conf->{dir}; |
|---|
| 15 |
unless (-e $dir && -d _) { |
|---|
| 16 |
mkdir $dir, 0755 or Plagger->context->error("mkdir $dir: $!"); |
|---|
| 17 |
} |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
sub register { |
|---|
| 21 |
my($self, $context) = @_; |
|---|
| 22 |
$context->register_hook( |
|---|
| 23 |
$self, |
|---|
| 24 |
'publish.feed' => \&feed, |
|---|
| 25 |
); |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
sub feed { |
|---|
| 29 |
my($self, $context, $args) = @_; |
|---|
| 30 |
|
|---|
| 31 |
my $file = Plagger::Util::filename_for($args->{feed}, $self->conf->{filename} || '%i.json'); |
|---|
| 32 |
my $path = File::Spec->catfile($self->conf->{dir}, $file); |
|---|
| 33 |
$context->log(info => "writing output to $path"); |
|---|
| 34 |
|
|---|
| 35 |
local $JSON::Syck::ImplicitUnicode = 1; |
|---|
| 36 |
my $body = JSON::Syck::Dump(Plagger::Walker->serialize($args->{feed})); |
|---|
| 37 |
|
|---|
| 38 |
if (my $var = $self->conf->{varname}) { |
|---|
| 39 |
$body = "var $var = $body;"; |
|---|
| 40 |
} elsif (my $jsonp = $self->conf->{jsonp}) { |
|---|
| 41 |
$body = "$jsonp($body)"; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
$context->log(info => "Serializing " . $args->{feed}->id . " to $path"); |
|---|
| 45 |
|
|---|
| 46 |
open my $out, ">:utf8", $path or $context->error("$path: $!"); |
|---|
| 47 |
print $out $body; |
|---|
| 48 |
close $out; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
1; |
|---|
| 52 |
|
|---|
| 53 |
__END__ |
|---|
| 54 |
|
|---|
| 55 |
=head1 NAME |
|---|
| 56 |
|
|---|
| 57 |
Plagger::Plugin::Publish::JSON - Publish JSON data output |
|---|
| 58 |
|
|---|
| 59 |
=head1 SYNOPSIS |
|---|
| 60 |
|
|---|
| 61 |
- module: Publish::JSON |
|---|
| 62 |
config: |
|---|
| 63 |
dir: /path/to/data |
|---|
| 64 |
|
|---|
| 65 |
=head1 DESCRIPTION |
|---|
| 66 |
|
|---|
| 67 |
This plugin dumps feed data to JSON JavaScript Object Notation. |
|---|
| 68 |
|
|---|
| 69 |
=head1 CONFIG |
|---|
| 70 |
|
|---|
| 71 |
=over 4 |
|---|
| 72 |
|
|---|
| 73 |
=item dir |
|---|
| 74 |
|
|---|
| 75 |
Directory name to save. |
|---|
| 76 |
|
|---|
| 77 |
=item varname |
|---|
| 78 |
|
|---|
| 79 |
varname: foo |
|---|
| 80 |
|
|---|
| 81 |
Variable name to store JSON data. If set, .json file would include the |
|---|
| 82 |
variable declaration e.g.: |
|---|
| 83 |
|
|---|
| 84 |
var foo = { ... } |
|---|
| 85 |
|
|---|
| 86 |
Optional. |
|---|
| 87 |
|
|---|
| 88 |
=item jsonp |
|---|
| 89 |
|
|---|
| 90 |
jsonp: bar |
|---|
| 91 |
|
|---|
| 92 |
JSONP callback name to pass JSON data back. Optional. If set, .json |
|---|
| 93 |
file would wrap the returned data in a callback function, e.g.: |
|---|
| 94 |
|
|---|
| 95 |
bar({ ... }) |
|---|
| 96 |
|
|---|
| 97 |
Optional. |
|---|
| 98 |
|
|---|
| 99 |
=back |
|---|
| 100 |
|
|---|
| 101 |
=head1 AUTHOR |
|---|
| 102 |
|
|---|
| 103 |
Tatsuhiko Miyagawa |
|---|
| 104 |
|
|---|
| 105 |
=head1 SEE ALSO |
|---|
| 106 |
|
|---|
| 107 |
L<Plagger>, L<JSON::Syck> |
|---|
| 108 |
|
|---|
| 109 |
=cut |
|---|