/
potd
261 lines (184 loc) · 6.18 KB
/
potd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Blosxom Plugin: potd -*- perl -*-
# Author: Kevin Scaldeferri (kevin+blosxom at scaldeferri dot com)
# Version: 0.01
# Blosxom Home/Docs/Licensing: http://www.blosxom.com/
package potd;
use strict;
use warnings;
# --- Configuration Variables ---
# pattern to use to match local image URLs in stories
my $image_pattern = qr/\.(jpg|jpeg|gif)/;
# these two patterns are used to transform the name of the image
# to generate the POTD. This would typically be used if you
# want to display full images in the main blog and thumbnails
# for the POTD. If either pattern is undef, no transformation will
# be done
my $from_pattern = "\."; # find first '.'
my $to_pattern = "-thumbnail."; # put '-thumbnail' before it
# frequency to pick a new POTD. Set to 0 to pick a new image
# for every page load
my $cache_expire = 60 * 60 * 24;
# use category-based or date-based permalinks
my $date_based_permalinks = 0;
# Set to 0 for no debugging or 5 for maximum verbosity
# or somewhere in between for something in between
my $debug_level = 1;
# -------------------------------
#
# Variables exported to templates
#
use vars qw($potd $potd_source);
use Data::Dumper;
my $cachefile = "${blosxom::plugin_state_dir}/.". __PACKAGE__ . ".cache";
sub debug {
my ($level, @msg) = @_;
if ($debug_level >= $level) {
print STDERR __PACKAGE__ . " debug $level: @msg\n";
}
1;
}
sub transform_image_name {
my $original = shift;
if (defined($from_pattern) && defined($to_pattern)) {
$original =~ s/$from_pattern/$to_pattern/o;
}
return $original;
}
sub file_to_permalink {
my ($file, $timestamp) = @_;
if ($date_based_permalinks) {
my ($dw,$mo,$mo_num,$da,$ti,$yr) = blosxom::nice_date($timestamp);
($file) = (split(/\//, $file))[-1];
$file =~ s/${blosxom::file_extension}$/html/o;
$file = "$blosxom::url/$yr/$mo_num/$da/$file";
} else {
# category based permalink
$file =~ s/${blosxom::datadir}/${blosxom::url}/o;
$file =~ s/${blosxom::file_extension}$/html/o;
}
return $file;
}
sub cache_valid {
my $cache;
if ( $cache_expire
&& ( $cache = do "$cachefile" )
&& ( time() - $cache->{timestamp} < $cache_expire )
) {
return $cache;
} else {
return undef; # not $cache!
}
}
#
# Plugin Subroutines follow
#
sub start {
debug(1, "loaded potd");
1;
}
sub filter {
my ($pkg, $files) = @_;
my $cache;
if (defined($cache = cache_valid()) ) {
debug(3, "using cache");
$potd = $cache->{potd};
$potd_source = $cache->{potd_source};
} else {
debug(3, "cache disabled or expired");
debug(1, "examining", scalar(keys %$files), "files");
my %images = ();
for my $filename (keys %$files) {
if (open my $fh, "<", $filename) {
debug(5, "opened $filename");
my $contents;
{
local $/;
$contents = <$fh>;
}
close $fh;
if (my @images = $contents =~ m/<img \s+
[^>]*
\bsrc=["']($image_pattern)["']
/oigx) {
for my $image (@images) {
debug(3, "found $image in $filename");
$images{$image} = $filename;
}
}
} else {
debug(1, "Couldn't open $filename : $!");
}
}
my @images = keys %images;
debug(5, scalar @images, "images found");
my $image = $images[int(rand(scalar @images))];
debug(5, "original image is $image");
$potd = transform_image_name($image);
debug(1, "potd is $potd");
my $file = $images{$image};
debug(5, "original file is $file");
$potd_source = file_to_permalink($file, $files->{$file});
debug(1, "potd_source is $potd_source");
$cache = { timestamp => time(),
potd => $potd,
potd_source => $potd_source };
if (open CACHE, ">", $cachefile) {
print CACHE Data::Dumper->Dump([$cache], ['cache']);
} else {
debug(1, "couldn't open cache file [$cachefile] : $!");
}
}
return 1;
}
1;
__END__
=head1 NAME
Blosxom Plug-in: potd (picture of the day)
=head1 SYNOPSIS
Randomly selects a photo that has been posted in the blog to be
the "picture of the day" (for an arbitrary value of day). Provides
the template variables:
* $potd::potd -- the URL to the POTD
* $potd::potd_source -- permalink to the entry containing the POTD
=head1 VERSION
0.01
=head1 Customization
=head2 Configuration variables
C<$image_pattern> is a regular expression used to pick images which
may be candidates for the POTD. The intent is typically to restrict
to images hosted in the same URL space as the blog, although the default
is simply any file with a jpg, jpeg, or gif suffix.
C<$from_pattern> and C<$to_pattern> are used to transform the original
url of the image in order to generate the POTD. This would typically
be used if you want to display full-size images in the main blog and
thumbnails for the POTD. If either pattern is undef, no
transformation will be done
C<$cache_expire> is the frequency with which to pick a new POTD. The
default is 1 day. This can be set to 0 to disable caching and pick a
new image for each request.
C<$debug_level> can be set to a value between 0 and 5; 0 will output
no debug information, while 5 will be very verbose. The default is 1,
and should be changed after you've verified the plugin is working
correctly.
=head1 AUTHOR
Kevin Scaldeferri (kevin+blosxom at scaldeferri dot com)
L<http://kevin.scaldeferri.com/>
=head1 BUGS
Probably; address bug reports and comments to me or to the Blosxom
mailing list L<http://www.yahoogroups.com/groups/blosxom>.
=head1 TODO
=over
=item * make img tag parsing more robust
=item * provide potd_alt containing any alt text for the image
=back
=head1 Giving Credit
The basic concept for this plugin was stolen from Tim Bray's "Ongoing",
L<http://www.tbray.org/ongoing/>
=head1 LICENSE
POTD Blosxom Plug-in
Copyright 2005, Kevin Scaldeferri
This code is released under the same license as Perl, namely the
Artistic License:
L<http://www.perl.com/pub/a/language/misc/Artistic.html>. (Note that
this is different from the license which Blosxom is distributed
under.)