blib/lib/HTML/TagCloud.pm

filter: Statementstmt 82.1% 106 / 129 Branchbran 73.5% 25 / 34 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 84.2% 16 / 19 Podpod 100% 9 / 9 totaltotal 77.6% 160 / 206 CC 26 SCAR 17.3
CC
26
Cov
78%
_tags_by_category
20.0
html_with_categories
12.0
tags
7.0
CRAP 5.6
↑ Summary
1 package HTML::TagCloud;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = '0.38';
5  
6 2 ▶use constant EMPTY_STRING => q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
9 9 my $class = shift;
10 9 my $self = {
11 counts => {},
12 urls => {},
13 category_for => {},
14 categories => [],
15 levels => 24,
16 distinguish_adjacent_tags => 0,
17 @_
18 };
19 9 bless $self, $class;
20 9 return $self;
21 }
22  
23 359 ▶sub add {
Subroutine add: called CC=3 SCAR=12.3
Pod documented
24 359 my ( $self, $tag, $url, $count, $category ) = @_;
25 359 $self->{counts}->{$tag} = $count;
26 359 $self->{urls}->{$tag} = $url;
27 359 ▶ if ( scalar @{ $self->{categories} } > 0 && defined $category ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"categories"};} > 0 and defined $category) 0 359 359
Condition33%1/3
scalar @{$self->{"categories"};} > 0 and defined $category
!ll&&!rl&&r
35900
MC/DC0%0/2
scalar @{$self->{"categories"};} > 0 and defined $category
scalar @{$self->{"categories"};} > 0 defined $category
Truth table33%1/3
A and B
A= scalar @{$self->{"categories"};} > 0B= defined $category
ABresult
0X0
100
111
28 0 $self->{category_for}->{$tag} = $category;
29 }
30 }
31  
32 10 ▶sub add_static {
Subroutine add_static: called CC=3 SCAR=12.7
Pod documented
33 10 my ( $self, $tag, $count, $category ) = @_;
34 10 $self->{counts}->{$tag} = $count;
35  
36 10 ▶ if ( scalar @{ $self->{categories} } > 0 && defined $category ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"categories"};} > 0 and defined $category) 0 10 10
Condition33%1/3
scalar @{$self->{"categories"};} > 0 and defined $category
!ll&&!rl&&r
1000
MC/DC0%0/2
scalar @{$self->{"categories"};} > 0 and defined $category
scalar @{$self->{"categories"};} > 0 defined $category
Truth table33%1/3
A and B
A= scalar @{$self->{"categories"};} > 0B= defined $category
ABresult
0X0
100
111
37 0 $self->{category_for}->{$tag} = $category;
38 }
39 }
40  
41 3 ▶sub css {
Subroutine css: called CC=3 SCAR=11.0
Pod documented
42 3 my ($self) = @_;
43 3 my $css = q(
44 #htmltagcloud {
45 text-align: center;
46 line-height: 1;
47 }
48 );
49 3 foreach my $level ( 0 .. $self->{levels} ) {
50 75 ▶ if ( $self->{distinguish_adjacent_tags} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'distinguish_adjacent_tags'}) { } 25 50 75
51 25 $css .= $self->_css_for_tag( $level, 'even' );
52 25 $css .= $self->_css_for_tag( $level, 'odd' );
53 }
54 else {
55 50 $css .= $self->_css_for_tag( $level, q{} );
56 }
57 }
58 3 return $css;
59 }
60  
61 100 ▶sub _css_for_tag {
Subroutine _css_for_tag: called CC=1 SCAR=0.0
62 100 my ( $self, $level, $subclass ) = @_;
63 100 my $font = 12 + $level;
64 100 return <<"END_OF_TAG";
65 span.tagcloud${level}${subclass} {font-size: ${font}px;}
66 span.tagcloud${level}${subclass} a {text-decoration: none;}
67 END_OF_TAG
68 }
69  
70 15 ▶sub tags {
Subroutine tags: called CC=7 SCAR=19.5
Pod documented
71 15 my ( $self, $limit ) = @_;
72 15 my $counts = $self->{counts};
73 15 my $urls = $self->{urls};
74 15 my $category_for = $self->{category_for};
75 15 ▶ my @tags = sort { $counts->{$b} <=> $counts->{$a} || $a cmp $b } keys %$counts;
Condition66%2/3
$counts->{$b} <=> $counts->{$a} || $a cmp $b
l!l&&r!l&&!r
983578980
MC/DC0%0/2
$counts->{$b} <=> $counts->{$a} || $a cmp $b
$counts->{$b} <=> $counts->{$a} $a cmp $b
Truth table66%2/3
A || B
A= $counts->{$b} <=> $counts->{$a}B= $a cmp $b
ABresult
1X1
011
000
76 15 ▶ @tags = splice( @tags, 0, $limit ) if defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit 6 9 15
77  
78 15 ▶ return unless scalar @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @tags 1 14 15
79  
80 14 my $min = log( $counts->{ $tags[-1] } );
81 14 my $max = log( $counts->{ $tags[0] } );
82 14 my $factor;
83  
84 # special case all tags having the same count
85 14 ▶ if ( $max - $min == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max - $min == 0) { } 9 5 14
86 9 $min = $min - $self->{levels};
87 9 $factor = 1;
88 }
89 else {
90 5 $factor = $self->{levels} / ( $max - $min );
91 }
92  
93 14 ▶ if ( scalar @tags < $self->{levels} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @tags < $self->{"levels"}) 13 1 14
94 13 $factor *= ( scalar @tags / $self->{levels} );
95 }
96 14 my @tag_items;
97 14 foreach my $tag ( sort @tags ) {
98 402 my $tag_item;
99 402 $tag_item->{name} = $tag;
100 402 $tag_item->{count} = $counts->{$tag};
101 402 $tag_item->{url} = $urls->{$tag};
102 $tag_item->{level}
103 402 = int( ( log( $tag_item->{count} ) - $min ) * $factor );
104 402 $tag_item->{category} = $category_for->{$tag};
105 402 push @tag_items, $tag_item;
106 }
107 14 return @tag_items;
108 }
109  
110 15 ▶sub html {
Subroutine html: called CC=2 SCAR=7.0
Pod documented
111 15 my ( $self, $limit ) = @_;
112 my $html
113 15 ▶ = scalar @{ $self->{categories} } > 0
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$self->{'categories'};} > 0 ? : 0 15 15
114 ? $self->html_with_categories($limit)
115 : $self->html_without_categories($limit);
116 15 return $html;
117 }
118  
119 15 ▶sub html_without_categories {
Subroutine html_without_categories: called CC=1 SCAR=0.0
Pod documented
120 15 my ( $self, $limit ) = @_;
121 15 my $html = $self->_html_for( [ $self->tags($limit) ] );
122 }
123  
124 15 ▶sub _html_for {
Subroutine _html_for: called CC=3 SCAR=11.0
125 15 my ( $self, $tags_ref ) = @_;
126 15 my $ntags = scalar( @{$tags_ref} );
127 15 ▶ return EMPTY_STRING if $ntags == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ntags == 0 1 14 15
128  
129 # Format the HTML division.
130 14 ▶ my $html
Branch: 1 branch
BranchTrueFalseTotal
$ntags == 1 ? : 3 11 14
131 = $ntags == 1
132 ? $self->_html_for_single_tag($tags_ref)
133 : $self->_html_for_multiple_tags($tags_ref);
134  
135 14 return $html;
136 }
137  
138 3 ▶sub _html_for_single_tag {
Subroutine _html_for_single_tag: called CC=1 SCAR=0.0
139 3 my ( $self, $tags_ref ) = @_;
140  
141 # Format the contents of the div.
142 3 my $tag_ref = $tags_ref->[0];
143 3 my $html = $self->_format_span( @{$tag_ref}{qw(name url)}, 1, 1 );
144  
145 3 return qq{<div id="htmltagcloud">$html</div>\n};
146 }
147  
148 11 ▶sub _html_for_multiple_tags {
Subroutine _html_for_multiple_tags: called CC=2 SCAR=6.9
149 11 my ( $self, $tags_ref ) = @_;
150  
151 # Format the contents of the div.
152 11 my $html = EMPTY_STRING;
153 11 my $is_even = 1;
154 11 foreach my $tag ( @{$tags_ref} ) {
155 my $span
156 399 = $self->_format_span( @{$tag}{qw(name url level)}, $is_even );
157 399 $html .= "$span\n";
158 399 $is_even = !$is_even;
159 }
160 11 $html = qq{<div id="htmltagcloud">
161 $html</div>};
162 11 return $html;
163 }
164  
165 0 ▶sub html_with_categories {
Subroutine html_with_categories: not called CC=3 SCAR=24.8
Pod documented
166 0 my ( $self, $limit ) = @_;
167  
168 # Get the collection of tags, organized by category.
169 0 my $tags_by_category_ref = $self->_tags_by_category($limit);
170 0 ▶ return EMPTY_STRING if !defined $tags_by_category_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tags_by_category_ref 0 0 0
171  
172 # Format the HTML document.
173 0 my $html = EMPTY_STRING;
174 CATEGORY:
175 0 for my $category ( @{ $self->{categories} } ) {
176 0 my $tags_ref = $tags_by_category_ref->{$category};
177 0 $html .= $self->_html_for_category( $category, $tags_ref );
178 }
179  
180 0 return $html;
181 }
182  
183 0 ▶sub _html_for_category {
Subroutine _html_for_category: not called CC=1 SCAR=6.9
184 0 my ( $self, $category, $tags_ref ) = @_;
185  
186 # Format the HTML.
187 0 my $html
188 = qq{<div class='$category'>}
189 . $self->_html_for($tags_ref)
190 . qq{</div>};
191  
192 0 return $html;
193 }
194  
195 0 ▶sub _tags_by_category {
Subroutine _tags_by_category: not called CC=4 SCAR=30.0
196 0 my ( $self, $limit ) = @_;
197  
198 # Get the tags.
199 0 my @tags = $self->tags($limit);
200 0 ▶ return if scalar @tags == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tags == 0 0 0 0
201  
202 # Build the categorized collection of tags.
203 0 my %tags_by_category;
204 0 for my $tag_ref (@tags) {
205 my $category
206 = defined $tag_ref->{category}
207 ? $tag_ref->{category}
208 0 ▶ : '__unknown__';
Branch: 1 branch
BranchTrueFalseTotal
defined $tag_ref->{'category'} ? : 0 0 0
209 0 push @{ $tags_by_category{$category} }, $tag_ref;
210 }
211  
212 0 return \%tags_by_category;
213 }
214  
215 1 ▶sub html_and_css {
Subroutine html_and_css: called CC=1 SCAR=0.0
Pod documented
216 1 my ( $self, $limit ) = @_;
217 1 my $html = qq{<style type="text/css">\n} . $self->css . "</style>";
218 1 $html .= $self->html($limit);
219 1 return $html;
220 }
221  
222 402 ▶sub _format_span {
Subroutine _format_span: called CC=5 SCAR=16.1
223 402 my ( $self, $name, $url, $level, $is_even ) = @_;
224 402 my $subclass = q{};
225 402 ▶ if ( $self->{distinguish_adjacent_tags} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"distinguish_adjacent_tags"}) 10 392 402
226 10 ▶ $subclass = $is_even ? 'even' : 'odd';
Branch: 1 branch
BranchTrueFalseTotal
$is_even ? : 7 3 10
227 }
228 402 my $span_class = qq{tagcloud$level$subclass};
229 402 my $span = qq{<span class="$span_class">};
230 402 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 392 10 402
231 392 $span .= qq{<a href="$url">};
232 }
233 402 $span .= $name;
234 402 ▶ if ( defined $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 392 10 402
235 392 $span .= qq{</a>};
236 }
237 402 $span .= qq{</span>};
238 }
239  
240 2 1;
241  
242 __END__
↑ Summary