CSS manipulation

Rick Measham rickm at isite.net.au
Tue Oct 29 03:52:24 CST 2002


>CSS in some browsers is not very well supported, especially the inheritance from multiple style sheets. There is also the consideration that a single file is faster to load.

The above should be carefully considered. However if you are in a school computer lab, you'll know which browser is being used.

That being said, here's a working solution using CSS.pm

#!/usr/bin/perl
use CSS;
use Strict;

#--------------------------------------------------------------------------------
# USAGE:
#   simplify(list_of_files);
# NOTE:
#   list_of_files should be in _increasing_ importance. Styles from the last
#   file will overwrite any duplicate styles of all previous files.
# RETURNS:
#   Simplified formatted CSS
#--------------------------------------------------------------------------------
# NOTE CAREFULLY:
#   CSS.pm is very strict in its parsing. Make sure your style sheets use a
#   semi-colon after every parameter, even at the end of a block:
#   body {
#      font-family: 'times';
#      font-size: '12pt';  <--- this semicolon MUST exist
#   }                           even though its at the end of the block
#                               This ain't perl buster!
#--------------------------------------------------------------------------------
# Author's jottings: This is probably not the most 'golf' way to do this, but
# its a working solution.
#--------------------------------------------------------------------------------

print simplify('pmdefault.css','pmuser.css');


#--------------------------------------------------------------------------------
# The routine
#--------------------------------------------------------------------------------

sub simplify {
	my ($output, %usedby, %css);
	my @files = @_;
	die("You have to tell me at least one css file to simplify.") unless @files;
	my $stylesheet = CSS->new(-source=>$files[0]);
	shift @files;
	foreach my $file (@files) {
		$stylesheet->add_file($file);
	}
	
	my @styles = $stylesheet->styles;

	foreach my $style (@styles) {
		my %props = $style->properties;
		foreach my $property (keys %props) {
			$props{$property} = "'$props{$property}'" if $props{$property}=~/\D/;
			push (@{$usedby{sprintf("%19s : %s",$property,$props{$property})}}, $style->selector); #Can I memoize sprintf?
		}
	}

	foreach my $style (sort keys %usedby) {
		my $stylename = join(', ',sort @{$usedby{$style}});
		push(@{$css{$stylename}},$style);
	}


	foreach my $style (sort keys %css) {
		$output .= "$style {\n";
		$output .= join(";\n",@{$css{$style}});
		$output .= ";\n}\n\n";
	}
	return $output;
}
-- 
--------------------------------------------------------
            There are 10 kinds of people:
  those that understand binary, and those that don't.
--------------------------------------------------------
  The day Microsoft makes something that doesn't suck 
    is the day they start selling vacuum cleaners
--------------------------------------------------------



More information about the Melbourne-pm mailing list