#!/usr/bin/perl # # stupid thing to try and compress the output of # gif2html (http://www.illuminated.co.uk/gif2html/) # by making more use of stylesheets for colouring # it seemed like a good idea at the time. # # howie@thingy.com http://wotsit.thingy.com/haj/ # 23rd January 2001 use Text::Wrap qw(wrap $columns $huge); $columns = 132; $source = shift; # First pass through, we'll collect up the colour stats. # This might take a lot of memory! # I'm going to assume that the input will have one bgcolor per line, # because that's how it is right now, and I'm lazy. # lines contain: bgcolor="#ffffff" open(INPUT,$source) || die("Failed to open input file: $!"); while() { if (m/bgcolor="#(\w\w\w\w\w\w)"/) { $colourcounts{$1}++; $lines++; } } close(INPUT); # find the most popular colours @mostcolours = sort {$colourcounts{$b} <=> $colourcounts{$a}} keys %colourcounts; print "$lines lines checked.\n"; $totalsavings = 0; $compressed=0; $i=0; $style_head=""; foreach $colour (@mostcolours) { $frequency = $colourcounts{$colour}; $classname = sprintf("s%x",$i); $stylesheet_replacement = sprintf("TD.%s{background:#%s;}\n",$classname,$colour); $body_replacement = 'class="'.$classname.'"'; $current_cost = length('bgcolor="#ffffff"') * $frequency; $new_cost = length($stylesheet_replacement) + length($body_replacement); $saving = $current_cost - $new_cost; if($saving > 0) { $totalsavings+=$saving; $replace{$colour}=$body_replacement; $style_head .= $stylesheet_replacement; $compressed++; } print "$frequency: $colour: $saving\n"; $i++; } print "saved $totalsavings with stylesheet, in total!\n"; print "$i colours in use, $compressed colours were packed\n"; $output = ""; # now we implement our decisions open(INPUT,$source) || die("Failed to open input file: $!"); while() { chomp;chomp; if(m/-->/) { $output .= "\n$style_head"; } if (m/bgcolor="#(\w\w\w\w\w\w)"/) { $colour=$1; if($replace{$colour} ne "") { s/bgcolor="#$colour"/$replace{$colour}/; } } $output .= $_; } close(INPUT); open(OUTPUT,">".$source.".new") || die("Failed to create output file: $!"); print OUTPUT wrap("","",$output); close(OUTPUT);