-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
niceTables.pl
1593 lines (1299 loc) · 45.9 KB
/
niceTables.pl
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
=head1 niceTables.pl
Subroutines for creating tables that:
=over
=item * conform to accessibility standards in HTML output
=item * have uniform styling across output formats, to the degree possible
=item * may use CSS for additional HTML styling
=item * may use LaTeX commands for additional hardcopy styling
=back
C<DataTable()> creates a table displaying data. It should not
be used for layout, such as displaying an array of graphs.
C<LayoutTable()> creates a "table" without using an HTML table in HTML
output. Use C<LayoutTable()> whenever you are simply laying out content
for space-saving purposes. Ask yourself if there is any meaningful
relation between content cells within a column or within a row. If the
answer is no in both cases, it is likely a case for C<LayoutTable()>.
=head2 Description
Command for a typical table:
DataTable(
[
[a,b,c,...],
[d,e,f,...],
...
],
options
);
LayoutTable(
[
[a,b,c,...],
[d,e,f,...],
...
],
options
);
The cell entries above like C<a> may be simple cell content,
a hash reference with C<data =E<gt> cellContent> and options,
or an array reference where the 0th entry is the the cell content
and it is followed by option key-value pairs.
As much as possible, options apply to all output formats.
Some options only apply to HTML, and some apply only to PDF.
Not all options are supported by every output format.
For example PreTeXt cannot use color information.
All features described below apply to a C<DataTable>.
Most apply to a C<LayoutTable> as well, but not
C<caption>, C<rowheaders>, C<header>, C<colspan>, or C<headerrow>.
=head2 Options for the WHOLE TABLE
=head3 All output formats
=over
=item C<center =E<gt> 0 or 1>
center the table (default 1)
=item C<caption =E<gt> string>
caption for the table
=item C<horizontalrules =E<gt> 0 or 1>
make rules above and below every row (default 0)
=item C<texalignment =E<gt> string>
an alignment string like is used in a LaTeX tabular environment: for example C<'r|ccp{1in}'>
C<l> for left-aligned column
C<c> for center-aligned column
C<r> for right-aligned column
C<p{width}> for a column with left-aligned paragraphs of fixed width.
The width needs to be absolute to work in all output formats.
C<X> for a column that expands to fill (see C<Xratio> below),
and will have left-aligned paragraphs
C<|> for a vertical rule (n adjacent pipes make one rule that is n times as thick)
C<!{\vrule width 3pt}> for a vertical rule of the indicated width
(must be an absolute width; C<3pt> is just an example)
C<E<gt>{commands}> Execute C<commands> at each cell in the column.
For example, C<'cE<gt>{\color{blue}}c'> will make the second column have blue text.
The following LaTeX commands may be used:
=over
C<\color{colorname}> for text color
C<\color[HTML]{xxxxxx}> for text color (xxxxxx is a 6-character hex color code)
C<\columncolor{colorname}> for background color
C<\columncolor[HTML]{xxxxxx}> for background color (xxxxxx is a 6-character hex color code)
C<\bfseries> for bold
C<\itshape> for italics
C<\ttfamily> for monospace
=back
Other LaTeX commands apply only to PDF output.
=item C<align =E<gt> string>
convenient short version of C<texalignment>
=item C<Xratio =E<gt> number>
When C<X> is part of overall alignment,
C<Xratio> must be some number between 0 and 1, inclusive of 1.
The table as a whole will be C<Xratio> wide, relative to the overall
horizontal space. And C<X> columns expand to fill the available space.
The default is 0.97.
=item C<encase =E<gt> [ , ]>
Encases all table entries in the two entries. For example, use C<[$BM,$EM]>
to wrap all cells in math delimiters. See also C<noencase> for individual cells.
=item C<rowheaders =E<gt> 0 or 1>
Make the first element of every row a row header. Default is 0.
=item C<headerrules =E<gt> 0 or 1>
Make a horizontal rule under a row of column headers and a vertical
rule to the right of a column of row headers. Default is 1.
=item C<valign =E<gt> 'top'>
Can be C<'top'>, C<'middle'>, or C<'bottom'>. Applies to all rows.
See below to override for an individual row.
=item C<padding =E<gt> [ , ]>
An array of two non-negative numbers used to define cell-padding. The first is
for top-down padding, the second for left-right padding. In HTML, each padding
is the value multiplied by 0.85rem. In LaTeX, the left-right padding is the
value multiplied by 10pt, and the top-down padding is implemented by setting
C<\arraystretch> to the value. (0.85rem and 10pt are default font sizes at the
time of this feature's introduction.) The default for a DataTable is C<[0,0.5]>
and the default for a LayoutTable is C<[1,1]>. C<padding> may also be entered
as a single nonnegative number to describe both top-down and left-right
padding at the same time.
=back
=head3 HTML output
Each css property setting should be a hash reference.
For example, C<{'font-family' =E<gt> 'fantasy', color =E<gt> 'red'}>.
If a key has a dash character, it needs to be in quotes. Alternatively,
you may uses a javascript flavor of CSS key like C<{fontFamily =E<gt> 'fantasy'}>
=over
=item C<tablecss =E<gt> css string>
css styling commands for the table element
=item C<captioncss =E<gt> css string>
css styling commands for the caption element
=item C<columnscss => array ref
an array reference to css strings for columns
Note: only four css properties apply to a col element:
=over
=item * C<border> (family)
=item * C<background> (family)
=item * C<width>
=item * C<column-span>
=back
=item C<datacss =E<gt> css string>
css styling commands for non-header cells
=item C<headercss =E<gt> css string>
css styling commands for header cells
=item C<allcellcss =E<gt> css string>
css styling commands for all cells
=back
=head3 PDF hardcopy output
=over
=item C<booktabs =E<gt> 0 or 1>
use the booktabs package for horizontal rules (default 1)
=back
=head2 Options for CELLS
Each cell entry can be an array reference where the first entry is the actual cell
content, and then key-value pairs follow. For example, in a table with four columns,
to make the first cell span two columns, enter the first cell as an array reference:
[[a, colspan => 2], b, c]
Alternatively, using a hash reference with a data key:
[{data => a, colspan => 2}, b, c]
=head3 All output formats
=over
=item C<halign =E<gt> string>
Similar to the components for C<texalignment> above.
However, only C<l>, C<c>, C<r>, C<p{}>, and vertical rule specifications should be used.
With vertical rule specifiers, any left vertical rule will only be observed for cells
is in the first column. Otherwise, use a right vertical rule on the cell to the left.
=item C<header =E<gt> type>,
Declares the scope of the HTML C<th> element. Case-insensitive:
=over
=item * C<th> for a generic table header
=item * C<ch> for a column header (C<col> and C<column> work too)
=item * C<rh> for a row header (C<row> works too)
=item * C<td> for overriding a C<headerrow> or C<rowheaders> option
(except PTX output cannot honor this)
=back
=item C<color =E<gt> string>
color name or 6-character hex color code for text color
=item C<bgcolor =E<gt> string>
color name or 6-character hex color code for background color
=item C<b=E<gt>1>
Set the cell to bold font.
=item C<i=E<gt>1>
Set the cell to italics font.
=item C<m=E<gt>1>
Set the cell to monospace font.
=item C<noencase =E<gt> 0 or 1>
If you are using encase (see above) use this to opt out.
=item C<colspan =E<gt> positive integer>
Makes the cell span more than one column. When using this, you
often set C<halign> as well.
=item C<top =E<gt> positive integer or string>
Make a top rule for one cell if the cell is in the top row. Thickness is either C<n>
pixels or a width like C<'0.04em'>. Has no effect on cells outside of top row.
=item C<bottom =E<gt> positive integer or string>
Make a bottom rule for one cell. Thickness is either C<n> pixels or a width like C<'0.04em'>.
=back
=head3 HTML output
This option is only for HTML output.
=over
=item C<cellcss =E<gt> string>
css styling commands for this cell
=back
=head3 PDF hardcopy output
The following apply only to PDF output
=over
=item C<texpre =E<gt> tex code> and C<texpost =E<gt> tex code>
For more fussy cell-by-cell alteration of the tex version of
the table, code to place before and after the cell content.
=item C<texencase =E<gt> array ref>
Shortcut for entering C<[texpre,texpost]> at once.
=back
=head2 Options for ROWS
Some parameters in a cell's options array affect the entire row.
When there is a clash, the last non-falsy declaration in the row will be used.
=over
=item C<rowcolor =E<gt> string>
Sets the row's background color. Must be a color name, 6-character hex color code.
=item C<rowcss =E<gt> string>
css styling commands for the row
=item C<headerrow =E<gt> 0 or 1>
Makes an entire row use header cells (with column scope).
=item C<rowtop =E<gt> positive integer or string>
When used on the first row, creates a top rule. Has no effect on other rows.
Thickness is either C<n> pixels or a width like C<'0.04em'>.
=item C<rowbottom =E<gt> positive integer string>
Make a bottom rule. Thickness is either C<n> pixels or a width like C<'0.04em'>.
=item C<valign =E<gt> string>
Override table's overall vertical alignment for this row. Can be C<'top'>, C<'middle'>,
or C<'bottom'>.
=back
=head2 Options for COLUMNS
Column styling is handled indirectly for now, mostly through the C<texalignment> option above.
=head2 Deprecations
These features were supported in an earlier version and still work, but are deprecated.
=over
=item * Each css setting can be a raw CSS string, including all its colons and a semicolons.
For example, C<tablecss =E<gt> 'font-family: fantasy; text-decoration: underline;'>.
=item * A cell can have C<tex =E<gt> commands>.
This executes commands at start of a cell with scope the entire cell.
The following LaTeX commands may be used and respected in HTML as well as LaTeX:
=over
=item * C<\color{colorname}> for text color
=item * C<\color[HTML]{xxxxxx}> for text color (xxxxxx is a 6-character hex color code)
=item * C<\columncolor{colorname}> for background color
=item * C<\columncolor[HTML]{xxxxxx}> for background color (xxxxxx is a 6-character hex color code)
=item * C<\bfseries> for bold
=item * C<\itshape> for italics
=item * C<\ttfamily> for monospace
=back
Other LaTeX commands apply only to hardcopy output.
=item * C<rowcolor> can be in the form C<'[HTML]{xxxxxx}'>
=back
=cut
sub _niceTables_init {
main::PG_restricted_eval('sub DataTable { NiceTables::DataTable(@_) }');
main::PG_restricted_eval('sub LayoutTable { NiceTables::LayoutTable(@_) }');
}
package NiceTables;
sub DataTable {
my $userArray = shift;
# cleaned up and initialized version of the user's array of cell data and cell/row options
# $tableArray references a 2D array for the table, with entries being a hash reference
# The data key is the cell content, and other keys are (initialiized) options for the cell
my $tableArray = TableArray($userArray);
# establish the true number of columns, accounting for all uses of colspan
my $colCount = ColumnCount($tableArray);
# $tableOpts is a hash reference keeping the (initialized) global table options
my $tableOpts = TableOptions($colCount, @_);
# $alignment is a 1D array of hash references, with options for each column
my $alignment = ParseAlignment($tableOpts->{texalignment});
# if the user's data implies fewer cells in any row than what is in texalignment
# then we add empty data cells, update $colCount, $tableOpts, and $alignment
my $needToUpdate;
for my $j (0 .. $#$tableArray) {
my $lastCol = $tableArray->[$j][-1]{rightcol};
for my $i ($lastCol + 1 .. $#$alignment) {
$needToUpdate = 1;
push(
@{ $tableArray->[$j] },
{
data => '',
leftcol => $i,
rightcol => $i,
halign => '',
header => '',
tex => '',
noencase => 0,
colspan => 1,
cellcss => '',
texpre => '',
texpost => '',
rowcolor => '',
rowcss => {},
headerrow => '',
rowtop => 0,
rowbottom => 0,
top => 0,
bottom => 0,
valign => ''
}
);
}
}
if ($needToUpdate) {
$colCount = ColumnCount($tableArray);
$tableOpts = TableOptions($colCount, @_);
$alignment = ParseAlignment($tableOpts->{texalignment});
}
# if the user's data implies more columns than what they specified in texalignment
# then we add columns to both $alignment and $tableOpts->{texalignment}
for my $i ($#$alignment + 1 .. $colCount) {
$alignment->[$i] = { halign => 'c', valign => '', right => '', width => '', tex => '' };
$tableOpts->{texalignment} .= 'c';
}
return TableEnvironment($tableArray, $tableOpts, $alignment);
}
sub LayoutTable {
return DataTable(@_, LaYoUt => 1);
}
# Make the outer table environment
sub TableEnvironment {
my ($tableArray, $tableOpts, $alignment) = @_;
# determine if somewhere in the alignment there are X columns
my $hasX = 0;
for my $align (@$alignment) {
if ($align->{halign} eq 'X') {
$hasX = 1;
last;
}
}
# determine if first row has a top border
my $top = '';
for my $x (@{ $tableArray->[0] }) {
$top = $x->{rowtop} if ($x->{rowtop});
}
my $booktabs = $tableOpts->{booktabs};
my $cols = Cols($tableArray, $tableOpts, $alignment);
my $rows = Rows($tableArray, $tableOpts, $alignment);
if ($main::displayMode eq 'TeX') {
my $tabulartype = $hasX ? 'tabularx' : 'tabular';
my $tabularwidth = $hasX ? "$tableOpts->{Xratio}\\linewidth" : '';
$rows = latexEnvironment($rows, $tabulartype, [ $tabularwidth, '[t]', $tableOpts->{texalignment} ], ' ');
$rows = prefix($rows, '\centering%') if $tableOpts->{center};
$rows = prefix($rows, '\renewcommand{\arraystretch}{' . ($tableOpts->{padding}[0] + 1) . '}', '');
$rows = prefix($rows, '\setlength{\tabcolsep}{' . ($tableOpts->{padding}[1] * 10) . 'pt}', '');
$rows = suffix(
$rows,
"\\captionsetup{textfont={sc},belowskip=12pt,aboveskip=4pt}\\captionof*{table}{$tableOpts->{caption}}",
' '
) if ($tableOpts->{caption});
$rows = wrap($rows, '\par', '\par', '');
$rows = wrap($rows, '{', '}', '');
} elsif ($main::displayMode eq 'PTX') {
my $ptxleft = getPTXthickness($alignment->[0]{left});
my $ptxtop = '';
if ($tableOpts->{horizontalrules} && $booktabs) {
$ptxtop = 'major';
} elsif ($tableOpts->{horizontalrules}) {
$ptxtop = 'minor';
}
$ptxtop = getPTXthickness($top) if $top;
my $ptxwidth = '';
my $ptxmargins = '';
if ($hasX) {
$ptxwidth = $tableOpts->{Xratio} * 100;
my $leftmargin = ($tableOpts->{center}) ? (100 - $ptxwidth) / 2 : 0;
my $rightmargin = 100 - $ptxwidth - $leftmargin;
$ptxmargins = "${leftmargin}% ${rightmargin}%";
$ptxwidth .= '%';
} elsif (!$tableOpts->{center}) {
$ptxwidth = '100%';
$ptxmargins = '0% 0%';
}
if ($tableOpts->{LaYoUt}) {
$rows = tag(
$rows,
'sbsgroup',
{
width => $ptxwidth,
margins => $ptxmargins,
}
);
} elsif (!$tableOpts->{LaYoUt}) {
$rows = prefix($rows, $cols);
$rows = tag(
$rows,
'tabular',
{
valign => ($tableOpts->{valign} ne 'middle') ? $tableOpts->{valign} : '',
bottom => $tableOpts->{horizontalrules} ? 'minor' : '',
rowheaders => $tableOpts->{rowheaders} ? 'yes' : '',
margins => $ptxmargins,
width => $ptxwidth,
left => $ptxleft,
top => $ptxtop,
}
);
}
# We fake a caption as a tabular that follows the actual tabular
# This is not great, but PTX has no option to put a caption on a tabular
# (It can put a caption on a table, but we are not making a PTX table.)
my $ptxcaption = '';
if ($tableOpts->{caption}) {
$ptxcaption = $tableOpts->{caption};
$ptxcaption = tag($ptxcaption, 'cell');
$ptxcaption = tag($ptxcaption, 'row');
my $ptxcapwidth = '';
if ($hasX) {
$ptxcapwidth = $tableOpts->{Xratio} * 100 . '%';
} else {
$ptxcapwidth = '50%';
}
$ptxcapcol = tag('', 'col', { width => $ptxcapwidth });
$ptxcaption = prefix($ptxcaption, $ptxcapcol);
$ptxcaption = tag($ptxcaption, 'tabular', { width => $ptxwidth, margins => $ptxmargins });
}
$rows = suffix($rows, $ptxcaption);
} else {
my $css = css($tableOpts->{tablecss});
if ($hasX) {
$css .= css('width', $tableOpts->{Xratio} * 100 . '%');
}
$css .= css('border-left', getRuleCSS($alignment->[0]{left}));
$css .= css('margin', 'auto') if $tableOpts->{center};
my $htmlcols = '';
$htmlcols = tag($cols, 'colgroup')
unless ($cols =~ /^(<col>|\n)*$/ || $tableOpts->{LaYoUt});
$rows = prefix($rows, $htmlcols);
my $htmlcaption = tag($tableOpts->{caption}, 'caption', { style => css($tableOpts->{captioncss}) });
$rows = prefix($rows, $htmlcaption) if ($tableOpts->{caption} && !$tableOpts->{LaYoUt});
if ($tableOpts->{LaYoUt}) {
$css .= css('display', 'table');
$css .= css('border-collapse', 'collapse');
$rows = tag($rows, 'div', { style => $css });
} else {
$rows = tag($rows, 'table', { style => $css });
}
}
return $rows;
}
sub Cols {
my ($tableArray, $tableOpts, $alignment) = @_;
my $columnscss = $tableOpts->{columnscss};
my @cols = ();
# Loop through columns ($alignment->[0] is the left border not a column)
for my $i (1 .. $#$alignment) {
my $align = $alignment->[$i];
# determine if this column has any paragraph cells
my $width = '';
for my $y (@$tableArray) {
for my $x (@$y) {
# accounting for use of colspan...
if ($x->{leftcol} == $i && $x->{halign} =~ /^p\{([^}]*?)\}/) {
$width = $1;
}
}
}
# determine if this column has a top border
my $top = '';
for my $x (@{ $tableArray->[0] }) {
# accounting for use of colspan...
if ($x->{leftcol} <= $i && $i <= $x->{rightcol} && $x->{top}) {
$top = $x->{top};
}
}
if ($main::displayMode eq 'PTX') {
my $ptxhalign = '';
$ptxhalign = 'center' if ($align->{halign} eq 'c');
$ptxhalign = 'right' if ($align->{halign} eq 'r');
my $ptxright = '';
$ptxright = getPTXthickness($align->{right});
my $ptxtop = '';
$ptxtop = getPTXthickness($top);
my $ptxwidth = '';
$ptxwidth = getWidthPercent($align->{width}) if $align->{width};
$ptxwidth = ($tableOpts->{Xratio} / $#$alignment * 100) . '%'
if ($align->{halign} eq 'X');
$ptxwidth = getWidthPercent($width) if $width;
push(
@cols,
tag(
'', 'col',
{
halign => $ptxhalign,
right => $ptxright,
top => $ptxtop,
width => $ptxwidth
}
)
);
} else {
my $htmlright = '';
$htmlright .= css('border-right', 'solid 2px')
if ($i == 1 && $tableOpts->{rowheaders} && $tableOpts->{headerrules});
$htmlright .= css('border-right', getRuleCSS($align->{right}));
my $htmltop = '';
$htmltop .= css('border-top', getRuleCSS($top));
# $i starts at 1, but columncss indexing starts at 0
my $htmlcolcss = css($columnscss->[ $i - 1 ]);
if ($align->{tex} =~ /\\columncolor(\[HTML\])?\{(.*?)[}!]/) {
$htmlcolcss .= css('background-color', ($1 ? '#' : '') . $2);
}
push(@cols, tag('', 'col', { style => "${htmlright}${htmltop}${htmlcolcss}" }));
}
}
return join("\n", @cols);
}
sub Rows {
my ($tableArray, $tableOpts, $alignment) = @_;
my @rows;
my @htmlhead;
my @htmlbody;
my $htmlout;
my $stillinhtmlhead = 1;
for my $i (0 .. $#$tableArray) {
my $rowArray = $tableArray->[$i];
my $booktabs = $tableOpts->{booktabs};
my $row = Row($rowArray, $tableOpts, $alignment);
my $html = $row;
# establish if this row has certain things
# when declared mulltiple times, last non-falsy values are used
my $bottom = 0;
my $top = 0;
my $rowcolor = '';
my $headerrow = '';
my $valign = '';
for my $x (@$rowArray) {
$bottom = $x->{rowbottom} if ($x->{rowbottom});
$top = $x->{rowtop} if ($x->{rowtop} && $i == 0);
$rowcolor = $x->{rowcolor} if ($x->{rowcolor});
$headerrow = 'yes' if ($x->{headerrow});
$valign = $x->{valign} if ($x->{valign});
}
if ($main::displayMode eq 'TeX') {
# separator argument is space (not the default line break)
# to avoid PGML catcode manipulation issues
$row = prefix($row, "\\rowcolor" . formatColorLaTeX($rowcolor), ' ')
if ($rowcolor);
$row = prefix($row, hrule($booktabs, 'top', $top), ' ')
if ($top || ($i == 0 && $tableOpts->{horizontalrules}));
$row = suffix($row, "\\\\", ' ') unless ($i == $#$tableArray);
$row = suffix($row, hrule($booktabs, 'mid', $bottom), ' ')
if ($i < $#$tableArray && ($bottom || $tableOpts->{horizontalrules})
|| $headerrow && $tableOpts->{headerrules});
$row = suffix($row, "\\\\" . hrule($booktabs, 'bottom', $bottom), ' ')
if ($i == $#$tableArray
&& ($bottom || $tableOpts->{horizontalrules}));
# do cells in this row have a top or bottom border?
# although a propery of cells, LaTeX makes us do this at the row level
for my $x (@$rowArray) {
$row = prefix($row, hrule($booktabs, 'cmid', $x->{top}) . "{$x->{leftcol}-$x->{rightcol}}", ' ')
if ($i == 0 && $x->{top});
$row = suffix($row, hrule($booktabs, 'cmid', $x->{bottom}) . "{$x->{leftcol}-$x->{rightcol}}", ' ')
if $x->{bottom};
}
# if this row had a row color, disable that now or else with nested tables
# the row color will extend into subsequent rows (this seems like a colortbl bug)
$row = suffix($row, '\hiderowcolors', ' ') if $rowcolor;
push(@rows, $row);
} elsif ($main::displayMode eq 'PTX') {
my $ptxbottom = '';
if ($i == $#$tableArray && $tableOpts->{horizontalrules} && $booktabs) {
$ptxbottom = 'major';
} elsif ($tableOpts->{horizontalrules}) {
$ptxbottom = 'minor';
}
$ptxbottom = getPTXthickness($bottom) if $bottom;
my $ptxleft = '';
$ptxleft = 'minor' if ($rowArray->[0]{halign} =~ /^\s*\|/);
$ptxleft = 'medium' if ($rowArray->[0]{halign} =~ /^\s*\|\s*\|/);
$ptxleft = 'major' if ($rowArray->[0]{halign} =~ /^\s*\|\s*\|\s*\|/);
if ($rowArray->[0]{halign} =~ /^(?:\s|\|)*!\{\s*\\vrule\s+width\s+([^}]*?)\s*}/) {
$ptxleft = 'minor' if ($1);
$ptxleft = 'minor' if ($1 == '0.04em');
$ptxleft = 'medium' if ($1 == '0.07em');
$ptxleft = 'major' if ($1 == '0.11em');
}
$ptxleft = '' if ($ptxleft eq $alignment->[0]{left});
$ptxleft = "none"
if (!$ptxleft && $rowArray->[0]{halign} && $alignment->[0]{left});
if ($tableOpts->{LaYoUt}) {
my $ptxwidthsum = 0;
my $ptxautocols = $#alignment;
for my $j (1 .. $#alignment) {
if ($rowArray->[ $j - 1 ]{width}) {
$ptxwidthsum +=
substr getWidthPercent($tableArray->[ $j - 1 ]{width}),
0, -1;
$ptxautocols -= 1;
} elsif ($alignment->[$j]{width}) {
$ptxwidthsum += substr getWidthPercent($alignment->[$j]{width}), 0, -1;
$ptxautocols -= 1;
}
}
# determine if somewhere in the overall alignment, there are X columns
my $hasX = 0;
for my $align (@$alignment) {
if ($align->{halign} eq 'X') {
$hasX = 1;
last;
}
}
my $leftoverspace =
(($hasX) ? $tableOpts->{Xratio} * 100 : 100) - $ptxwidthsum;
my $divvyuptherest = 0;
$divvyuptherest = int($leftoverspace / $ptxautocols * 10000) / 10000
unless ($ptxautocols == 0);
my @ptxwidths;
for my $j (1 .. $#alignment) {
if ($rowOpts->[ $j - 1 ]{width}) {
push(@ptxwidths, getWidthPercent($rowOpts->[ $j - 1 ]{width}));
} elsif ($alignment->[$j]{width}) {
push(@ptxwidths, getWidthPercent($alignment->[$j]{width}));
} else {
push(@ptxwidths, $divvyuptherest . '%');
}
}
my $ptxwidths = join(" ", @ptxwidths);
$row = tag(
$row,
'sidebyside',
{
valign => ($valign) ? $valign : $tableOpts->{valign},
margins => '0% 0%',
widths => $ptxwidths,
}
);
} else {
$row = tag(
$row, 'row',
{
left => $ptxleft,
valign => $valign,
header => $headerrow,
bottom => $ptxbottom
}
);
}
push(@rows, $row);
} else {
my $css = '';
for my $x (@$rowArray) {
$css .= css($x->{rowcss});
}
$css .= css('background-color', formatColorHTML($rowcolor));
$css .= css('border-top', 'solid 3px')
if ($i == 0 && $tableOpts->{horizontalrules});
$css .= css('border-top', getRuleCSS($top));
$css .= css('border-bottom', 'solid 1px')
if ($i < $#$tableArray && $tableOpts->{horizontalrules});
$css .= css('border-bottom', 'solid 3px')
if ($i == $#$tableArray && $tableOpts->{horizontalrules});
$css .= css('border-bottom', getRuleCSS($bottom));
$css .= css('vertical-align', $valign);
if ($tableOpts->{LaYoUt}) {
$css .= css('display', 'table-row');
$html = tag($html, 'div', { style => $css });
push(@htmlbody, $html);
} else {
$html = tag($html, 'tr', { style => $css });
if ($stillinhtmlhead && $headerrow) {
push(@htmlhead, $html);
} else {
$stillinhtmlhead = 0;
push(@htmlbody, $html);
}
}
}
if ($tableOpts->{LaYoUt}) {
$htmlout = join("\n", @htmlbody);
} else {
my $htmlvalign = '';
$htmlvalign = $tableOpts->{valign}
unless ($tableOpts->{valign} eq 'middle');
$htmlout = tag(join("\n", @htmlbody), 'tbody', { style => css('vertical-align', $htmlvalign) });
if (@htmlhead) {
my $htmlheadcss = css('vertical-align', $htmlvalign);
$htmlheadcss .= css('border-bottom', 'solid 2px') if $tableOpts->{headerrules};
$htmlout = prefix($htmlout, tag(join("\n", @htmlhead), 'thead', { style => $htmlheadcss }));
}
}
}
return main::MODES(
TeX => join(" ", @rows),
HTML => $htmlout,
PTX => join("\n", @rows),
);
}
sub Row {
my ($rowArray, $tableOpts, $alignment) = @_;
my $headerrow = '';
my $valign = '';
for my $x (@$rowArray) {
$headerrow = 'yes' if ($x->{headerrow});
$valign = $x->{valign} if ($x->{valign});
}
my @cells;
# Loops over the cells in the row
for my $i (0 .. $#$rowArray) {
my $cellOpts = $rowArray->[$i];
my $cellAlign = $alignment->[ $rowArray->[$i]{leftcol} ];
my $cellData = $cellOpts->{data};
my $cell = $cellData;
if ($main::displayMode eq 'TeX') {
$cell = prefix($cell, $cellOpts->{tex}, ' ');
$cell = wrap($cell, @{ $tableOpts->{encase} })
unless $cellOpts->{noencase};
$cell = wrap($cell, $cellOpts->{texpre}, $cellOpts->{texpost});
$cell = prefix($cell, '\bfseries', ' ')
if ($tableOpts->{rowheaders} && $cellOpts->{header} ne 'td' && $i == 0
|| ($headerrow && $cellOpts->{header} ne 'td')
|| $cellOpts->{header} =~ /^(th|rh|ch|col|column|row)$/i);
# Situations where we need \multicolumn
if ($cellOpts->{colspan} > 1
|| $cellOpts->{halign}
|| $valign
|| ($tableOpts->{valign} && $tableOpts->{valign} ne 'top')
|| ($tableOpts->{rowheaders} && $tableOpts->{headerrules} && $i == 0))
{
my $columntype = $cellOpts->{halign};
$columntype = $cellAlign->{halign} // 'l' unless $columntype;
$columntype = 'p{' . $tableOpts->{Xratio} / ($#$rowArray + 1) . "\\linewidth}"
if ($columntype eq 'X');
$columntype = "p{$cellAlign->{width}}"
if ($cellAlign->{width});
$columntype =~ s/^p/m/ if ($valign eq 'middle');
$columntype =~ s/^p/b/ if ($valign eq 'bottom');
$columntype =~ s/^p/m/ if ($tableOpts->{valign} eq 'middle');
$columntype =~ s/^p/b/ if ($tableOpts->{valign} eq 'bottom');
$columntype = ">{$cellAlign->{tex}}" . $columntype if $cellAlign->{tex};
$columntype = getLaTeXcolumnWidth($alignment->[0]{left}) . $columntype
if ($i == 0 && $alignment->[0]{left} && !$cellOpts->{halign});
if ($i == 0 && $cellOpts->{colspan} == 1 && $tableOpts->{rowheaders} && $tableOpts->{headerrules}) {
$columntype .= '|';
} elsif (!$cellOpts->{halign}) {
$columntype .= getLaTeXcolumnWidth($cellAlign->{right});
}
$cell = latexCommand('multicolumn', [ $cellOpts->{colspan}, $columntype, $cell ]);
}
$cell = suffix($cell, '&', ' ') unless ($i == $#$rowArray);
push(@cells, $cell);
} elsif ($main::displayMode eq 'PTX') {
$cell = wrap($cell, @{ $tableOpts->{encase} })
unless $cellOpts->{noencase};
$cell = tag($cell, 'p')
if (($cellAlign->{width} || $cellAlign->{halign} eq 'X' || $cellOpts->{halign} =~ /^p/))
&& !$tableOpts->{LaYoUt};
my $ptxhalign = '';
$ptxhalign = 'left' if ($cellOpts->{halign} =~ /l/);
$ptxhalign = 'right' if ($cellOpts->{halign} =~ /r/);
my $ptxright = '';
$ptxright = 'minor' if ($cellOpts->{halign} =~ /\|\s*$/);
$ptxright = 'medium' if ($cellOpts->{halign} =~ /\|\s*\|\s*$/);
$ptxright = 'major' if ($cellOpts->{halign} =~ /\|\s*\|\s*\|\s*$/);
my $ptxbottom = '';
$ptxbottom .= getPTXthickness($cellOpts->{bottom});
if ($cellOpts->{halign} =~ /!\{\s*\\vrule\s+width\s+([^}]*?)\s*}\s*$/) {
$ptxright = 'minor' if ($1);
$ptxright = 'minor' if ($1 eq '0.04em');
$ptxright = 'medium' if ($1 eq '0.07em');
$ptxright = 'major' if ($1 eq '0.11em');
}
if ($tableOpts->{LaYoUt}) {
$cell = tag($cell, 'p');
$cell = tag($cell, 'stack',);
} else {
$cell = tag(
$cell, 'cell',
{
halign => $ptxhalign,
colspan => ($cellOpts->{colspan} > 1) ? $cellOpts->{colspan} : '',
right => $ptxright,
bottom => $ptxbottom
},
''
);
}