1 # Copyright 2002-2007 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: table_organize.tag,v 1.11 2007-11-05 20:15:27 docelic Exp $
10 UserTag table-organize Order cols
11 UserTag table-organize attrAlias columns cols
12 UserTag table-organize Interpolate
13 UserTag table-organize addAttr
14 UserTag table-organize hasEndTag
15 UserTag table-organize Version $Revision: 1.11 $
16 UserTag table-organize Routine <<EOR
18 my ($cols, $opt, $body) = @_;
19 $cols = int($cols) || 2;
20 $body =~ s/(.*?)(<td)\b/$2/is
23 $body =~ s:(</td>)(?!.*</td>)(.*):$1:is;
27 if($opt->{cells} and ref($opt->{cells}) eq 'ARRAY') {
28 @cells = @{$opt->{cells}};
30 elsif($opt->{embed}) {
31 if($opt->{embed} eq 'lc') {
32 push @cells, $1 while $body =~ s:(<td\b.*?</td>)::s;
35 push @cells, $1 while $body =~ s:(<TD\b.*?</TD>)::s;
39 push @cells, $1 while $body =~ s:(<td\b.*?</td>)::is;
42 while ($opt->{min_rows} and ($opt->{min_rows} * ($cols - 1)) > scalar(@cells) ) {
47 if(int($opt->{limit}) and $opt->{limit} < scalar(@cells) ) {
48 splice(@cells, $opt->{limit});
52 $opt->{$_} = defined $opt->{$_} ? " $opt->{$_}" : '';
60 elsif (ref $opt->{td} ) {
62 push @td, '' while scalar(@td) < $cols;
65 @td = (" $opt->{td}") x $cols;
69 for(qw/caption tr pre post/) {
73 elsif (ref $opt->{$_}) {
74 $attr{$_} = $opt->{$_};
77 $attr{$_} = [$opt->{$_}];
81 my $pretty = $opt->{pretty};
83 #$opt->{td} =~ s/^(\S)/ $1/;
84 #$opt->{tr} =~ s/^(\S)/ $1/;
93 $opt->{filler} = ' ' if ! defined $opt->{filler};
98 $td_beg = qq{<FONT $opt->{font}>};
99 $td_end = qq{</FONT>};
102 if($rows = int($opt->{rows}) ) {
103 $total_mod = $rows * $cols;
104 @rest = splice(@cells, $total_mod)
105 if $total_mod < @cells;
106 $opt->{table} = ' ' if ! $opt->{table};
109 my $joiner = $opt->{joiner} || ($pretty ? "\n\t\t" : "");
111 if ($opt->{columnize}) {
112 my $cell_count = scalar @cells;
113 my $row_count_ceil = POSIX::ceil($cell_count / $cols);
114 my $row_count_floor = int($cell_count / $cols);
115 my $remainder = $cell_count % $cols;
116 my @tmp = splice(@cells, 0);
118 for (my $r = 0; $r < $row_count_ceil; $r++) {
119 for (my $c = 0; $c < $cols; $c++) {
120 if ($c >= $remainder + 1) {
121 $index = $r + $row_count_floor * $c + $remainder;
124 $index = $r + $row_count_ceil * $c;
126 push @cells, $tmp[$index];
127 last if $r + 1 == $row_count_ceil and $c + 1 == $remainder;
132 while (scalar(@cells) % $cols) {
133 push @cells, "<td>$opt->{filler}</td>";
136 #$out .= "<!-- starting table tmod=$tmod -->";
138 $out .= "<table$opt->{table}>";
139 $out .= "\n" if $pretty;
140 if($opt->{caption}) {
141 my $idx = $tmod % scalar(@{$attr{caption}});
142 #$out .= "<!-- caption index $idx -->";
143 $out .= "\n" if $pretty;
144 $out .= "<caption>" . $attr{caption}[$idx] . "</caption>";
145 $out .= "\n" if $pretty;
150 $out .= "\t" if $pretty;
153 my $idx = $rmod % scalar(@{$attr{tr}});
154 $out .= " " . $attr{tr}[$idx];
157 $out .= "\n\t\t" if $pretty;
158 my @op = splice (@cells, 0, $cols);
160 for ( my $i = 0; $i < $cols; $i++) {
161 $op[$i] =~ s/(<td)/$1 $td[$i]/i;
164 @op = map { s/>/>$td_beg/; $_ } @op if $td_beg;
165 @op = map { s/(<[^<]+)$/$td_end$1/; $_ } @op if $td_end;
167 $out .= join($joiner, @op);
168 $out .= "\n\t" if $pretty;
170 $out .= "\n" if $pretty;
175 $out .= "\n" if $pretty;
178 my $num = $total_mod < scalar(@rest) ? $total_mod : scalar(@rest);
179 @cells = splice(@rest, 0, $num);
183 return $out . $postamble;