line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Plack middleware to minify HTML on-the-fly |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Plack::Middleware::HTMLMinify; |
4
|
|
|
|
|
|
|
BEGIN { |
5
|
3
|
|
|
3
|
|
3049
|
$Plack::Middleware::HTMLMinify::VERSION = '1.0.0'; |
6
|
|
|
|
|
|
|
} |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
25
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
92
|
|
9
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
121
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Plack::Middleware::HTMLMinify - Plack middleware for HTML minify |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
version 1.0.0 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 DESCRIPTION |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
This module will use L to minify HTML code on-the-fly |
22
|
|
|
|
|
|
|
automatically. Currently it will check if Content-Type is C. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use Plack::Builder; |
27
|
|
|
|
|
|
|
builder { |
28
|
|
|
|
|
|
|
enable 'HTMLMinify', opt => {remove_newlines => 1}; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
3
|
|
701
|
use parent 'Plack::Middleware'; |
|
3
|
|
|
|
|
268
|
|
|
3
|
|
|
|
|
23
|
|
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
3
|
|
18371
|
use HTML::Packer; |
|
3
|
|
|
|
|
440648
|
|
|
3
|
|
|
|
|
99
|
|
36
|
3
|
|
|
3
|
|
41
|
use Plack::Util; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
79
|
|
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
3
|
|
17
|
use Plack::Util::Accessor qw/opt packer/; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
31
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub call { |
41
|
2
|
|
|
2
|
1
|
63876
|
my ($self, $env) = @_; |
42
|
|
|
|
|
|
|
|
43
|
2
|
|
|
|
|
32
|
my $res = $self->app->($env); |
44
|
|
|
|
|
|
|
Plack::Util::response_cb($res, sub { |
45
|
2
|
|
|
2
|
|
41
|
my $res = shift; |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
16
|
my $h = Plack::Util::headers($res->[1]); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Only process text/html. |
50
|
2
|
|
|
|
|
96
|
my $ct = $h->get('Content-Type'); |
51
|
2
|
50
|
33
|
|
|
179
|
return unless defined $ct and $ct =~ qr{text/html}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Don't touch compressed content. |
54
|
0
|
0
|
|
|
|
0
|
return if defined $h->get('Content-Encoding'); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Concat all content, and if response body is undefined then ignore it. |
57
|
0
|
|
|
|
|
0
|
my $body = []; |
58
|
0
|
|
|
|
|
0
|
Plack::Util::foreach($res->[2], sub { push @$body, $_[0]; }); |
|
0
|
|
|
|
|
0
|
|
59
|
0
|
|
|
|
|
0
|
$body = join '', @$body; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Minify and replace it. |
62
|
0
|
0
|
|
|
|
0
|
$self->packer->minify(\$body, $self->opt) if '' ne $body; |
63
|
0
|
|
|
|
|
0
|
$res->[2] = [$body]; |
64
|
0
|
|
|
|
|
0
|
$h->set('Content-Length', length $body); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
return; |
67
|
2
|
|
|
|
|
62
|
}); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub prepare_app { |
71
|
2
|
|
|
2
|
1
|
417
|
my $self = shift; |
72
|
2
|
|
|
|
|
9
|
my $packer = HTML::Packer->init; |
73
|
2
|
|
|
|
|
5279
|
$self->packer($packer); |
74
|
2
|
50
|
|
|
|
224
|
$self->opt({remove_newlines => 1}) unless defined $self->opt; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 AUTHOR |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Gea-Suan Lin, C<< >> |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Copyright 2011 Gea-Suan Lin. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
This software is released under 3-clause BSD license. See |
86
|
|
|
|
|
|
|
L for more |
87
|
|
|
|
|
|
|
information. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |