Acme-AsciiArt2HtmlTable
view release on metacpan or search on metacpan
lib/Acme/AsciiArt2HtmlTable.pm view on Meta::CPAN
# randomizing new colors
if ( $config{'randomize-new-colors'} ) {
if ( not defined $config{'colors'}{ $lines[$line]->[$cell] } ) {
$config{'colors'}{ $lines[$line]->[$cell] } = _random_color();
}
}
# optimization
my $optimization = '';
# debugging messages were kept for future reference
# remember that lines and cells are not the exact values, as
# arrays start at index 0 and both lines and cells start at
# position 1
#my $debug = "line $line, cell $cell, ";
if ( $config{'optimization'} ) {
#$debug .= "\nthis is line $line, cell $cell";
# check how many cells we could have on each line from the line we're
# in to the last one
my %we_could_have;
for ( $line .. $#lines ) {
$we_could_have{$_} = _count_in_the_beginning(
$lines[$line]->[$cell],
@{$lines[$_]}[$cell .. $#{$lines[$_]}]
);
#$debug .= "\nwe could have $we_could_have{$_} on line $_";
}
# check, for each line, how many cells an area up to that line would have
my %area;
my %area_width;
for ( $line .. $#lines ) {
my $min = _min( @we_could_have{$line .. $_} );
$area{$_} = (1 + $_ - $line) * $min;
$area_width{$_} = $min;
#$debug .="\nwe could make an area of $area{$_} up to line $_, with a maximum of $area_width{$_} cells per line";
}
# check which is the line that maximizes optimization
my $max_area = _max(values %area);
my $best_line = _max(grep { $area{$_} == $max_area } keys %area);
#$debug .= "\nour best choice seem to be using line $best_line";
# check the are width
my $width = $cell + $area_width{$best_line} - 1;
# clean everything in the area we're about to optimize
#$debug .= "\nwe want to clean everything from lines $line to $best_line and cells $cell to $width";
for my $l ( $line .. $best_line ) {
for my $c ( $cell .. $width ) {
next if ( $l == $line and $c == $cell );
$lines[$l]->[$c] = '';
}
}
# optimize
my $rowspan = $best_line - $line + 1;
my $colspan = $area_width{$best_line};
if ( $rowspan > 1 ) { $optimization .= " rowspan=\"$rowspan\"" }
if ( $colspan > 1 ) { $optimization .= " colspan=\"$colspan\"" }
#$debug .= "\n";
}
$lines[$line]->[$cell] = "<td$optimization bgcolor=\"" .
( $config{'colors'}{ $lines[$line]->[$cell] } ||
$config{'colors'}{'default'} ) .
"\"></td>";
}
$lines[$line] = join "\n", grep /./, @{$lines[$line]};
( run in 1.261 second using v1.01-cache-2.11-cpan-49f99fa48dc )