-
Notifications
You must be signed in to change notification settings - Fork 3
/
.minify.pl
executable file
·43 lines (40 loc) · 1.34 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
#!/usr/bin/env perl
# vim: fdm=marker :
use v5.14; use warnings; use autodie; use utf8;
use open qw( :encoding(UTF-8) :std );
binmode STDIN, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
sub slurp { local $/; open my $f, '<', shift; return scalar <>; }
sub slurp_stdin() { local $/; return scalar <> }
sub minify_html { my $t = shift;
## remove comments
$t =~ s|<!--.*?-->||gs;
## collapse spaces
$t =~ s|\s+| |g;
## remove horizontal spaces around punctuation
$t =~ s| <|<|g; # note: this changes the behavior of the html; I'm relying on that
$t =~ s|&newline;|\n|g;
$t =~ s/(<(?:script|style)>) /$1/g;
$t =~ s/ (<\/(?:script|style)>)/$1/g;
## old:
# $t =~ s|(?<=\W) (?=\W)||g;
# $t =~ s|(?<=[^\w"]) (?=\w)||g; # don't remove the space between html attributes
# $t =~ s| (?=\W)||g;
## remove leading and trailing spaces
$t =~ s|\A\s+||g;
$t =~ s|\s+\Z||g;
## for debugging
# $t =~ s| <|█<|g;
# $t =~ s|> |>█|g;
## unquote attribute values if allowable (I use only double-quotes in html)
## https://html.spec.whatwg.org/multipage/syntax.html#unquoted
$t =~ s|(\b\w+)="([^\s"'`<>=]+)"|$1=$2|g;
$t =~ s|(\b\w+)=""|$1|g;
return $t;
}
if (defined $ARGV[0] && -f $ARGV[0]) {
print minify_html(slurp($ARGV[0]))
}
else {
print minify_html(slurp_stdin)
}