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

Revision 1196 (checked in by jiro, 14 years ago)

added Notify::NetSend?, Publish::Excel plugins

Line 
1 package Plagger::Plugin::Publish::Excel;
2 use strict;
3 use warnings;
4 use base qw(Plagger::Plugin);
5
6 use Plagger::Util qw(strip_html);
7 use Spreadsheet::WriteExcel;
8
9 sub init {
10     my $self = shift;
11     $self->SUPER::init(@_);
12     Plagger->context->error("filename is missing")
13         unless exists $self->conf->{filename};
14 }
15
16 sub register {
17     my ($self, $context) = @_;
18     $context->register_hook(
19         $self,
20         'publish.init' => \&initialize,
21         'publish.feed' => \&feed,
22     );
23 }
24
25 sub initialize {
26     my ($self, $context) = @_;
27     my $workbook = Spreadsheet::WriteExcel->new($self->conf->{filename});
28     $self->{workbook} = $workbook;
29     $self->{body_format} = $workbook->add_format(text_wrap => 1);
30 }
31
32 sub feed {
33     my ($self, $context, $args) = @_;
34     my $feed = $args->{feed};
35     my $worksheet = $self->{workbook}->add_worksheet(escape_sheet_name($feed->title));
36     my $row = 0;
37     for my $entry ($feed->entries) {
38         my $col = 0;
39         $worksheet->write($row, $col++, $entry->date->format('Mail'));
40         $worksheet->write($row, $col++, $entry->title);
41         $worksheet->write($row, $col++, $entry->permalink);
42         $worksheet->write($row, $col++, strip_html($entry->body), $self->{body_format});
43         $row++;
44     }
45 }
46
47 sub escape_sheet_name {
48     my $name = shift;
49     $name =~ s![\[\]:*?/\\]! !g;
50     $name = substr $name, 0, 31 if length $name > 31;
51     $name;
52 }
53
54 1;
55 __END__
56
57 =head1 NAME
58
59 Plagger::Plugin::Publish::Excel - Publish feeds as Excel workbook
60
61 =head1 SYNOPSIS
62
63   - module: Publish::Excel
64     config:
65       filename: /path/to/workbook.xls
66
67 =head1 DESCRIPTION
68
69 This plugin creates Excel workbook.
70
71 =head1 AUTHOR
72
73 Jiro Nishiguchi
74
75 =head1 SEE ALSO
76
77 L<Plagger>, L<Spreadsheet::WriteExcel>
78
79 =cut
Note: See TracBrowser for help on using the browser.