blib/lib/HTML/TagCloud/Simple.pm

filter: Statementstmt 94.2% 65 / 69 Branchbran 85.0% 17 / 20 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 86.7% 92 / 106 CC 17 SCAR 27.1
CC
17
Cov
90%
build_cloud
17.5
BEGIN
1.0
BEGIN
1.0
CRAP 15.1
↑ Summary
1 package HTML::TagCloud::Simple;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use 5.010; # sorry, need smart matching
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8 1 our @ISA = qw(Exporter);
9 1 our @EXPORT = qw();
10 1 our @EXPORT_OK = qw(build_cloud);
11  
12 1 our $VERSION = '0.03';
13  
14 ## TODO
15 # need to add the reverse sorting mechanisms
16 # come up with good way to test this output
17  
18 1 $HTML::Tag::Font_Family = 'monospace';
19 1 @HTML::Tag::Font_Sizes = ('70%', '85%', '100%', '115%', '130%'); # hmm..
20  
21 ## generalized routines for generating HTML tag clouds
22  
23 4 ▶sub build_cloud {
Subroutine build_cloud: called CC=17 SCAR=28.6
Pod undocumented
24 # build_cloud(\%hash, $base_url, $height, $width, [$sort_method], [$min_count]) -- returns an HTML string for a tag 'cloud'
25 4 my $href = shift;
26 4 my %tags = %{$href};
27 4 my $base_url = shift;
28 4 ▶ my $height = shift // 50;
Condition50%1/2
shift() // 50
l!l
40
MC/DC0%0/1
shift() // 50
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29 4 ▶ my $width = shift // 50;
Condition50%1/2
shift() // 50
l!l
40
MC/DC0%0/1
shift() // 50
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
30 4 ▶ my $sort_method = shift // 'ascii'; # allows sorting by 'ascii' or 'value' (can also prefix either with 'reverse-')
Condition50%1/2
shift() // "ascii"
l!l
40
MC/DC0%0/1
shift() // "ascii"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
31 4 ▶ my $min_count = shift // 0;
Condition100%2/2
shift() // 0
l!l
13
MC/DC100%1/1
shift() // 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
32 4 my $html;
33  
34 ## need to cull entries with less than $min_count
35 4 my @keepers = grep { $tags{$_}{count} > $min_count; } keys %tags;
36 4 foreach (keys %tags) {
37 112 ▶ delete $tags{$_} unless @keepers ~~ /$_/;
Branch: 1 branch
BranchTrueFalseTotal
unless \@keepers ~~ qr/$_/ 2 110 112
38 }
39  
40 4 my @ordered = sort { $tags{$a}{count} <=> $tags{$b}{count} } keys %tags;
41 4 my $high = $tags{$ordered[-1]}{count};
42 4 my $range = ($high / $#HTML::Tag::Font_Sizes);
43
44 4 foreach my $tag (keys %tags) {
45 110 my $count = $tags{$tag}{count};
46  
47 110 ▶ my $size = ($count < ($range)) ? $HTML::Tag::Font_Sizes[0] :
Branch: 4 branches
BranchTrueFalseTotal
$count < $range ? : 27 83 110
$count < $range * 2 ? : 28 55 83
$count < $range * 3 ? : 26 29 55
$count < $range * 4 ? : 25 4 29
48 ($count < ($range * 2)) ? $HTML::Tag::Font_Sizes[1] :
49 ($count < ($range * 3)) ? $HTML::Tag::Font_Sizes[2] :
50 ($count < ($range * 4)) ? $HTML::Tag::Font_Sizes[3] :
51 $HTML::Tag::Font_Sizes[-1]; # default
52 110 $tags{$tag}{size} = $size;
53  
54 }
55  
56 ## build the initial HTML
57 4 my $id = 'tag_cloud_1';
58 4 $html = "<div id=\"$id\" style=\"display: inline; border-style: solid; border-width: 1px; border-color: #000000; position: absolute; height: $height; width: $width;\">";
59  
60 ## iterate keys according to sort
61 4 my @keys;
62 4 ▶ if ($sort_method =~ /value/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_method =~ /value/i) { } 0 4 4
63 0 @keys = sort { $tags{$a}{count} <=> $tags{$b}{count} } keys %tags;
64 } else {
65 4 @keys = sort { $a cmp $b } keys %tags;
66 }
67  
68 4 ▶ for (my $i = 0; $i <= $#keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#keys) 110 4 114
69 110 my $lhtml;
70 110 my $key = $keys[$i];
71 110 my %tag = %{$tags{$key}}; # need to have some error checking here
72 110 my $size = $tag{size};
73 110 my $count = $tag{count};
74
75 110 my $link;
76 110 ▶ if (defined $tag{link_rel}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $tag{'link_rel'}) { } 107 3 110
elsif (defined $tag{'link_abs'}) { } 3 0 3
77 107 my $link = $tag{link_rel};
78 107 my $alt = $count;
79
80 107 ▶ warn "WARN:: '<base>' specified in '$key', but no 'base' spec in build_cloud() call" unless $base_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url 0 107 107
81 107 $link =~ s/<base>/$base_url/;
82 107 $link =~ s/<value>/$count/g;
83 107 $link =~ s/<key>/$key/g;
84
85
86 107 $lhtml = "<a href=\"$link\" title=\"$count\"><span style=\"font-face: $HTML::Tag::Font_Family; font-size: $size;\">$key</span></a>";
87
88 } elsif (defined $tag{link_abs}) {
89 3 my $link = $tag{link_abs};
90 3 my $alt = $count;
91
92 3 $lhtml = "<a href=$link title=$alt><span style='font-face: $HTML::Tag::Font_Family; font-size: $size'>$key</span></a>";
93
94 } else {
95 0 warn "WARN:: no 'link_abs' or 'link_rel' specified for '$key'";
96 0 next;
97 }
98
99 110 $html .= " " . $lhtml;
100  
101 }
102  
103 ## build the closing HTML
104 4 $html .= "</div>";
105  
106 4 return $html;
107 }
108  
109  
110 1 1;
111  
112 __END__
↑ Summary