| 1 | package Template::Plugin::Filter::MinifyHTML; | ||
| 2 | |||
| 3 | our $VERSION = '0.02'; | ||
| 4 | |||
| 5 | use Template::Plugin::Filter; | ||
| 6 | use base qw( Template::Plugin::Filter ); | ||
| 7 | |||
| 8 | sub init { | ||
| 9 | my $self = shift; | ||
| 10 | $self->install_filter('minify_html'); | ||
| 11 | $self->{_cfg} = shift; | ||
| 12 | return $self; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub filter { | ||
| 16 | my ($self, $text) = @_; | ||
| 17 | |||
| 18 | for ($text) { | ||
| 19 | s/^\s+//; # Leading Whitespace | ||
| 20 | s/\s+$//; # Trailing Whitespace | ||
| 21 | s/[ \r\n\t]+/ /g; # More than one space | ||
| 22 | s/>\s+</></g; # Fix > < | ||
| 23 | |||
| 24 | s/\s*\/>/>/g # Remove trailing / on self-closing elements | ||
| 25 | if ($self->{_cfg}->{html5}); | ||
| 26 | |||
| 27 | s/<!--[^[].*?[^]]-->//g # Remove HTML comments (leaving IE conditional comments alone (hopefully)) | ||
| 28 | if ($self->{_cfg}->{comments}); | ||
| 29 | } | ||
| 30 | |||
| 31 | return $text; | ||
| 32 | } | ||
| 33 | |||
| 34 | 1; | ||
| 35 | |||
| 36 | __END__ |