AppleScript C Perl Shell Xcode Other

Optimize CSS file

Post Reply
coding / perl     Views: 178Prev .. Next
Optimize CSS filePosted: Friday, November 25, 2022 [00:29:10] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 357
This is a simple Perl script to optimize/minimize CSS files for the Web
Optimize CSS file
View Code#!/usr/bin/perl
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>;$css =~ s/\n|\r//g;$css =~ s/\s+$//;
unless(-f "$css") {print "Error - CSS file:\n\"$css\" - not found\n";exit(0);}
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;$nfl =~ s/(\.\w{3,4})$/\.cut$&/;$nfl =~ s/\s/\./g;
open(CSS,">$nfl") or die "cannot open $nfl : $!\n";print CSS $d;close(CSS);
print "Finished building: $nfl\n";
print "Do you want to save it with original file name [y/n]: ";
my $changeFileName=<STDIN>;$changeFileName =~ s/\W//g;

if($changeFileName =~ m/y/i) {
`mv $nfl $css`;
print "File renamed to $css\n";
}
print " \* \* Finished \* \*\n";exit(0);

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;$d =~ s#/\*(.*?)\*/##gs;return $d;}

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;$d =~ s/ \n/\n/g;$d = cunlsp($d);return $d;}

sub docutnlsp {my $d = shift;$d =~ s/\n /\n/g;$d = cunlsp($d);return $d;}

sub cuttab {
my $d = shift;
if($d =~ m/\t/) {$d = docuttab($d);}
return $d;
}

sub docuttab {my $d = shift;$d =~ s/\t//g;$d = cuttab($d);return $d;}

sub cutnl {
my $d = shift;
if($d =~ m/\n\n/) {$d = docutnl($d);}
return $d;
}

sub docutnl {my $d = shift;$d =~ tr/\n\n//s;$d = cutnl($d);return $d;}



Program is local, fast and easy to use:
View Codeperl optimize.css.cgi

just adjust a shebang to your perl location or remove it altogether if used in TerminalThere's no place like ~
coding / perlPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group