P { align: left; font-family: sans; font-size: 12pt; } #### TD { text-align: left; font-family: sans; font-size: 12pt; } P { text-align: left; width: 80%; } #### P, TD { text-align: left; font-family: sans; font-size: 12pt; } P { width: 80%; } #### #!/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); } } 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; }