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

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

Publish::Excel: better not use strip_html, use Text API

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