Optimize CSS file
Post ReplyOptimize CSS filePosted: Friday, November 25, 2022 [00:29:10] - 1
This is a simple Perl script to optimize/minimize CSS files for the Web use strict; # Change above shebang if needed according to your Perl setup # Program can't handle a whitespace - please make sure path to # CSS file without whitespaces print "Choose CSS file: "; my $css = <STDIN>; unless(-f "$css") {print "Error - CSS file:\n\"$css\" - not found\n"; my $d = `cat "$css"`; $d = cutnotes($d); $d = cutnl($d); $d = cuttab($d); $d = cunlsp($d); $d = cutmnl($d); $d =~ s/^\n//; $d =~ s/\n//g; $d =~ s#(\;)(\})#$2#g; $d =~ s/(\w)( )(\{)/$1$3/g; $d =~ s/(\:)( )(\w|\W)/$1$3/g; $d =~ s#\;\* \{#\;\*\{#; my $nfl=$css; open(CSS,">$nfl") or die "cannot open $nfl : $!\n"; print "Finished building: $nfl\n"; print "Do you want to save it with original file name [y/n]: "; my $changeFileName=<STDIN>; if($changeFileName =~ m/y/i) { `mv $nfl $css`; print "File renamed to $css\n"; } print " \* \* Finished \* \*\n"; sub cutmnl { $d =~ s#(\{)(\n|\r)#$1#g; $d =~ s#(\;)(\s|\s+)#$1#gs; $d =~ s#(\;)(\n|\r)(\w|\.|\#)#$1$3#gs; return $d; } sub cutnotes {my $d = shift; sub cunlsp { my $d = shift; if($d =~ m/\n /) {$d = docutnlsp($d);} if($d =~ m/ \n/) {$d = dospnlcut($d);} return $d; } sub dospnlcut {my $d = shift; sub docutnlsp {my $d = shift; sub cuttab { my $d = shift; if($d =~ m/\t/) {$d = docuttab($d);} return $d; } sub docuttab {my $d = shift; sub cutnl { my $d = shift; if($d =~ m/\n\n/) {$d = docutnl($d);} return $d; } sub docutnl {my $d = shift; Program is local, fast and easy to use: just adjust a shebang to your perl location or remove it altogether if used in Terminal |