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

Revision 1288 (checked in by otsune, 14 years ago)

put generated time at Publish::CHTML index

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                      modified => (Plagger::Date->now),
79                  }),
80                  "$feed_path/index.html");
81
82     $self->add(+{
83         feed_link => './feeds/' . $feed->id . '/' . $self->id . '.html',
84         title  => $feed->title || '(no-title)',
85         lastdate => $feed->entries->[-1]->date,
86         count => scalar(@{$feed->entries}),
87     });
88 }
89
90 sub finalize {
91     my($self, $context) = @_;
92
93     return unless @{$self->feeds};
94     $self->write($self->work . '/' . $self->id . '.html',
95                  $self->templatize('chtml_index.tt', {
96                      conf => $self->conf,
97                      feeds => [ $self->feeds ],
98                      earlier => $self->earlier($self->work),
99                  }),
100                  $self->work . '/index.html');
101 }
102
103 sub write {
104     my ($self, $file, $chtml, $symlink) = @_;
105     open my $out, ">:encoding($self->{conf}->{encoding})", $file or $self->context->error("$file: $!");
106     local $PerlIO::encoding::fallback = Encode::FB_HTMLCREF;
107     print $out $chtml;
108     close $out;
109     $self->symlink($file, $symlink) if $symlink;
110 }
111
112 sub symlink {
113     my ($self, $old, $new) = @_;
114     unlink $new if -e $new;
115     symlink $old, $new;
116 }
117
118 sub earlier {
119     my ($self, $path) = @_;
120     my $earlier;
121     my $file = "$path/earlier";
122     if (open my $in, $file) {
123         $earlier = <$in>;
124         close $in;
125     }
126     open my $out, ">$file" or $self->context->error("$file: $!");
127     print $out $self->id;
128     close $out;
129     $earlier;
130 }
131 1;
Note: See TracBrowser for help on using the browser.