File Coverage

blib/lib/Spreadsheet/HTML/Presets/List.pm
Criterion Covered Total %
statement 6 67 8.9
branch 0 54 0.0
condition 0 12 0.0
subroutine 2 4 50.0
pod 2 2 100.0
total 10 139 7.1


nor C is specified, is specified, nor C is specified, is specified, will be or C will be used as the or C (respectively) will
line stmt bran cond sub pod time code
1             package Spreadsheet::HTML::Presets::List;
2 5     5   15 use strict;
  5         5  
  5         119  
3 5     5   19 use warnings FATAL => 'all';
  5         4  
  5         3719  
4              
5             sub list {
6 0     0 1   my ($self,$data,$args);
7 0 0         $self = shift if ref($_[0]) =~ /^Spreadsheet::HTML/;
8 0 0         ($self,$data,$args) = $self ? $self->_args( @_ ) : Spreadsheet::HTML::_args( @_ );
9              
10 0           my $list = [];
11 0 0         if (exists $args->{row}) {
12 0 0         $args->{row} = 0 unless $args->{row} =~ /^\d+$/;
13 0           $list = @$data[$args->{row}];
14             } else {
15 0 0 0       $args->{col} = 0 unless $args->{col} && $args->{col} =~ /^\d+$/;
16 0           $list = [ map { $data->[$_][$args->{col}] } 0 .. $#$data ];
  0            
17             }
18              
19 0 0         shift @$list if $args->{headless};
20              
21 0 0         $HTML::AutoTag::ENCODE = defined $args->{encode} ? $args->{encode} : exists $args->{encodes};
22 0 0         $HTML::AutoTag::ENCODES = defined $args->{encodes} ? $args->{encodes} : '';
23             return $args->{_auto}->tag(
24             tag => $args->{ordered} ? 'ol' : 'ul',
25             attr => $args->{ol} || $args->{ul},
26             cdata => [
27             map {
28 0 0 0       my ( $cdata, $attr ) = Spreadsheet::HTML::_extrapolate( $_, undef, $args->{li} );
  0            
29 0           { tag => 'li', attr => $attr, cdata => $cdata }
30             } @$list
31             ]
32             );
33             }
34              
35             sub select {
36 0     0 1   my ($self,$data,$args);
37 0 0         $self = shift if ref($_[0]) =~ /^Spreadsheet::HTML/;
38 0 0         ($self,$data,$args) = $self ? $self->_args( @_ ) : Spreadsheet::HTML::_args( @_ );
39              
40 0           my $cdata = [];
41 0           my $values = [];
42 0 0         if (exists $args->{row}) {
43 0 0         $args->{row} = 0 unless $args->{row} =~ /^\d+$/;
44 0           $cdata = @$data[$args->{row}];
45 0           $values = @$data[$args->{row} + 1];
46             } else {
47 0 0 0       $args->{col} = 0 unless $args->{col} && $args->{col} =~ /^\d+$/;
48 0           $cdata = [ map { $data->[$_][$args->{col}] } 0 .. $#$data ];
  0            
49 0           $values = [ map { $data->[$_][$args->{col} + 1 ] } 0 .. $#$data ];
  0            
50             }
51              
52 0           my $selected = [];
53 0 0         if ($args->{selected}) {
54 0 0         $args->{selected} = [ $args->{selected} ] unless ref $args->{selected};
55 0           for my $text (@$cdata) {
56 0 0         if (grep $_ eq $text, @{ $args->{selected} }) {
  0            
57 0           push @$selected, 'selected';
58             } else {
59 0           push @$selected, undef;
60             }
61             }
62             }
63              
64 0           my $attr = { value => [] };
65 0 0         $attr->{value} = $cdata if $args->{values};
66 0 0         $attr->{selected} = $selected if map defined $_ ? $_ : (), @$selected;
    0          
67              
68             my $options = [
69             map {
70 0           my ( $cdata, $opt_attr ) = Spreadsheet::HTML::_extrapolate( $_, $attr, $args->{option} );
71 0           { tag => 'option', attr => $opt_attr, cdata => $cdata };
72 0 0         } $args->{values} ? @$values : @$cdata
73             ];
74              
75 0 0 0       if (ref( $args->{optgroup} ) eq 'ARRAY' and @{ $args->{optgroup} }) {
  0            
76 0           my @groups = @{ $args->{optgroup} };
  0            
77 0           my @ranges = Spreadsheet::HTML::_range( 0, $#$options, $#groups );
78 0           splice( @$options, $_, 0, { tag => 'optgroup', attr => { label => pop @groups } } ) for reverse @ranges;
79             }
80              
81 0 0         if ($args->{headless}) {
82 0           shift @$options;
83 0           shift @{ $attr->{value} };
  0            
84             }
85              
86 0 0         $HTML::AutoTag::ENCODE = defined $args->{encode} ? $args->{encode} : exists $args->{encodes};
87 0 0         $HTML::AutoTag::ENCODES = defined $args->{encodes} ? $args->{encodes} : '';
88              
89 0           my $label = '';
90 0 0         if ($args->{label}) {
91 0           $label = $args->{_auto}->tag( %{ Spreadsheet::HTML::_tag( %$args, tag => 'label' ) } );
  0            
92             }
93              
94             return $label . $args->{_auto}->tag(
95             tag => 'select',
96             attr => $args->{select},
97             cdata => [
98             ( $args->{placeholder}
99             ? { tag => 'option', attr => { value => '' }, cdata => $args->{placeholder} }
100             : ()
101 0 0         ), @$options
102             ],
103             );
104             }
105              
106             =head1 NAME
107              
108             Spreadsheet::HTML::Presets::List - Generate
109              
110             =head1 DESCRIPTION
111              
112             This is a container for L preset methods.
113             These methods are not meant to be called from this package.
114             Instead, use the Spreadsheet::HTML interface:
115              
116             use Spreadsheet::HTML;
117             my $generator = Spreadsheet::HTML->new( data => \@data );
118             print $generator->list( ordered => 1 );
119             print $generator->select( values => 1, placeholder => 'Pick one' );
120              
121             # or
122             use Spreadsheet::HTML qw( list );
123             print list( data => \@data, col => 2 );
124             print Spreadsheet::HTML::select( data => \@data, row => 0 );
125              
126             Note that C is not exportable, due to the existance of Perl's
127             built-in C function.
128              
129             =head1 METHODS
130              
131             =over 4
132              
133             =item * C
134              
135             Renders ordered
    and unordered
      lists.
136              
137             =back
138              
139             =head2 LITERAL PARAMETERS
140              
141             =over 8
142              
143             =item * C
144              
145             Boolean. Discard first element. Useful for datasets that include headings.
146              
147             headless => 1
148              
149             =item * C
150              
151             Boolean. Uses
    instead of
      container when true.
152              
153             ordered => 1
154              
155             =item * C
156              
157             Integer. Start at this column. If neither C
158             then the first column (0) is used.
159              
160             col => 2
161              
162             =item * C
163              
164             Integer. Start at this row. If neither C nor C
165             then the first column (0) is used (not the first row).
166              
167             row => 0
168              
169             =back
170              
171             =head2 TAG PARAMETERS
172              
173             =over 8
174              
175             =item * C
176              
177             Hash reference of attributes.
178              
179             ol => { class => 'list' }
180              
181             =item * C
182              
183             Hash reference of attributes.
184              
185             ul => { class => 'list' }
186              
187             =item * C
  • 188              
    189             Accepts hash reference, sub reference, or array ref containing either or both.
    190              
    191             li => { class => [qw( odd even )] }
    192             li => sub { ucfirst shift }
    193             li => [ { class => 'item' }, sub { sprintf '%2d', shift } ]
    194              
    195             =back
    196              
    197             =over 4
    198              
    199             =item * C
    200              
    201             Renders
    202              
    203             =back
    204              
    205             =head2 LITERAL PARAMETERS
    206              
    207             =over 8
    208              
    209             =item * C
    210              
    211             Discard first element. Useful for datasets that include headings.
    212              
    213             headless => 1
    214              
    215             =item * C
    216              
    217             Integer. Start at this column. If neither C
    218             then the first column (0) is used.
    219              
    220             col => 2
    221              
    222             =item * C
    223              
    224             Integer. Start at this row. If neither C nor C
    225             then the first column (0) is used (not the first row).
    226              
    227             row => 0
    228              
    229             =item * C
    230              
    231             Optional boolean. Default false. The selected C or C
    232             used as the
    233              
    234             values => 0
    235              
    236             When set to true the selected C
    237             tags' 'value' attribute and the NEXT C
    238             be used as the
    239              
    240             values => 1
    241              
    242             =item * C
    243              
    244             Optional scalar or array ref of default
    245             or
    246              
    247             selected => 'id1'
    248             selected => [qw( id1 id4 )]
    249              
    250             =item * C
    251              
    252             Optional string. Inserts the C as the first
    253             This
    254             value of C.
    255              
    256             placeholder => 'Please select an option'
    257              
    258             =item * C
    259              
    260             Emits
    261             only key is the CDATA for the
    262              
    263             label => 'Label with no attributes'
    264             label => { 'Label with attributes' => { class => 'label' } }
    265              
    266             =item * C
    267              
    268             Optional array ref of scalars.
    269              
    270             optgroup => [ 'Group 1:', 'Group 2:', 'Group 3:' ]
    271              
    272             =back
    273              
    274             =head2 TAG PARAMETERS
    275              
    276             =over 8
    277              
    278             =item * C
    279              
    280             Hash reference of attributes.
    281              
    282             select => { class => 'select' }
    283              
    284             =item * C
    285              
    286             Accepts hash reference, sub reference, or array ref containing either or both.
    287              
    288             option => { class => [qw( odd even )] }
    289             option => sub { uc shift }
    290             option => [ sub { uc shift }, { class => [qw( odd even )] } ]
    291              
    292             =back
    293              
    294             =head1 SEE ALSO
    295              
    296             =over 4
    297              
    298             =item L
    299              
    300             The interface for this functionality.
    301              
    302             =item L
    303              
    304             More presets.
    305              
    306             =item L
    307              
    308             =back
    309              
    310             =head1 AUTHOR
    311              
    312             Jeff Anderson, C<< >>
    313              
    314             =head1 LICENSE AND COPYRIGHT
    315              
    316             Copyright 2016 Jeff Anderson.
    317              
    318             This program is free software; you can redistribute it and/or modify it
    319             under the terms of the the Artistic License (2.0). You may obtain a
    320             copy of the full license at:
    321              
    322             L
    323              
    324             Any use, modification, and distribution of the Standard or Modified
    325             Versions is governed by this Artistic License. By using, modifying or
    326             distributing the Package, you accept this license. Do not use, modify,
    327             or distribute the Package, if you do not accept this license.
    328              
    329             If your Modified Version has been derived from a Modified Version made
    330             by someone other than you, you are nevertheless required to ensure that
    331             your Modified Version complies with the requirements of this license.
    332              
    333             This license does not grant you the right to use any trademark, service
    334             mark, tradename, or logo of the Copyright Holder.
    335              
    336             This license includes the non-exclusive, worldwide, free-of-charge
    337             patent license to make, have made, use, offer to sell, sell, import and
    338             otherwise transfer the Package with respect to any patent claims
    339             licensable by the Copyright Holder that are necessarily infringed by the
    340             Package. If you institute patent litigation (including a cross-claim or
    341             counterclaim) against any party alleging that the Package constitutes
    342             direct or contributory patent infringement, then this Artistic License
    343             to you shall terminate on the date that such litigation is filed.
    344              
    345             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
    346             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
    347             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    348             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
    349             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
    350             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
    351             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
    352             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    353              
    354             =cut
    355              
    356             1;