Revert rss20 to using $ENV{PATH_INFO} in self link.
[blosxom-plugins.git] / gavinc / tagcloud
blobe0c3baaba8c14b69654451fc90e2dff7662f0ac9
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
6 package tagcloud;
8 use strict;
9 use vars qw(%config $cloud);
11 # Uncomment next line to enable debug output (don't uncomment debug() lines)
12 #use Blosxom::Debug debug_level => 1;
14 # --- Configuration defaults -----
16 %config = (
18   # tagcloud requires a hashref containing 'tag => count' pairs
19   tag_hashref           => $tags::tag_counts,
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',
30   # Tags to omit from tagcloud
31   tagcloud_blacklist => [ 'Now Playing' ],
35 # ---------------------------------
36 # __END_CONFIG__
38 my %tagcloud_blacklist = map { $_ => 1 } @{$config{tagcloud_blacklist}};
40 $cloud = '';
42 sub start {
43     eval { $config{tag_hashref} }
44         or warn "[tagcloud] tag_hashref not set - skipping" 
45             and return 0;
46     1;
49 sub story {
50     my $tag_hashref = $config{tag_hashref};
51     return unless keys %$tag_hashref;
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;
65     my $diff = $max - $min;
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     }
82     $cloud = sprintf "%s%s%s", 
83         $config{tagcloud_prefix}, join(', ', @tagcloud), $config{tagcloud_suffix};
86 sub _url_escape {
87     my $s = shift;
88     $s =~ s/[^0-9A-Za-z,.:]/sprintf('%%%02X', ord($&))/seg;
89     return $s;
95 __END__
97 =head1 NAME
99 tagcloud - blosxom plugin to set a $tagcloud::cloud template variable
100 given an arbitrary set of tags and counts
103 =head1 DESCRIPTION
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.
110 =head1 USAGE
112 tagcloud should be loaded after the plugin that is generating its tag
113 set.
116 =head1 SEE ALSO
118 L<tags>
120 Blosxom: http://blosxom.sourceforge.net/
123 =head1 ACKNOWLEDGEMENTS
125 Portions of this plugin were swiped verbatim from xtaran's excellent 
126 L<tagging> plugin.
129 =head1 AUTHOR
131 Gavin Carr <gavin@openfusion.com.au>, http://www.openfusion.net/
134 =head1 LICENSE
136 Copyright 2007, Gavin Carr.
138 This plugin is licensed under the same terms as blosxom itself i.e.
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:
147 The above copyright notice and this permission notice shall be included
148 in all copies or substantial portions of the Software.
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.
158 =cut
160 # vim:ft=perl:sw=4