-
Notifications
You must be signed in to change notification settings - Fork 0
/
minify.pl
113 lines (96 loc) · 2.54 KB
/
minify.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use File::Copy qw(move);
use strict;
use warnings;
no warnings 'qw';
my $USAGE = "<infile>[outdir]";
my $HELP = "Help: Minifies source by removing Whitespace
usage\n
usage: $USAGE\n
infile: mandatory path to infile
[outfile]: optional path to outfile, otherwise print to stdout\n";
die "$HELP" if $ARGV[0] eq '-h';
die "$HELP" if $ARGV[0] eq '--help';
sub usage{ die "usage ($0): $USAGE\n" }
my $infile = $ARGV[0];
my $outfile = $ARGV[1];
usage unless $infile;
die "Err: infile $infile not exist" unless -f $infile;
if($outfile){
die "Err: outfile $outfile already exist" if -f $outfile;
}
open( my $ifh, '<', $infile ) || die "Err: could not open infile $infile";
my $instr = undef;
my ( @out, @line, @word );
my $lineout;
$lineout = sub {
if (@word) {
push @line, join( '', @word, @_ );
@word = ();
} else {
push @line, join( '', @_ );
}
};
while (<$ifh>) {
if (/^\s*\#/) {
push @out, $_;
} elsif (/^\s*$/) {
push @out, $_;
} else {
my ( $prev, $leadspace ) = ( undef, 1 );
foreach ( split( '', $_ ) ) {
if ($instr) {
if (/\"/) {
$lineout->($_);
undef $instr;
undef $leadspace;
} else {
push @word, $_;
}
} else {
if (/\n/) {
$lineout->($_);
} elsif (/\,/) {
$lineout->();
push @word, $_;
undef $leadspace;
} elsif (/\s/) {
my $w = join( '', @word );
@word = ();
if ( grep { 1 if ( $_ eq $w ); } qw|, + - = [ ] ( )| ) {
if(@line) {if ( $line[-1] eq ' ' ) { pop @line }}
#die fffff => 'xx ' . $w . 'yyy' ;
push @line, $w;
} else {
push @line, $w;
if ($leadspace) {
} elsif ( $prev eq ' ' ) {
} else {
push @line, ' ';
}
}
} elsif (/\"/) {
$instr = 1;
$lineout->($_);
undef $leadspace;
} else {
undef $leadspace;
push @word, $_;
}
}
$prev = $_;
}
$lineout->();
push @out, join( '', @line );
@line = ();
}
}
my $o = join( "", @out );
close $ifh;
if($outfile){
open( my $ofh, '>', $outfile ) || die "Err: could not open outfile $outfile";
print $ofh $o;
close $ofh;
}else{
print $o;
}
@out = ();