root/trunk/plagger/lib/Plagger.pm

Revision 2 (checked in by miyagawa, 15 years ago)

import Plagger

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger;
2 use strict;
3 our $VERSION = '0.10';
4
5 use 5.8.1;
6 use Carp;
7 use Data::Dumper;
8 use YAML;
9 use UNIVERSAL::require;
10
11 use Plagger::Date;
12 use Plagger::Entry;
13 use Plagger::Feed;
14 use Plagger::Update;
15 use Template;
16
17 our $TT;
18
19 sub context { undef }
20
21 sub bootstrap {
22     my($class, %opt) = @_;
23
24     my $self = bless {
25         conf  => {},
26         stash => {},
27         update => Plagger::Update->new,
28     }, $class;
29
30     my $config;
31     if (-e $opt{config} && -r _) {
32         $config = YAML::LoadFile($opt{config});
33         $self->{conf} = $config->{global};
34     } else {
35         croak "Plagger->bootstrap: $opt{config}: $!";
36     }
37
38     local *Plagger::context = sub { $self };
39
40     $self->load_plugins(@{ $config->{plugins} || [] });
41     $self->run();
42 }
43
44 sub conf   { $_[0]->{conf}  }
45 sub stash  { $_[0]->{stash} }
46 sub update { $_[0]->{update} }
47
48 sub load_plugins {
49     my($self, @plugins) = @_;
50
51     if ($self->conf->{plugin_path}) {
52         unshift @INC, @{ $self->conf->{plugin_path} };
53     }
54
55     for my $plugin (@plugins) {
56         $self->load_plugin($plugin);
57     }
58 }
59
60 sub load_plugin {
61     my($self, $config) = @_;
62
63     my $module = delete $config->{module};
64     $module =~ s/^Plagger::Plugin:://;
65     $module = "Plagger::Plugin::$module";
66     $module->require or warn $@;
67
68     $self->log(info => "plugin $module loaded.");
69
70     my $plugin = $module->new($config);
71     $plugin->register($self);
72 }
73
74 sub register_hook {
75     my($self, $plugin, @hooks) = @_;
76     while (my($hook, $callback) = splice @hooks, 0, 2) {
77         push @{ $self->{hooks}->{$hook} }, +{
78             callback => $callback,
79             plugin   => $plugin,
80         };
81     }
82 }
83
84 sub run_hook {
85     my($self, $hook, @args) = @_;
86     for my $action (@{ $self->{hooks}->{$hook} }) {
87         $action->{callback}->($action->{plugin}, $self, @args);
88     }
89 }
90
91 sub run {
92     my $self = shift;
93
94     $self->run_hook('subscription.load');
95     $self->run_hook('subscription.aggregate');
96
97     for my $feed ($self->update->feeds) {
98         $self->run_hook('publish.notify', $feed);
99     }
100
101     $self->run_hook('publish.finalize');
102 }
103
104 sub log {
105     my($self, $level, $msg) = @_;
106     my $caller = caller(0);
107     chomp($msg);
108     warn "$caller: $msg\n";
109 }
110
111 sub dumper {
112     my($self, $stuff) = @_;
113     local $Data::Dumper::Indent = 1;
114     $self->log(debug => Dumper($stuff));
115 }
116
117 sub template {
118     my $self = shift;
119     unless ($TT) {
120         my $path = $self->conf->{template_path} || 'templates';
121         $TT = Template->new({ INCLUDE_PATH => [ $path, "$path/plugins" ] });
122     }
123     $TT;
124 }
125
126 1;
127 __END__
128
129 =head1 NAME
130
131 Plagger - Pluggable RSS/Atom Aggregator
132
133 =head1 SYNOPSIS
134
135   use Plagger;
136
137 =head1 DESCRIPTION
138
139 Plagger is a pluggable RSS/Atom feed aggregator.
140
141 =head1 AUTHOR
142
143 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
144
145 =head1 COPYRIGHT
146
147 This library is free software; you can redistribute it and/or modify
148 it under the same terms as Perl itself.
149
150 =head1 SEE ALSO
151
152 L<http://plagger.bulknews.net/>
153
154 =cut
Note: See TracBrowser for help on using the browser.