root/trunk/plagger/lib/Plagger/Plugin/Widget/Simple.pm

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

refacored EntryFullText? to use load_assets. Plugin->load_assets can take filename as its 1st argument to make it easier. Callback function now takes base filename as 2nd parameter

Line 
1 package Plagger::Plugin::Widget::Simple;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Encode;
6 use HTML::Entities;
7 use URI;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'publish.entry.fixup' => \&add,
14         'plugin.init'         => \&initialize,
15     );
16 }
17
18 sub initialize {
19     my($self, $context, $args) = @_;
20
21     if (my $name = $self->conf->{widget}) {
22         my $found;
23         $self->load_assets(
24             "$name.yaml",
25             sub {
26                 my $data = YAML::LoadFile(shift);
27                 $self->{conf} = { %{$self->{conf}}, %$data };
28                 $found++;
29             },
30         );
31
32         unless ($found) {
33             $context->log(error => "Can't find widget for $name");
34         }
35     }
36 }
37
38 sub add {
39     my($self, $context, $args) = @_;
40
41     my $widget = Plagger::Plugin::Widget::Simple::Widget->new;
42     $widget->{feed}   = $args->{entry}->source || $args->{feed};
43     $widget->{plugin} = $self;
44
45     $args->{entry}->add_widget($widget);
46 }
47
48 package Plagger::Plugin::Widget::Simple::Widget;
49
50 sub new { bless {}, shift }
51
52 sub feed { shift->{feed} }
53 sub plugin { shift->{plugin} }
54
55 sub value {
56     my($self, $string, $args) = @_;
57
58     if ($string =~ /\$/) { # DWIM
59         $string = eval $string;
60         Plagger->context->log(error => $@) if $@;
61         utf8::encode($string) if utf8::is_utf8($string);
62     }
63
64     $string;
65 }
66
67 sub html {
68     my($self, $entry) = @_;
69     my $uri = URI->new($self->plugin->conf->{link});
70
71     my $args = { entry => $entry, feed => $self->{feed} };
72
73     if (my $append = $self->plugin->conf->{append}) {
74         $uri->path( $uri->path . $self->value($append, $args) );
75     }
76
77     if (my $query = $self->plugin->conf->{query}) {
78         if (ref $query) {
79             my @query = map {
80                 ($_ => $self->value($query->{$_}, $args));
81             } sort keys %$query;
82             $uri->query_form(@query);
83         } else {
84             $query = $self->value($query, $args);
85             $uri->query($query);
86         }
87     }
88
89     my $url = HTML::Entities::encode($uri->as_string);
90
91     my $content;
92     if (my $template = $self->plugin->conf->{content_dynamic}) {
93         $content = $self->plugin->templatize(\$template, $args);
94     } else {
95         $content = $self->plugin->conf->{content};
96     }
97
98     return qq(<a href="$url">$content</a>);
99 }
100
101 1;
102 __END__
103
104 =head1 NAME
105
106 Plagger::Plugin::Widget::Simple - Simple widget creation using config
107
108 =head1 SYNOPSIS
109
110   - module: Widget::Simple
111     config:
112       link: http://www.example.com/
113       content_dynamic: "Entry from [% entry.author %]"
114
115 =head1 DESCRIPTION
116
117 Widget::Simple is a plugin that allows you to write your own widget
118 using a simple configuration file.
119
120 =head1 CONFIG
121
122 =over 4
123
124 =item link
125
126   link: http://example.com/add
127
128 URL that the widget links to. Required.
129
130 =item query
131
132   query:
133     version: 4
134     url: $args->{entry}->url
135
136 Query parameter to append to the URL. If the value contains C<$>,
137 it'll be automatically eval()ed. Optional.
138
139 =item content
140
141   content: <img src="http://example.com/img.gif" alt="foo" />
142
143 Content to display in a widget. HTML tags will be displayed as is and
144 thus any HTML meta characters have to be escaped. Required, if you
145 don't use I<content_dynamic>.
146
147 =item content_dynamic
148
149   content_dynamic: "Entry from [% entry.author | html %]"
150
151 If you want to dyncamically generate the content of widget, use
152 I<content_dynamic> instead. Value of the content_dynamic is compiled
153 and rendered using Template-Toolkit. Required, if you don't use
154 I<content>.
155
156 =back
157
158 =head1 AUTHOR
159
160 Tatsuhiko Miyagawa
161
162 =head1 SEE ALSO
163
164 L<Plagger>
165
166 =cut
Note: See TracBrowser for help on using the browser.