| 1 | package Tags::HTML::Stars; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 4 | ▶ | use base qw(Tags::HTML); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 4 | ▶ | use Class::Utils qw(set_params split_params); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 4 | ▶ | use Error::Pure qw(err); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 4 | ▶ | use List::Util qw(max); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 4 | ▶ | use MIME::Base64; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 4 | ▶ | use Readonly; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | # Constants. | ||||||||||||||||||
| 14 | 4 | Readonly::Scalar our $STAR_FULL_FILENAME => 'Star*.svg'; | |||||||||||||||||
| 15 | 4 | Readonly::Scalar our $STAR_HALF_FILENAME => 'Star-.svg'; | |||||||||||||||||
| 16 | 4 | Readonly::Scalar our $STAR_NOTHING_FILENAME => 'Star½.svg'; | |||||||||||||||||
| 17 | 4 | Readonly::Scalar our $IMG_STAR_FULL => encode_base64(<<'END', ''); | |||||||||||||||||
| 18 | <svg width="300px" height="275px" viewBox="0 0 300 275" xmlns="http://www.w3.org/2000/svg" version="1.1"> | ||||||||||||||||||
| 19 | <polygon fill="#fdff00" stroke="#605a00" stroke-width="15" points="150,25 179,111 269,111 197,165 223,251 150,200 77,251 103,165 31,111 121,111" /> | ||||||||||||||||||
| 20 | </svg> | ||||||||||||||||||
| 21 | END | ||||||||||||||||||
| 22 | 4 | Readonly::Scalar our $IMG_STAR_HALF => encode_base64(<<'END', ''); | |||||||||||||||||
| 23 | <svg width="300px" height="275px" viewBox="0 0 300 275" xmlns="http://www.w3.org/2000/svg" version="1.1"> | ||||||||||||||||||
| 24 | <clipPath id="empty"><rect x="150" y="0" width="150" height="275" /></clipPath> | ||||||||||||||||||
| 25 | <clipPath id="filled"><rect x="0" y="0" width="150" height="275" /></clipPath> | ||||||||||||||||||
| 26 | <polygon fill="none" stroke="#808080" stroke-width="15" stroke-opacity="0.37647060" points="150,25 179,111 269,111 197,165 223,251 150,200 77,251 103,165 31,111 121,111" clip-path="url(#empty)" /> | ||||||||||||||||||
| 27 | <polygon fill="#fdff00" stroke="#605a00" stroke-width="15" points="150,25 179,111 269,111 197,165 223,251 150,200 77,251 103,165 31,111 121,111" clip-path="url(#filled)" /> | ||||||||||||||||||
| 28 | </svg> | ||||||||||||||||||
| 29 | END | ||||||||||||||||||
| 30 | 4 | Readonly::Scalar our $IMG_STAR_NOTHING => encode_base64(<<'END', ''); | |||||||||||||||||
| 31 | <svg width="300px" height="275px" viewBox="0 0 300 275" xmlns="http://www.w3.org/2000/svg" version="1.1"> | ||||||||||||||||||
| 32 | <polygon fill="none" stroke="#808080" stroke-width="15" stroke-opacity="0.37647060" points="150,25 179,111 269,111 197,165 223,251 150,200 77,251 103,165 31,111 121,111" /> | ||||||||||||||||||
| 33 | </svg> | ||||||||||||||||||
| 34 | END | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 4 | our $VERSION = 0.03; | |||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | # Constructor. | ||||||||||||||||||
| 39 | sub new { | ||||||||||||||||||
| 40 | 5 | ▶ | my ($class, @params) = @_; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | # No CSS support. | ||||||||||||||||||
| 43 | 5 | push @params, 'no_css', 1; | |||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | # Create object. | ||||||||||||||||||
| 46 | 5 | my ($object_params_ar, $other_params_ar) = split_params( | |||||||||||||||||
| 47 | ['public_image_dir', 'star_width'], @params); | ||||||||||||||||||
| 48 | 5 | my $self = $class->SUPER::new(@{$other_params_ar}); | |||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | # Public image directory. | ||||||||||||||||||
| 51 | 4 | $self->{'public_image_dir'} = undef; | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | # Star width (300px). | ||||||||||||||||||
| 54 | 4 | $self->{'star_width'} = undef; | |||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | # Process params. | ||||||||||||||||||
| 57 | 4 | set_params($self, @{$object_params_ar}); | |||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | # Object. | ||||||||||||||||||
| 60 | 4 | return $self; | |||||||||||||||||
| 61 | } | ||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | # Process 'Tags'. | ||||||||||||||||||
| 64 | sub _process { | ||||||||||||||||||
| 65 | 2 | ▶ | my ($self, $stars_hr) = @_; | ||||||||||||||||
|
Subroutine
_process: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | # Main stars. | ||||||||||||||||||
| 68 | 2 | $self->{'tags'}->put( | |||||||||||||||||
| 69 | ['b', 'div'], | ||||||||||||||||||
| 70 | ); | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 2 | my $max_num = max(keys %{$stars_hr}); | |||||||||||||||||
| 73 | 2 | foreach my $num (1 .. $max_num) { | |||||||||||||||||
| 74 | 6 | my $image_src = $self->_star_src($stars_hr->{$num}); | |||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | $self->{'tags'}->put( | ||||||||||||||||||
| 77 | ['b', 'img'], | ||||||||||||||||||
| 78 | ( | ||||||||||||||||||
| 79 | $self->{'star_width'} | ||||||||||||||||||
| 80 | ? ( | ||||||||||||||||||
| 81 | 6 | ▶ | ['a', 'style', 'width: '.$self->{'star_width'}.';'], | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 82 | ) : (), | ||||||||||||||||||
| 83 | ), | ||||||||||||||||||
| 84 | ['a', 'src', $image_src], | ||||||||||||||||||
| 85 | ['e', 'img'], | ||||||||||||||||||
| 86 | ); | ||||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 2 | $self->{'tags'}->put( | |||||||||||||||||
| 90 | ['e', 'div'], | ||||||||||||||||||
| 91 | ); | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 2 | return; | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | sub _star_src { | ||||||||||||||||||
| 97 | 6 | ▶ | my ($self, $id) = @_; | ||||||||||||||||
|
Subroutine
_star_src: called CC=9 SCAR=30.5
| |||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | # Link to file. | ||||||||||||||||||
| 100 | 6 | my $src; | |||||||||||||||||
| 101 | 6 | ▶ | if (defined $self->{'public_image_dir'}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | 0 | $src = $self->{'public_image_dir'}; | |||||||||||||||||
| 103 | 0 | ▶ | if ($src ne '') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | 0 | $src .= '/'; | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | 0 | ▶ | if ($id eq 'full') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 107 | 0 | $src .= $STAR_FULL_FILENAME; | |||||||||||||||||
| 108 | } elsif ($id eq 'half') { | ||||||||||||||||||
| 109 | 0 | $src .= $STAR_HALF_FILENAME; | |||||||||||||||||
| 110 | } elsif ($id eq 'nothing') { | ||||||||||||||||||
| 111 | 0 | $src .= $STAR_NOTHING_FILENAME | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | } else { | ||||||||||||||||||
| 114 | 6 | $src = 'data:image/svg+xml;base64,'; | |||||||||||||||||
| 115 | 6 | ▶ | if ($id eq 'full') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 116 | 2 | $src .= $IMG_STAR_FULL; | |||||||||||||||||
| 117 | } elsif ($id eq 'half') { | ||||||||||||||||||
| 118 | 2 | $src .= $IMG_STAR_HALF; | |||||||||||||||||
| 119 | } elsif ($id eq 'nothing') { | ||||||||||||||||||
| 120 | 2 | $src .= $IMG_STAR_NOTHING; | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | } | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | 6 | return $src; | |||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | 4 | 1; | |||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||