Add __END_CONFIG__ token to remaining gavinc plugins.
[matthijs/upstream/blosxom-plugins.git] / gavinc / tagcloud
1 # Blosxom Plugin: tagcloud
2 # Author(s): Gavin Carr <gavin@openfusion.com.au>
3 # Version: 0.002000
4 # Documentation: See the bottom of this file or type: perldoc tagcloud
5
6 package tagcloud;
7
8 use strict;
9 use vars qw(%config $cloud);
10
11 # Uncomment next line to enable debug output (don't uncomment debug() lines)
12 #use Blosxom::Debug debug_level => 1;
13
14 # --- Configuration defaults -----
15
16 %config = (
17
18   # tagcloud requires a hashref containing 'tag => count' pairs
19   tag_hashref           => $tags::tag_counts,
20
21   # Formatting options
22   tagcloud_prefix       => qq(<p class="tagcloud">\n),
23   tagcloud_suffix       => qq(</p>\n),
24   show_tag_no           => 0,
25   min_tag_no            => 2,
26   min_size              => 75,
27   max_size              => 200,
28   entry_type            => 'posting',
29
30   # Tags to omit from tagcloud
31   tagcloud_blacklist => [ 'Now Playing' ],
32
33 );
34
35 # ---------------------------------
36 # __END_CONFIG__
37
38 my %tagcloud_blacklist = map { $_ => 1 } @{$config{tagcloud_blacklist}};
39
40 $cloud = '';
41
42 sub start {
43     eval { $config{tag_hashref} }
44         or warn "[tagcloud] tag_hashref not set - skipping" 
45             and return 0;
46     1;
47 }
48
49 sub story {
50     my $tag_hashref = $config{tag_hashref};
51     return unless keys %$tag_hashref;
52  
53     my %tags = ();
54     my $min = undef;
55     my $max = 1;
56     for (keys %$tag_hashref) {
57         next if exists $tagcloud_blacklist{ $_ };
58         next if $tag_hashref->{$_} < $config{min_tag_no};
59         $tags{$_} = $tag_hashref->{$_};
60         $min = $tag_hashref->{$_} if $min > $tag_hashref->{$_} || ! $min;
61         $max = $tag_hashref->{$_} if $max < $tag_hashref->{$_};
62     }
63     return unless keys %tags;
64
65     my $diff = $max - $min;
66
67     my @tagcloud = ();
68     for my $tag (sort { lc $a cmp lc $b } keys %tags) {
69         my $label = $tag;
70         $label .= " ($tags{$tag})" if $config{show_tag_no};
71         my $url_tag = _url_escape($tag);
72         my $url = "$blosxom::url/tags/$url_tag";
73         my $plural = $tags{$tag} == 1 ? '' : 's';
74         my $title = sprintf "%s %s%s tagged", $tags{$tag}, $config{entry_type}, $plural;
75         my $tag_percent = int($config{min_size} + 
76             ((($config{max_size}-$config{min_size})/$diff) * ($tags{$tag}-$min+1)));
77         my $style = 'white-space: nowrap;';
78         $style .= qq(font-size: $tag_percent%;") if $diff;
79         push @tagcloud, qq(<a href="$url" rel="tag" title="$title" style="$style">$label</a>);
80     }
81
82     $cloud = sprintf "%s%s%s", 
83         $config{tagcloud_prefix}, join(', ', @tagcloud), $config{tagcloud_suffix};
84 }
85
86 sub _url_escape {
87     my $s = shift;
88     $s =~ s/[^0-9A-Za-z,.:]/sprintf('%%%02X', ord($&))/seg;
89     return $s;
90 }
91
92
93 1;
94
95 __END__
96
97 =head1 NAME
98
99 tagcloud - blosxom plugin to set a $tagcloud::cloud template variable
100 given an arbitrary set of tags and counts
101
102
103 =head1 DESCRIPTION
104
105 tagcloud is a blosxom plugin which sets a $tagcloud::cloud template
106 variable given an arbitrary set of tags and counts. It was developed
107 for use with the L<tags> plugin.
108
109
110 =head1 USAGE
111
112 tagcloud should be loaded after the plugin that is generating its tag
113 set.
114
115
116 =head1 SEE ALSO
117
118 L<tags>
119
120 Blosxom: http://blosxom.sourceforge.net/
121
122
123 =head1 ACKNOWLEDGEMENTS
124
125 Portions of this plugin were swiped verbatim from xtaran's excellent 
126 L<tagging> plugin.
127
128
129 =head1 AUTHOR
130
131 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
132
133
134 =head1 LICENSE
135
136 Copyright 2007, Gavin Carr.
137
138 This plugin is licensed under the same terms as blosxom itself i.e.
139
140 Permission is hereby granted, free of charge, to any person obtaining a
141 copy of this software and associated documentation files (the "Software"),
142 to deal in the Software without restriction, including without limitation
143 the rights to use, copy, modify, merge, publish, distribute, sublicense,
144 and/or sell copies of the Software, and to permit persons to whom the
145 Software is furnished to do so, subject to the following conditions:
146
147 The above copyright notice and this permission notice shall be included
148 in all copies or substantial portions of the Software.
149
150 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
152 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
153 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
154 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
155 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
156 OTHER DEALINGS IN THE SOFTWARE.
157
158 =cut
159
160 # vim:ft=perl:sw=4
161