root/trunk/plagger/lib/Plagger/Plugin/Publish/Serializer.pm

Revision 1390 (checked in by miyagawa, 14 years ago)

add Publish::Serializer

Line 
1 package Plagger::Plugin::Publish::Serializer;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Data::Serializer;
6 use Plagger::Util;
7 use Plagger::Walker;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'publish.feed' => \&feed,
14         'plugin.init'  => \&initialize,
15     );
16 }
17
18 sub initialize {
19     my($self, $context, $args) = @_;
20     my $dir = $self->conf->{dir} or $context->error("dir is required");
21
22     unless (-e $dir && -d _) {
23         mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
24     }
25 }
26
27 sub feed {
28     my($self, $context, $args) = @_;
29
30     my $file = Plagger::Util::filename_for($args->{feed}, $self->conf->{filename});
31     my $path = File::Spec->catfile($self->conf->{dir}, $file);
32
33     my $data = $self->serialize( Plagger::Walker->serialize($args->{feed}) );
34     utf8::encode($data) if utf8::is_utf8($data);
35
36     $context->log(info => "Serializing " . $args->{feed}->id . " to $path");
37     open my $out, ">", $path or $context->error("$path: $!");
38     print $out $data;
39     close $out;
40 }
41
42 sub serialize {
43     my($self, $data) = @_;
44
45     my $selializer =  Data::Serializer->new(
46         serializer => $self->conf->{serializer} || 'Data::Dumper',
47         options    => $self->conf->{option} || {},
48     );
49
50     $selializer->raw_serialize($data);
51 }
52
53 1;
54 __END__
55
56 =head1 NAME
57
58 Plagger::Plugin::Publish::Serializer - Serialize feed data using Data::Serializer
59
60 =head1 SYNOPSIS
61
62   - module: Publish::Serializer
63     config:
64       serializer: YAML
65       filename: %i.yaml
66
67 =head1 DESCRIPTION
68
69 This plugin dumps feed data to whatever serialization format that Data::Serializer supports.
70
71 =head1 CONFIG
72
73 =over 4
74
75 =item dir
76
77 Directory to save the serialized data in. Required.
78
79 =item serializer
80
81   serializer: YAML::Syck
82
83 Serializer subclass that Data::Serializer uses. Defaults to I<Data::Dumper>.
84
85 =item filename
86
87 Filename to save the data to. Required.
88
89 =back
90
91 =head1 AUTHOR
92
93 Tatsuhiko Miyagawa
94
95 =head1 SEE ALSO
96
97 L<Plagger>
98
99 =cut
Note: See TracBrowser for help on using the browser.