-
Notifications
You must be signed in to change notification settings - Fork 2
/
HTML.pm
1185 lines (759 loc) · 34.8 KB
/
HTML.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Spreadsheet::HTML;
use strict;
use warnings FATAL => 'all';
our $VERSION = '1.24';
use Exporter 'import';
our @EXPORT_OK = qw(
generate portrait landscape
north east south west handson
layout checkerboard scroll
chess checkers draughts conway sudoku
calculator calendar banner maze
beadwork list
);
use HTML::AutoTag;
use Spreadsheet::HTML::Engine;
use Spreadsheet::HTML::Presets;
use Spreadsheet::HTML::File::Loader;
sub portrait { generate( @_, theta => 0 ) }
sub landscape { generate( @_, theta => -270, tgroups => 0 ) }
sub north { generate( @_, theta => 0 ) }
sub east { generate( @_, theta => 90, tgroups => 0, pinhead => 1 ) }
sub south { generate( @_, theta => -180, tgroups => 0, pinhead => 1 ) }
sub west { generate( @_, theta => -270, tgroups => 0 ) }
sub layout { Spreadsheet::HTML::Presets::layout( @_ ) }
sub list { Spreadsheet::HTML::Presets::List::list( @_ ) }
sub select { Spreadsheet::HTML::Presets::List::select( @_ ) }
sub handson { Spreadsheet::HTML::Presets::Handson::handson( @_ ) }
sub conway { Spreadsheet::HTML::Presets::Conway::conway( @_ ) }
sub calculator { Spreadsheet::HTML::Presets::Calculator::calculator( @_ ) }
sub chess { Spreadsheet::HTML::Presets::Chess::chess( @_ ) }
sub checkers { Spreadsheet::HTML::Presets::Draughts::draughts( @_ ) }
sub draughts { Spreadsheet::HTML::Presets::Draughts::draughts( @_ ) }
sub tictactoe { Spreadsheet::HTML::Presets::TicTacToe::tictactoe( @_ ) }
sub sudoku { Spreadsheet::HTML::Presets::Sudoku::sudoku( @_ ) }
sub checkerboard { Spreadsheet::HTML::Presets::checkerboard( @_ ) }
sub calendar { Spreadsheet::HTML::Presets::calendar( @_ ) }
sub scroll { Spreadsheet::HTML::Presets::Scroll::scroll( @_ ) }
sub maze { Spreadsheet::HTML::Presets::maze( @_ ) }
sub banner { Spreadsheet::HTML::Presets::banner( @_ ) }
sub beadwork { Spreadsheet::HTML::Presets::Beadwork::beadwork( @_ ) }
sub generate {
my %args = _process( @_ );
$args{theta} *= -1 if $args{theta} and $args{flip};
if (!$args{theta}) { # north
$args{data} = $args{flip} ? [ map [ CORE::reverse @$_ ], @{ $args{data} } ] : $args{data};
} elsif ($args{theta} == -90) {
$args{data} = [ CORE::reverse @{ _transpose( $args{data} ) }];
$args{data} = ($args{pinhead} and !$args{headless})
? [ map [ @$_[1 .. $#$_], $_->[0] ], @{ $args{data} } ]
: [ map [ CORE::reverse @$_ ], @{ $args{data} } ];
} elsif ($args{theta} == 90) { # east
$args{data} = _transpose( $args{data} );
$args{data} = ($args{pinhead} and !$args{headless})
? [ map [ @$_[1 .. $#$_], $_->[0] ], @{ $args{data} } ]
: [ map [ CORE::reverse @$_ ], @{ $args{data} } ];
} elsif ($args{theta} == -180) { # south
$args{data} = ($args{pinhead} and !$args{headless})
? [ @{ $args{data} }[1 .. $#{ $args{data} }], $args{data}[0] ]
: [ CORE::reverse @{ $args{data} } ];
} elsif ($args{theta} == 180) {
$args{data} = ($args{pinhead} and !$args{headless})
? [ map [ CORE::reverse @$_ ], @{ $args{data} }[1 .. $#{ $args{data} }], $args{data}[0] ]
: [ map [ CORE::reverse @$_ ], CORE::reverse @{ $args{data} } ];
} elsif ($args{theta} == -270) { # west
$args{data} = [@{ _transpose( $args{data} ) }];
} elsif ($args{theta} == 270) {
$args{data} = [ CORE::reverse @{ _transpose( $args{data} ) }];
}
if ($args{scroll}) {
my ($js, %new_args) = Spreadsheet::HTML::Presets::Scroll::scroll(
%args,
data => [ map [ map $_->{cdata}, @$_ ], @{ $args{data} } ],
);
for (keys %args) {
if (ref $args{$_} eq 'HASH') {
$new_args{$_} = { %{ $new_args{$_} || {} }, %{ $args{$_} || {} } };
}
}
my $table = _make_table( _process( %new_args ) );
return $js . $table;
}
return _make_table( %args );
}
sub new {
my $class = shift;
my %attrs = ref($_[0]) eq 'HASH' ? %{+shift} : @_;
return bless { %attrs }, $class;
}
sub _process {
my ($self,$data,$args) = _args( @_ );
if ($self and $self->{is_cached}) {
return wantarray ? ( data => $self->{data}, %{ $args || {} } ) : $data;
}
# headings is an alias for -r0
$args->{-r0} = $args->{headings} if exists $args->{headings};
# headings to index mapping (alias for some -cX)
my %index = ();
if ($#{ $data->[0] }) {
%index = map { '-' . ($data->[0][$_] || '') => $_ } 0 .. $#{ $data->[0] };
for (grep /^-/, keys %$args) {
$args->{"-c$index{$_}" } = $args->{$_} if exists $index{$_};
}
}
my $empty = exists $args->{empty} ? $args->{empty} : ' ';
my $tag = ($args->{headless} or $args->{matrix}) ? 'td' : 'th';
for my $row (0 .. $args->{_max_rows} - 1) {
unless ($args->{_layout}) {
push @{ $data->[$row] }, undef for 1 .. $args->{_max_cols} - $#{ $data->[$row] } + 1; # pad
pop @{ $data->[$row] } for $args->{_max_cols} .. $#{ $data->[$row] }; # truncate
}
for my $col (0 .. $#{ $data->[$row] }) {
my ( $cdata, $attr ) = ( $data->[$row][$col], undef );
for ($tag, "-c$col", "-r$row", "-r${row}c${col}") {
next unless exists $args->{$_};
( $cdata, $attr ) = _extrapolate( $cdata, $attr, $args->{$_} );
}
do{ no warnings;
$cdata = HTML::Entities::encode_entities( $cdata, $args->{encodes} ) if $args->{encode} || exists $args->{encodes};
$cdata =~ s/^\s*$/$empty/g;
};
$data->[$row][$col] = {
tag => $tag,
(defined( $cdata ) ? (cdata => $cdata) : ()),
(keys( %$attr ) ? (attr => $attr) : ()),
};
}
$tag = 'td';
}
if ($args->{cache} and $self and !$self->{is_cached}) {
$self->{data} = $data;
$self->{is_cached} = 1;
}
shift @$data if $args->{headless};
return wantarray ? ( data => $data, %$args ) : $data;
}
sub _make_table {
my %args = @_;
my @cdata = ( _tag( %args, tag => 'caption' ) || (), _colgroup( %args ) );
if ($args{tgroups}) {
my @body = @{ $args{data} };
my $head = shift @body unless $args{matrix} and scalar @{ $args{data} } > 2;
my $foot = pop @body if !$args{matrix} and $args{tgroups} > 1 and scalar @{ $args{data} } > 2;
my $head_row = { tag => 'tr', attr => $args{'thead.tr'}, cdata => $head };
my $foot_row = { tag => 'tr', attr => $args{'tfoot.tr'}, cdata => $foot };
my $body_rows = [ map { tag => 'tr', attr => $args{tr}, cdata => $_ }, @body ];
if (int($args{group} || 0) > 1) {
$body_rows = [
map [ @$body_rows[$_ .. $_ + $args{group} - 1] ],
_range( 0, $#$body_rows, $args{group} )
];
pop @{ $body_rows->[-1] } while !defined $body_rows->[-1][-1];
} else {
$body_rows = [ $body_rows ];
}
push @cdata, (
( $head ? { tag => 'thead', attr => $args{thead}, cdata => $head_row } : () ),
( $foot ? { tag => 'tfoot', attr => $args{tfoot}, cdata => $foot_row } : () ),
( map { tag => 'tbody', attr => $args{tbody}, cdata => $_ }, @$body_rows ),
);
} else {
push @cdata, map { tag => 'tr', attr => $args{tr}, cdata => $_ }, @{ $args{data} };
}
return $args{_auto}->tag( tag => 'table', attr => $args{table}, cdata => \@cdata );
}
sub _args {
my ($self,@data,$data,@args,$args);
$self = shift if UNIVERSAL::isa( $_[0], __PACKAGE__ );
$data = shift if (@_ == 1);
while (@_) {
if (ref( $_[0] )) {
push @data, shift;
if (ref( $_[0] )) {
push @data, shift;
} elsif (defined $_[0]) {
push @args, shift, shift;
}
} else {
push @args, shift, shift;
}
}
$data ||= (@data == 1) ? $data[0] : (@data) ? [ @data ] : undef;
$args = scalar @args ? { @args } : {};
$args = { %{ $self || {} }, %{ $args || {} } };
$data = delete $args->{data} if exists $args->{data};
$args->{_auto} ||= HTML::AutoTag->new(
indent => $args->{indent},
level => $args->{level},
sorted => $args->{sorted_attrs},
);
return ( $self, $self->{data}, $args ) if $self and $self->{is_cached};
$args->{worksheet} ||= 1;
$args->{worksheet} = 1 if $args->{worksheet} < 1;
if ($args->{file}) {
$data = Spreadsheet::HTML::File::Loader::_parse( $args, $data );
unlink $args->{file} if $args->{_unlink};
}
$data = [ $data ] unless ref($data) eq 'ARRAY';
$data = [ $data ] unless ref($data->[0]) eq 'ARRAY';
if ($args->{wrap} and defined $data->[0][0]) {
my @flat = map @$_, @$data;
$data = [
map [ @flat[$_ .. $_ + $args->{wrap} - 1] ],
_range( 0, $#flat, $args->{wrap} )
];
}
$data = Spreadsheet::HTML::Engine::_apply( $data, $args->{apply} ) if $args->{apply};
$args->{_max_rows} = scalar @{ $data } || 1;
$args->{_max_cols} = scalar @{ $data->[0] } || 1;
if ($args->{fill}) {
my ($row,$col) = split /\D/, $args->{fill};
$args->{_max_rows} = $row if (int($row || 0)) > ($args->{_max_rows});
$args->{_max_cols} = $col if (int($col || 0)) > ($args->{_max_cols});
}
return ( $self, [ map [@$_], @$data], $args );
}
sub _extrapolate {
my ( $cdata, $attr, $thingy ) = @_;
my $new_attr;
$thingy = [ $thingy ] unless ref( $thingy ) eq 'ARRAY';
for (@{ $thingy }) {
if (ref($_) eq 'CODE') {
$cdata = $_->($cdata);
} elsif (ref($_) eq 'HASH') {
$new_attr = $_;
}
}
$attr = { %{ $attr || {} }, %{ $new_attr || {} } };
return ( $cdata, $attr );
}
sub _colgroup {
my %args = @_;
my @colgroup;
$args{col} = [ $args{col} ] if ref($args{col}) eq 'HASH';
if (ref($args{col}) eq 'ARRAY') {
if (ref $args{colgroup} eq 'ARRAY') {
@colgroup = map {
tag => 'colgroup',
attr => $_,
cdata => [ map { tag => 'col', attr => $_ }, @{ $args{col} } ]
}, @{ $args{colgroup} };
} else {
@colgroup = {
tag => 'colgroup',
attr => $args{colgroup},
cdata => [ map { tag => 'col', attr => $_ }, @{ $args{col} } ]
};
}
} else {
$args{colgroup} = [ $args{colgroup} ] if ref($args{colgroup}) eq 'HASH';
if (ref $args{colgroup} eq 'ARRAY') {
@colgroup = map { tag => 'colgroup', attr => $_ }, @{ $args{colgroup} };
}
}
return @colgroup;
}
sub _tag {
my %args = @_;
my $thingy = $args{ $args{tag} };
return unless defined $thingy;
my $tag = { tag => $args{tag}, cdata => $thingy };
if (ref $thingy eq 'HASH') {
$tag->{cdata} = ( keys %$thingy )[0];
$tag->{attr} = ( values %$thingy )[0];
}
return $tag;
}
# credit: Math::Matrix
sub _transpose {
my $data = shift;
my @trans;
for my $i (0 .. $#{ $data->[0] }) {
push @trans, [ map $_->[$i], @$data ]
}
return \@trans;
}
sub _range {grep!(($_-$_[0])%($_[2]||1)),$_[0]..$_[1]}
1;
__END__
=head1 NAME
Spreadsheet::HTML - Just another HTML table generator.
=head1 SYNOPSIS
Object oriented interface:
use Spreadsheet::HTML;
my @data = ( [qw(foo b&r b&z)], [1,2,3], [4,5,6], [7,8,9] );
my $gen = Spreadsheet::HTML->new( data => \@data, encode => 1 );
print $gen->portrait( indent => ' ' );
print $gen->landscape( indent => "\t" );
$gen = Spreadsheet::HTML->new( file => 'data.xls', worksheet => 2 );
print $gen->generate( preserve => 1 );
Procedural interface:
use Spreadsheet::HTML qw( portrait landscape );
print portrait( \@data, td => sub { sprintf "%02d", shift } );
print landscape( \@data, tr => { class => [qw(odd even)] } );
=head1 DESCRIPTION
Generate HTML tables with ease (HTML4, XHTML and HTML5). Generate portrait,
landscape and other rotated views, Handsontable tables, HTML calendars,
checkerboard patterns, games such as sudoku, banners and mazes, and create
animations of cell values and backgrounds via jQuery. Transform Excel, HTML,
JSON, CSV, YAML, PNG, JPEG and GIF files instantly into HTML tables.
=head1 CLI TOOLS
=over 4
=item * C<mktable>
Quickly generate tables without writing a script:
$ mktable landscape --param file=data.xls --param preserve=1 > out.html
If you have L<HTML::Display> installed, you can direct the output to
your default browser:
$ mktable landscape --param data=[[a..d],[1..4],[5..8]] --display
$ mktable conway --param data=[1..300] --param wrap=20 --param matrix=1 --display
$ mktable sudoku --display
$ mktable tictactoe --display
$ mktable calendar --param today='{bgcolor=>"red"}' --display
$ mktable beadwork --param preset=dk --display
Run C<mktable --man> for more documentation. You can also use this tool to quickly look
up documentation for methods and (most) parameters:
$ mktable --help handson
$ mktable --help theta
$ mktable --help td
=item * C<benchmark-spreadsheet-html>
Run benchmarks against several different HTML table generators
available on CPAN. See C<benchmark-spreadsheet-html --man> for more.
=back
=head1 METHODS
All methods (except C<new>) are exportable as functions. They all
accept the same named parameters (see PARAMETERS below). With the
exception of C<new>, all methods return an HTML table as a scalar string.
=over 4
=item * C<new( %params )>
my $generator = Spreadsheet::HTML->new( data => $data );
Constructs object. Accepts the same named parameters as the table
generating methods below:
=item * C<generate( %params )>
Generate an HTML table.
print $generator->generate();
=item * C<portrait( %params )>
Generate an HTML table with headings positioned at the top.
print $generator->portrait();
=for html
<table style="border: 1px dashed #A0A0A0"><tr><td><b> heading1 </b></td><td><b> heading2 </b></td><td><b> heading3 </b></td><td><b> heading4 </b></td></tr><tr><td> row1col1 </td><td> row1col2 </td><td> row1col3 </td><td> row1col4 </td></tr><tr><td> row2col1 </td><td> row2col2 </td><td> row2col3 </td><td> row2col4 </td></tr><tr><td> row3col1 </td><td> row3col2 </td><td> row3col3 </td><td> row3col4 </td></tr></table>
=item * C<north( %params )>
Alias for C<portrait()>.
print $generator->north();
=item * C<landscape( %params )>
Generate an HTML table with headings positioned at the left.
print $generator->landscape();
=for html
<table style="border: 1px dashed #A0A0A0"><tr><td><b> heading1 </b></td><td> row1col1 </td><td> row2col1 </td><td> row3col1 </td></tr><tr><td><b> heading2 </b></td><td> row1col2 </td><td> row2col2 </td><td> row3col2 </td></tr><tr><td><b> heading3 </b></td><td> row1col3 </td><td> row2col3 </td><td> row3col3 </td></tr><tr><td><b> heading4 </b></td><td> row1col4 </td><td> row2col4 </td><td> row3col4 </td></tr></table>
=item * C<west( %params )>
Alias for C<landscape>.
print $generator->west();
=item * C<south( %params )>
Generate an HTML table with headings positioned at the bottom.
print $generator->south();
=for html
<table style="border: 1px dashed #A0A0A0"><tr><td> row1col1 </td><td> row1col2 </td><td> row1col3 </td><td> row1col4 </td></tr><tr><td> row2col1 </td><td> row2col2 </td><td> row2col3 </td><td> row2col4 </td></tr><tr><td> row3col1 </td><td> row3col2 </td><td> row3col3 </td><td> row3col4 </td></tr><tr><td><b> heading1 </b></td><td><b> heading2 </b></td><td><b> heading3 </b></td><td><b> heading4 </b></td></tr></table>
=item * C<east( %params )>
This method generates an HTML table with headings positioned at the right.
print $generator->east();
=for html
<table style="border: 1px dashed #A0A0A0"><tr><td> row1col1 </td><td> row2col1 </td><td> row3col1 </td><td><b> heading1 </b></td></tr><tr><td> row1col2 </td><td> row2col2 </td><td> row3col2 </td><td><b> heading2 </b></td></tr><tr><td> row1col3 </td><td> row2col3 </td><td> row3col3 </td><td><b> heading3 </b></td></tr><tr><td> row1col4 </td><td> row2col4 </td><td> row3col4 </td><td><b> heading4 </b></td></tr></table>
=back
For most cases, C<portrait> (headings at top)
and C<landscape> (headings at left) are all you need.
=head1 PARAMETERS
All methods/procedures accept the same named parameters. Some methods
pre-define parameters on your behalf, for convenience. If these parameters
are defined, they may be overriden where applicable. You do not necessarily
have to specify C<data>, any bare array references will be collected
and assigned to C<data>. Just because you can, however, does not
mean you should. Everything is meant to be convenient.
=head2 LITERAL PARAMETERS
Literal parameters provides the means to modify the macro
aspects of the table, such as indentation, encoding, data
source and table orientation.
=over 4
=item * C<data>
The data to be rendered into table cells. Should be
an array ref of array refs.
data => [["a".."c"],[1..3],[4..6],[7..9]]
=item * C<file>
String. The name of the data file to read. Supported formats
are XLS, CSV, JSON, YAML, HTML, GIF, PNG and JPG.
file => 'foo.json'
C<file> overrides C<data>. You generally either specify C<data>
or C<file> but not both (unless the file is an image in which the
two can be combined).
data => \@data, file => 'background.png'
See L<Spreadsheet::HTML::File::Loader> for more on supported file formats.
See L<DBIx::HTML> for formatting your SQL database queries.
If you don't mind having a temp file created on your behalf, you can
also supply a URI:
file => 'http://your.site.com/path/to/foo.json'
The temp file will be unlinked unless you set the super secret hidden
param C<_unlink> to zero (0):
_unlink => 0
=item * C<block>
Integer. Can be supplied in conjunction with C<file> for image
formats (JPEG, PNG and GIF). Defaults to 8. Cannot be less than 1.
Representing an image as an HTML table on a pixel by pixel basis
tends to produce tables that are too large. This parameter can be
used to increase and decrease that size, by reading in blocks of
pixels (8x8=64 pixels to be represented by one table cell) and
determining the pixel color for that block (see C<blend> below).
The smaller the block size the longer the processing time.
block => 4
=item * C<blend>
Boolean. Default false. Can be supplied in conjuction with C<block>
to change the algorithm for determining block's pixel color. When
set to true, the pixel color is determined by averaging the colors of
all pixels in that block. When false (default), the pixel color is
determined by finding the the most used color in the block.
blend => 1
Turning C<blend> on tends to work better for photo realism. Keep it
off for producing 1980's style pixel art images.
=item * C<worksheet>
Integer. Can be supplied in conjunction with C<file>. If multiple
worksheets or data tables (or image frames) are present, use this
parameter to select which one (index 1 based). Defaults to 1 (first found).
worksheet => 3 # the third worksheet, data table or image frame found
=item * C<preserve>
Boolean. Can be supplied in conjunction with C<file>. Attempts to copy
over all formatting styles from original document to table.
Styles are not currently deduped, so use with care as the
final output will contain a lot of redundant cell styles.
preserve => 1
=item * C<fill>
String. Can be supplied instead of C<data> to generate empty
cells, or in conjunction with C<data> to pad existing
cells (currently pads the right and bottom sides only.)
fill => '5x12'
=item * C<wrap>
Integer. Can be supplied in conjunction with a 1D C<data> to
automatically wrap into a 2D array matrix. Can also
"rewrap" existed 2D array matrices, but at the expense
of likely mangling the headings.
wrap => 10
=item * C<apply>
String. Applies formulas parsable by L<Spreadsheet::Engine> to data.
apply => 'set B6 formula SUM(B2:B5)'
Accepts lists:
apply => ['set B6 formula SUM(B2:B5)', 'set C6 formula SUM(C2:C5)']
Can be used to create total and sub total rows. See
L<Spreadsheet::Engine> for more.
=item * C<theta: 0, 90, 180, 270, -90, -180, -270>
Integer. Rotates table clockwise for positive values and
counter-clockwise for negative values. Default to 0:
headers at top. 90: headers at right. 180: headers at bottom.
270: headers at left. To achieve landscape, use -270.
theta => -270
=item * C<flip>
Boolean. Flips table horizontally from the perspective of
the headings "row" by negating the value of C<theta>.
flip => 1
=item * C<pinhead>
Boolean. Works in conjunction with C<theta> to ensure reporting
readability. Without it, C<south()> and C<east()> would
have data cells arranged in reverse order.
pinhead => 1
=item * C<indent>
String. Render the table with nested indentation. Defaults to
undefined which produces no indentation. Adds newlines
when set to any value that is defined.
indent => ' '
indent => "\t"
=item * C<level>
Integer. Start indentation at this level. Useful for matching
nesting styles of original HTML text that you may want
to insert into to.
level => 4
This value does not say 'use 4 spaces', it applies the
repetition operator to the value of C<indent> 4 times.
=item * C<encode>
Boolean. Encode HTML entities. Defaults to false, which produces no encoding.
If set to true without further specifying a value for C<encodes> (see below),
will encode all control chars, high bit chars and '<', '&', '>', ''' and '"'.
encode => 1
=item * C<encodes>
String. Set value to those characters you wish to have HTML encoded.
encodes => '<>"'
=item * C<empty>
String. Replace empty cells with this value. Defaults to C< >.
Set value to C<undef> to avoid any substitutions.
empty => ' '
=item * C<matrix>
Boolean. Render the headings row with only <td> tags, no <th> tags.
matrix => 1
=item * C<headless>
Boolean. Render the table with without the headings row at all.
Any configuration to C<headings> or C<-r0> will be discarded with
the headings row.
headless => 1
=item * C<tgroups>
Integer. Group table rows into <thead>, <tbody> and <tfoot> sections.
When C<tgroups> is set to 1, the <tfoot> section is
omitted. The last row of the data is found at the end
of the <tbody> section instead. (loose)
tgroups => 1
When C<tgroups> is set to 2, the <tfoot> section is found
in between the <thead> and <tbody> sections. (strict)
tgroups => 2
=item * C<group>
Integer. Will chunk body rows into tbody groups of size C<group>.
group => 4
Currently only accepts integers (not column names).
=item * C<cache>
Boolean. Preserve data after it has been processed (and loaded).
Useful for loading data from files only once.
cache => 1
=item * C<scroll>
Boolean. Scrolls the table cells. See L<Spreadsheet::HTML::Presets::Scroll>.
scroll => 1
=item * C<headings>
Apply callback subroutine to each cell in headings row.
headings => sub { join(" ", map {ucfirst lc $_} split "_", shift) }
Or apply hash reference as attributes:
headings => { class => 'some-class' }
Or both via array reference:
headings => [ sub { uc shift }, { class => "foo" } ]
Since C<headings> is a natural alias for the dynamic parameter
C<-r0>, it could be considered as a dynamic parameter. Be
careful not to prepend a dash to C<headings> ... only dynamic
parameters use leading dashes.
=item * C<sorted_attrs>
Boolean. This is useful for ensuring that attributes within tags are
rendered in alphabetical order, for consistancy. You will most likely
never need this feature.
sorted_attrs
=back
=head2 DYNAMIC PARAMETERS
Dynamic parameters provide a means to control the micro
elements of the table, such as modifying headings by their
name and rows and columns by their indices (X). They contain
leading dashes to seperate them from literal and tag parameters.
=over 4
=item * C<-rX>
Apply this callback subroutine to all cells in row X.
(0 index based)
-r3 => sub { uc shift }
Or apply hash ref as attributes:
-r3 => { class => 'some-class' }
Or both:
-r3 => [ sub { uc shift }, { class => "foo" } ]
=item * C<-cX>
Apply this callback to all cells in column X.
(0 index based)
-c4 => sub { sprintf "%02d", shift || 0 }
Or apply hash ref as attributes:
-c4 => { class => 'some-class' }
Or both:
-c4 => [ sub { uc shift }, { class => "foo" } ]
You can alias any column number by the value of the heading
name in that column:
-occupation => sub { "<b>$_[0]"</b>" }
-salary => { class => 'special-row' }
-employee_no => [ sub { sprintf "%08d", shift }, { class => "foo" } ]
=item * C<-rXcX>
Apply this callback or hash ref of attributres
to the cell at row X and column X. (0 index based)
-r3c4 => { class => 'special-cell' }
=back
=head2 TAG PARAMETERS
Tag parameters provide a means to control the attributes
of the table's tags, and in the case of <th> and <td> the
contents via callback subroutines. Although similar in form,
they are differentiated from litertal parameters because they
share the names of the actual HTML table tags.
=over 4
=item * C<table>
Hash ref. Apply these attributes to the specified tag.
table => { border => 1 }
=item * C<thead>
Hash ref. Apply these attributes to the specified tag.
thead => { class => 'heading' }
=item * C<tfoot>
Hash ref. Apply these attributes to the specified tag.
tfoot => { class => 'footing' }
=item * C<tbody>
Hash ref. Apply these attributes to the specified tag.
tbody => { class => 'body' }
=item * C<tr>
Hash ref. Apply these attributes to the specified tag.
tr => { style => { background => [qw( color1 color2 )]' } }
Does not apply to <tr> groups found within <thead> or <tfoot>.
(See C<thead.tr> and C<tfoot.tr> below.)
=item * C<th>
Hash ref, sub ref or array ref containing either.
th => { class => 'heading' }
th => sub { uc shift }
th => [ sub { uc shift }, { class => 'heading' } ]
=item * C<td>
Hash ref, sub ref or array ref containing either.
td => { class => 'cell' }
td => sub { uc shift }
td => [ sub { uc shift }, { class => 'cell' } ]
=item * C<caption>
Caption is special in that you can either pass a string to
be used as CDATA or a hash whose only key is the string
to be used as CDATA.
caption => "Just Another Title"
caption => { "A Title With Attributes" => { align => "bottom" } }
=item * C<colgroup>
Add colgroup tag(s) to the table. Use an AoH for multiple.
colgroup => { span => 2, style => { 'background-color' => 'orange' } }
colgroup => [ { span => 20 }, { span => 1, class => 'end' } ]
=item * C<col>
Add col tag(s) to the table. Use an AoH for multiple. Wraps
tags within a colgroup tag. Same usage as C<colgroup>.
col => { span => 2, style => { 'background-color' => 'orange' } }
col => [ { span => 20 }, { span => 1, class => 'end' } ]
=item * C<thead.tr>
When C<tgroups> is 1 or 2, this tag parameter is available to control
the attributes of the <tr> tag within the <thead> group.
'tr.head' => { class => 'heading-row' }
=item * C<tfoot.tr>
When C<tgroups> is 2, this tag parameter is available to control
the attributes of the <tr> tag within the <tfoot> group.
'tr.foot' => { class => 'footing-row' }
=back
=head1 PRESET METHODS
The following preset methods are availble for creating tables that can be used
with little to no additional coding. All preset methods accept all of the
above mentioned parameters (%params) in addition to those specific to themselves.
=over 4
=item * C<layout( %params )>
Generate layout tables.
=item * C<list( ordered, col, row, %params )>
Generate <ol> and <ul> lists.
=item * C<select( col, row, values, selected, placeholder, optgroup, label, %params )>
Generate <select> form elements.
=item * C<handson( args, jquery, handsonjs, css, %params )>
Generate Handsontable tables. (Excel like interface for browsers.)
=item * C<checkerboard( colors || class, %params )>
Generate checkerboard patterns in cell backgrounds. Specify an array of colors to be
arranged in checkerboard pattern or array of class names (for external CSS).
=item * C<banner( on, off, text, font, dir, emboss, %params )>
Generate banners via Text::FIGlet.
=item * C<scroll( fgdirection, fx, fy, bgdirection, bx, by, interval, jquery, %params )>
Scroll table cell foregrounds and backgrounds.
=item * C<calendar( month, year, today, -day, %params )>
Generate calendars.
=item * C<calculator( jquery, %params )>
Generate a simple HTML table calculator.
=item * C<beadwork( preset, art, map, bgcolor, %params )>
Turn cell backgrounds into 8-bit pixel art.
=item * C<conway( on, off, colors, fade, interval, jquery, %params )>
Turn cell backgrounds into Conway's game of life.
=item * C<sudoku( blanks, attempts, jquery, %params )>
Generate 9x9 HTML table sudoku boards.
=item * C<maze( on, off, %params )>
Generates a static maze.
=item * C<tictactoe( jquery, %params )>
Creates a playable Tic-Tac-Toe game board.
=item * C<draughts( on, off, jquery, %params )>
=item * C<checkers( on, off, jquery, %params )>