Skip to content
This repository
tag: REL_5_0_2
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 340 lines (275 sloc) 7.983 kb
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
UserTag table-organize Order cols
UserTag table-organize attrAlias columns cols
UserTag table-organize Interpolate
UserTag table-organize addAttr
UserTag table-organize hasEndTag
UserTag table-organize Documentation <<EOD

=head1 table-organize

[table-organize <options>]
[loop ....] <td> [loop-tags] </td> [/loop]
[/table-organize]

Takes an unorganized set of table cells and organizes them into
rows based on the number of columns; it will also break them into
separate tables.

If the number of cells are not on an even modulus of the number of columns,
then "filler" cells are pushed on.

Parameters:

=over 4

=item cols (or columns)

Number of columns. This argument defaults to 2 if not present.

=item rows

Optional number of rows. Implies "table" parameter.

=item table

If present, will cause a surrounding <TABLE > </TABLE> pair with the attributes
specified in this option.

=item caption

Table <CAPTION> container text, if any. Can be an array.

=item td

Attributes for table cells. Can be an array.

=item tr

Attributes for table rows. Can be an array.

=item columnize

Will display cells in (newspaper) column order, i.e. rotated.

=item pretty

Adds newline and tab characters to provide some reasonable indenting.

=item filler

Contents to place in empty cells put on as filler. Defaults to C<&nbsp;>.

=item min_rows

On small result sets, can be ugly to build more than necessary columns.
This will guarantee a minimum number of rows -- columns will change
as numbers change. Formula: $num_cells % $opt->{min_rows}.

=item limit

Maximum number of cells to use. Truncates extra cells silently.

=item embed

If you want to embed other tables inside, make sure they are called with
lower case <td> elements, then set the embed tag and make the cells you wish
to organize be <TD> elements. To switch that sense, and make the upper-case
or mixed case be the ignored cells, set the embed parameter to C<lc>.

    [table-organize embed=lc]
<td>
<TABLE>
<TR>
<TD> something
</TD>
</TR>
</table>
</td>
    [/table-organize

or

    [table-organize embed=uc]
<TD>
<table>
<tr>
<td> something
</td>
</tr>
</table>
</TD>
[/table-organize]

=back

The C<tr>, C<td>, and C<caption> attributes can be specified with indexes;
if they are, then they will alternate according to the modulus.

The C<td> option array size should probably always equal the number of columns;
if it is bigger, then trailing elements are ignored. If it is smaller, no attribute
is used.

For example, to produce a table that 1) alternates rows with background
colors C<#EEEEEE> and C<#FFFFFF>, and 2) aligns the columns RIGHT CENTER
LEFT, do:

        [table-organize
            cols=3
            pretty=1
            tr.0='bgcolor="#EEEEEE"'
            tr.1='bgcolor="#FFFFFF"'
            td.0='align=right'
            td.1='align=center'
            td.2='align=left'
            ]
            [loop list="1 2 3 1a 2a 3a 1b"] <td> [loop-code] </td> [/loop]
        [/table-organize]

which will produce:

        <tr bgcolor="#EEEEEE">
                <td align=right>1</td>
                <td align=center>2</td>
                <td align=left>3</td>
        </tr>
        <tr bgcolor="#FFFFFF">
                <td align=right>1a</td>
                <td align=center>2a</td>
                <td align=left>3a</td>
        </tr>
        <tr bgcolor="#EEEEEE">
                <td align=right>1b</td>
                <td align=center>&nbsp;</td>
                <td align=left>&nbsp;</td>
        </tr>

If the attribute columnize=1 is present, the result will look like:

        <tr bgcolor="#EEEEEE">
                <td align=right>1</td>
                <td align=center>1a</td>
                <td align=left>1b</td>
        </tr>
        <tr bgcolor="#FFFFFF">
                <td align=right>2</td>
                <td align=center>2a</td>
                <td align=left>&nbsp;</td>
        </tr>
        <tr bgcolor="#EEEEEE">
                <td align=right>3</td>
                <td align=center>3a</td>
                <td align=left>&nbsp;</td>
        </tr>

See the source for more ideas on how to extend this tag.

=cut

EOD
UserTag table-organize Routine <<EOR
sub {
my ($cols, $opt, $body) = @_;
$cols = int($cols) || 2;
$body =~ s/(.*?)(<td)\b/$2/is
or return;
my $out = $1;
$body =~ s:(</td>)(?!.*</td>)(.*):$1:is;
my $postamble = $2;

my @cells;
if($opt->{cells} and ref($opt->{cells}) eq 'ARRAY') {
@cells = @{$opt->{cells}};
}
elsif($opt->{embed}) {
if($opt->{embed} eq 'lc') {
push @cells, $1 while $body =~ s:(<td\b.*?</td>)::s;
}
else {
push @cells, $1 while $body =~ s:(<TD\b.*?</TD>)::s;
}
}
else {
push @cells, $1 while $body =~ s:(<td\b.*?</td>)::is;
}

while ($opt->{min_rows} and ($opt->{min_rows} * ($cols - 1)) > scalar(@cells) ) {
$cols--;
last if $cols == 1;
}

if(int($opt->{limit}) and $opt->{limit} < scalar(@cells) ) {
splice(@cells, $opt->{limit});
}

for(qw/ table/) {
$opt->{$_} = defined $opt->{$_} ? " $opt->{$_}" : '';
}

my @td;

if(! $opt->{td}) {
@td = '' x $cols;
}
elsif (ref $opt->{td} ) {
@td = @{$opt->{td}};
push @td, '' while scalar(@td) < $cols;
}
else {
@td = (" $opt->{td}") x $cols;
}

my %attr;
for(qw/caption tr pre post/) {
if( ! $opt->{$_} ) {
#do nothing
}
elsif (ref $opt->{$_}) {
$attr{$_} = $opt->{$_};
}
else {
$attr{$_} = [$opt->{$_}];
}
}

my $pretty = $opt->{pretty};

#$opt->{td} =~ s/^(\S)/ $1/;
#$opt->{tr} =~ s/^(\S)/ $1/;

my @rest;
my $rows;

my $rmod;
my $tmod = 0;
my $total_mod;

$opt->{filler} = '&nbsp;' if ! defined $opt->{filler};

my $td_beg;
my $td_end;
if($opt->{font}) {
$td_beg = qq{<FONT $opt->{font}>};
$td_end = qq{</FONT>};
}

if($rows = int($opt->{rows}) ) {
$total_mod = $rows * $cols;
@rest = splice(@cells, $total_mod)
if $total_mod < @cells;
$opt->{table} = ' ' if ! $opt->{table};
}

my $joiner = $opt->{joiner} || ($pretty ? "\n\t\t" : "");
while(@cells) {
if ($opt->{columnize}) {
my $cell_count = scalar @cells;
my $row_count_ceil = POSIX::ceil($cell_count / $cols);
my $row_count_floor = int($cell_count / $cols);
my $remainder = $cell_count % $cols;
my @tmp = splice(@cells, 0);
my $index;
for (my $r = 0; $r < $row_count_ceil; $r++) {
for (my $c = 0; $c < $cols; $c++) {
if ($c >= $remainder + 1) {
$index = $r + $row_count_floor * $c + $remainder;
}
else {
$index = $r + $row_count_ceil * $c;
}
push @cells, $tmp[$index];
last if $r + 1 == $row_count_ceil and $c + 1 == $remainder;
}
}
}

while (scalar(@cells) % $cols) {
push @cells, "<td>$opt->{filler}</td>";
}

#$out .= "<!-- starting table tmod=$tmod -->";
if($opt->{table}) {
$out .= "<table$opt->{table}>";
$out .= "\n" if $pretty;
if($opt->{caption}) {
my $idx = $tmod % scalar(@{$attr{caption}});
#$out .= "<!-- caption index $idx -->";
$out .= "\n" if $pretty;
$out .= "<CAPTION>" . $attr{caption}[$idx] . "</CAPTION>";
$out .= "\n" if $pretty;
}
}
$rmod = 0;
while(@cells) {
$out .= "\t" if $pretty;
$out .= "<tr";
if($opt->{tr}) {
my $idx = $rmod % scalar(@{$attr{tr}});
$out .= " " . $attr{tr}[$idx];
}
$out .= ">";
$out .= "\n\t\t" if $pretty;
my @op = splice (@cells, 0, $cols);
if($opt->{td}) {
for ( my $i = 0; $i < $cols; $i++) {
$op[$i] =~ s/(<td)/$1 $td[$i]/i;
}
}
@op = map { s/>/>$td_beg/; $_ } @op if $td_beg;
@op = map { s/(<[^<]+)$/$td_end$1/; $_ } @op if $td_end;

$out .= join($joiner, @op);
$out .= "\n\t" if $pretty;
$out .= "</tr>";
$out .= "\n" if $pretty;
$rmod++;
}
if($opt->{table}) {
$out .= "</table>";
$out .= "\n" if $pretty;
}
if(@rest) {
my $num = $total_mod < scalar(@rest) ? $total_mod : scalar(@rest);
@cells = splice(@rest, 0, $num);
}
$tmod++;
}
return $out . $postamble;
}
EOR

Something went wrong with that request. Please try again.