| 1 | # Plagger::Plugin::Publish::PalmDoc | ||
| 2 | # $Id: /mirror/plagger/trunk/plagger/lib/Plagger/Plugin/Publish/PalmDoc.pm 5425 2006-09-25T03:05:48.317033Z cheebow $ | ||
| 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__ |