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

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

Merge from refactoring-planet. YAY!

Line 
1 package Plagger::Plugin::Publish::CHTML;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Encode;
6 use Digest::MD5 qw(md5_hex);
7 use File::Path;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'publish.feed' => \&feed,
14         'publish.finalize' => \&finalize,
15     );
16     $self->chtml_init($context);
17 }
18
19 sub chtml_init {
20     my ($self, $context) = @_;
21     $self->{context} = $context;
22     $self->conf->{encoding} ||= 'cp932';
23     $self->{id} = time;
24     @{$self->{feeds}} = ();
25     unless ($self->conf->{work}) {
26         $context->error("Can't parse value in work");
27     }
28     $self->conf->{title} ||= __PACKAGE__;
29     $self->conf->{mobile_gw} = undef unless $self->conf->{mobile_gw} =~ m{^https?://.*?/}i;
30 }
31
32 sub id { shift->{id} }
33 sub context { shift->{context} }
34 sub work { shift->conf->{work} }
35
36 sub add {
37     my($self, $feed) = @_;
38     push @{ $self->{feeds} }, $feed;
39 }
40
41 sub feeds {
42     my $self = shift;
43     wantarray ? @{ $self->{feeds} } : $self->{feeds};
44 }
45
46 sub feed {
47     my($self, $context, $args) = @_;
48
49     my $feed = $args->{feed} or return;
50     my $feed_path = $self->work . '/feeds/' . $feed->id;
51     my $publish_path = "$feed_path/" . $self->id;
52
53     mkpath($publish_path);
54     foreach my $entry ($feed->entries) {
55         my $entry_id = md5_hex($entry->permalink);
56         $self->write("$publish_path/$entry_id.html",
57                      $self->templatize('chtml_entry.tt', {
58                          conf => $self->conf,
59                          feed => $feed,
60                          entry => $entry,
61                          strip_html => sub {
62                              my $html = shift;
63                              $html =~ s|\s{2,}||og;
64                              $html =~ s|<[bh]r.*?>|\n|ogi;
65                              $html =~ s|<.*?>||og;
66                              $html;
67                          },
68                      }));
69
70         $entry->{feed2entry_link} = $self->id . "/$entry_id.html";
71     }
72
73     $self->write("$publish_path.html",
74                  $self->templatize('chtml_feed.tt', {
75                      conf => $self->conf,
76                      feed => $feed,
77                      earlier => $self->earlier($feed_path),
78                  }),
79                  "$feed_path/index.html");
80
81     $self->add(+{
82         feed_link => './feeds/' . $feed->id . '/' . $self->id . '.html',
83         title  => $feed->title || '(no-title)',
84         lastdate => $feed->entries->[-1]->date,
85         count => scalar(@{$feed->entries}),
86     });
87 }
88
89 sub finalize {
90     my($self, $context) = @_;
91
92     return unless @{$self->feeds};
93     $self->write($self->work . '/' . $self->id . '.html',
94                  $self->templatize('chtml_index.tt', {
95                      conf => $self->conf,
96                      feeds => [ $self->feeds ],
97                      earlier => $self->earlier($self->work),
98                  }),
99                  $self->work . '/index.html');
100 }
101
102 sub write {
103     my ($self, $file, $chtml, $symlink) = @_;
104     open my $out, ">:encoding($self->{conf}->{encoding})", $file or $self->context->error("$file: $!");
105     local $PerlIO::encoding::fallback = Encode::FB_HTMLCREF;
106     print $out $chtml;
107     close $out;
108     $self->symlink($file, $symlink) if $symlink;
109 }
110
111 sub symlink {
112     my ($self, $old, $new) = @_;
113     unlink $new if -e $new;
114     symlink $old, $new;
115 }
116
117 sub earlier {
118     my ($self, $path) = @_;
119     my $earlier;
120     my $file = "$path/earlier";
121     if (open my $in, $file) {
122         $earlier = <$in>;
123         close $in;
124     }
125     open my $out, ">$file" or $self->context->error("$file: $!");
126     print $out $self->id;
127     close $out;
128     $earlier;
129 }
130 1;
Note: See TracBrowser for help on using the browser.