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

Revision 1701 (checked in by cheebow, 14 years ago)

Publish::PalmDoc?: fixed Garbled characters of feed title

  • Property svn:keywords set to Id
Line 
1 # Plagger::Plugin::Publish::PalmDoc
2 # $Id$
3 package Plagger::Plugin::Publish::PalmDoc;
4 use strict;
5 use base qw( Plagger::Plugin );
6
7 use Encode;
8 use File::Spec;
9 use Plagger::Date;
10 use Palm::PalmDoc;
11
12 sub register {
13     my($self, $context) = @_;
14     $context->register_hook(
15         $self,
16         'publish.feed' => \&feed,
17         'publish.finalize' => \&finalize,
18     )
19 }
20
21 sub feed {
22     my($self, $context, $args) = @_;
23    
24     push @{ $self->{_feeds} }, $args->{feed};
25 }
26
27 sub finalize {
28     my($self, $context, $args) = @_;
29    
30     my $path = $self->conf->{path}  || './';
31     my $prefix = $self->conf->{prefix} || 'PalmDoc';
32
33     my $feed_count = 0;
34     foreach my $feed (@{ $self->{_feeds} }) {
35         $feed_count++;
36         my $text;
37         $text = $self->makeText($feed, $context);
38         $text = encode("sjis", $text);
39
40         my $filename = $prefix . '-' . $feed_count . '.pdb';
41         my $outfile = File::Spec->catfile($path, $filename);
42         my $title = encode("sjis", $feed->title);
43         my $doc = Palm::PalmDoc->new({OUTFILE=>$outfile, TITLE=>$title});
44         $doc->compression(1);
45         $doc->body($text);
46         $doc->write_text();
47     }
48 }
49
50 sub makeEntryText {
51     my($self, $entry, $context) = @_;
52    
53     my $entry_text = $self->templatize('palmdoc.tt', {
54         entry => $entry,
55         now   => Plagger::Date->now,
56     });
57
58     $entry_text;
59 }
60
61 sub makeMeDocText {
62     my($self, $feed, $context) = @_;
63
64     my @entries = @{$feed->entries};
65     my $entry_num = @entries;
66
67     my $text;
68     $text = "#!Medoc index " . $entry_num . "\n";
69     my $toc;
70     my $body;
71     my $lines;
72     my $all_lines = 2 + $entry_num;
73     foreach my $entry (@entries) {
74         my $entry_text = $self->makeEntryText($entry, $context);
75         $lines = ($entry_text =~ tr/\n/\n/);
76         $toc .= $all_lines . ':' . $entry->title . "\n";
77         $body .= $entry_text;
78         $all_lines += $lines;
79     }
80    
81     $text .= $toc . $body;
82 }
83
84 sub makeDocText {
85     my($self, $feed, $context) = @_;
86
87     my $text;
88     foreach my $entry ($feed->entries) {
89         my $entry_text = $self->makeEntryText($entry, $context);
90         $text .= '[BM]' . $entry_text;
91     }   
92     $text .= '<[BM]>';
93
94     $text;
95 }
96
97 sub makeText {
98     my($self, $feed, $context) = @_;
99
100     my $text;
101     if ($self->conf->{medoc}) {
102         $text = $self->makeMeDocText($feed, $context)
103     } else {
104         $text = $self->makeDocText($feed, $context)
105     }
106
107     $text;
108 }
109
110 1;
111
112 __END__
113
114 =head1 NAME
115
116 Plagger::Plugin::Publish::PalmDoc - Publish as PalmDoc
117
118 =head1 SYNOPSIS
119
120   - module: Publish::PalmDoc
121     config:
122       path: /path/to/
123       prefix: PalmDoc
124       medoc: 1
125      
126 =head1 DESCRIPTION
127
128 This plugin publishes feeds as PalmDoc format.
129
130 =head1 CONFIG
131
132 =over 4
133
134 =item path
135
136 The directory to save the PalmDoc file.
137
138 =item prefix
139
140 The prefix to use for the output file. The filename will become <prefix-n.pdb>. (n: integer)
141
142 =item medoc
143
144 controls the output format. 1 for MeDoc format, 0 for Doc format.
145
146 =back
147
148 =head1 AUTHOR
149
150 Motokazu Sekine (CHEEBOW) @M-Logic, Inc.
151
152
153 =head1 SEE ALSO
154
155 L<Plagger>, L<Palm::PalmDoc>
156
157 =cut
Note: See TracBrowser for help on using the browser.