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