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

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

Publich::CHTML: update filepath generator to use id_safe

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 }
30
31 sub id { shift->{id} }
32 sub context { shift->{context} }
33 sub work { shift->conf->{work} }
34
35 sub add {
36     my($self, $feed) = @_;
37     push @{ $self->{feeds} }, $feed;
38 }
39
40 sub feeds {
41     my $self = shift;
42     wantarray ? @{ $self->{feeds} } : $self->{feeds};
43 }
44
45 sub feed {
46     my($self, $context, $args) = @_;
47
48     my $feed = $args->{feed} or return;
49     my $feed_path = $self->work . '/feeds/' . $feed->id_safe;
50     my $publish_path = "$feed_path/" . $self->id;
51
52     mkpath($publish_path);
53     foreach my $entry ($feed->entries) {
54         my $entry_id = md5_hex($entry->permalink);
55         $self->write("$publish_path/$entry_id.html",
56                      $self->templatize('chtml_entry.tt', {
57                          conf => $self->conf,
58                          feed => $feed,
59                          entry => $entry,
60                          strip_html => sub {
61                              my $html = shift;
62                              $html =~ s|\s{2,}||og;
63                              $html =~ s|<[bh]r.*?>|\n|ogi;
64                              $html =~ s|<.*?>||og;
65                              $html;
66                          },
67                      }));
68
69         $entry->{feed2entry_link} = $self->id . "/$entry_id.html";
70     }
71
72     $self->write("$publish_path.html",
73                  $self->templatize('chtml_feed.tt', {
74                      conf => $self->conf,
75                      feed => $feed,
76                      earlier => $self->earlier($feed_path),
77                      modified => (Plagger::Date->now),
78                  }),
79                  "$feed_path/index.html");
80
81     $self->add(+{
82         feed_link => './feeds/' . $feed->id_safe . '/' . $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                      modified => (Plagger::Date->now),
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.