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

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

Merge from refactoring-planet. YAY!

Line 
1 package Plagger::Plugin::Publish::Planet;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use File::Copy::Recursive qw[rcopy];
6 use File::Spec;
7 use URI;
8
9 our $VERSION = '0.02';
10
11 sub register {
12     my($self, $context) = @_;
13     $context->register_hook(
14         $self,
15         'publish.feed' => \&add_feed,
16     );
17 }
18
19 sub add_feed {
20     my($self, $context, $args) = @_;
21
22     my $feed = $args->{feed};
23     if ($feed->id ne 'smartfeed:all') {
24         $context->error("Publish::Planet requires SmartFeed::All to run.");
25     }
26
27     my $theme = $self->conf->{theme} || $self->conf->{skin} || 'default'; # 'skin' as backward compatible
28     my $file = File::Spec->catfile($theme, 'template', 'index.tt');
29
30     my $stash = $self->build_stash;
31
32     my $vars = {
33         %$stash,
34         feed    => $feed,
35         entries => [ grep is_http($_->link), $feed->entries ],
36         members => [ $context->subscription->feeds ],
37     };
38
39     $self->_write_index(
40         $context,
41         $self->templatize($file, $vars),
42         File::Spec->catfile($self->conf->{dir}, 'index.html'),
43     );
44
45     $self->_apply_theme(
46         $context,
47         $theme,
48         $self->conf->{dir},
49     );
50 }
51
52 sub is_http {
53     my $uri = URI->new(shift);
54     my $scheme = $uri->scheme or return;
55     $scheme eq 'http' or $scheme eq 'https';
56 }
57
58 sub build_stash {
59     my $self = shift;
60
61     my $stash = $self->conf->{template} || {};
62
63     # backward compatible for non Bundle::Planet users
64     $stash->{url}->{base} ||= '';
65     $stash->{url}->{atom} ||= "$stash->{url}->{base}/smartfeed_all.atom";
66     $stash->{url}->{rss}  ||= "$stash->{url}->{base}/smartfeed_all.rss";
67
68     # make style_url as absolute URIs
69     if (my $stylesheet = $stash->{style_url} and $stash->{url}->{base}) {
70         $stylesheet = [ $stylesheet ] unless ref $stylesheet;
71         $stash->{style_url} = [ map URI->new_abs($_, $stash->{url}->{base})->as_string, @$stylesheet ];
72     }
73
74     $stash;
75 }
76
77 sub _write_index {
78     my ($self, $context, $index, $file) = @_;
79
80     $context->log(info => "Save Planet HTML to $file");
81     open my $out, ">:utf8", $file or $context->error("$file: $!");
82     print $out $index;
83     close $out;
84 }
85
86 sub _apply_theme {
87     my ($self, $context, $theme_name, $output_dir) = @_;
88     $context->log(debug => "Assets Directory: " . $self->assets_dir);
89
90     my $static = File::Spec->catfile($self->assets_dir, $theme_name, 'static');
91     if (-e $static) {
92         rcopy($static, $output_dir) or $context->log(error => "rcopy: $!");
93     }
94 }
95
96 1;
97
98 __END__
99
100 =head1 NAME
101
102 Plagger::Plugin::Publish::Planet - Planet XHTML publisher
103
104 =head1 SYNOPSIS
105
106   - module: Publish::Planet
107     rule:
108       expression: $args->{feed}->id eq 'smartfeed:all'
109     config:
110       dir: /path/to/htdocs
111       theme: sixapart-std
112
113 =head1 DESCRIPTION
114
115 This plugin generates XHTML out of aggregated feeds suitable to put on
116 the web as "Blog aggregator" like Python Planet does.
117
118 =head1 CONFIG
119
120 =over 4
121
122 =item dir
123
124 Directory to save output XHTML and CSS files in. Required.
125
126 =item theme
127
128 Name of "theme" to use as an XHTML template. Available options are
129 I<default> and I<sixapart-std>. Optional and defaults to 'default'.
130
131 =item template
132
133 Stash variables to pass to template. Example:
134
135   template:
136     style_url: http://example.com/foo.css
137     url:
138       base: http://example.org/planet/
139
140 =over 8
141
142 =item style_url
143
144   style_url: http://www.example.com/style.css
145
146 URL of stylesheet to use in templates. You can pass multiple URLs by passing an array. Optional.
147
148 =item url
149
150   url:
151     base: http://example.com/planet/
152
153 URL to be used as a Planet base. This URL is used as a base URL for
154 RSS/Atom feeds and stylesheet if they're relative.. Optional.
155
156 =back
157
158 =back
159
160 =head1 EXAMPLES
161
162 You can see a couple of Publish::Planet powered sites.
163
164 L<http://plagger.org/planet/>
165
166 L<http://planet.yapcchicago.org/>
167
168 =head1 AUTHOR
169
170 Casey West
171
172 Tatsuhiko Miyagawa
173
174 =head1 SEE ALSO
175
176 L<Plagger>, L<http://plagger.org/planet/>, L<http://planetplanet.org/>
Note: See TracBrowser for help on using the browser.