| 1 |
#!/usr/local/bin/perl |
| 2 |
# htmlclean |
| 3 |
# Copyright (C) 1998 by ITU |
| 4 |
|
| 5 |
use strict; |
| 6 |
|
| 7 |
sub usage { |
| 8 |
print STDERR <<END; |
| 9 |
usage: $0 file1 [file2 file3 ...] |
| 10 |
END |
| 11 |
exit 1; |
| 12 |
} |
| 13 |
|
| 14 |
sub print_version { |
| 15 |
my($version) = $HTML::Clean::VERSION; |
| 16 |
print "$0\n\tHTML::Clean::VERSION: $version\n\n"; |
| 17 |
exit 0; |
| 18 |
} |
| 19 |
|
| 20 |
=head1 NAME |
| 21 |
|
| 22 |
htmlclean - a small script to clean up existing HTML |
| 23 |
|
| 24 |
=head1 SYNOPSIS |
| 25 |
|
| 26 |
B<htmlclean [-v] [-V] file1 [file2 file3 ...]> |
| 27 |
|
| 28 |
=head1 DESCRIPTION |
| 29 |
|
| 30 |
This program provides a command-line interface to the HTML::Clean |
| 31 |
module, which can help you to provide more compatible, smaller HTML |
| 32 |
files at the expense of reducing the human readability of the HTML |
| 33 |
code. In some cases you may be able to reduce the size of your HTML |
| 34 |
by up to 50%! |
| 35 |
|
| 36 |
The HTML::Clean library provides a number of features that improve your |
| 37 |
HTML for browsing and serving: |
| 38 |
|
| 39 |
B<htmlclean> passes each file given on the command line to the library |
| 40 |
and writes out the new HTML according to the specified options. The |
| 41 |
default is to create a backup file and replace the file with cleaned HTML. |
| 42 |
|
| 43 |
=over 6 |
| 44 |
|
| 45 |
=item Removing whitespace, Comments and other useless or redundant constructs |
| 46 |
|
| 47 |
=item Insuring that font tags work across multiple operating systems |
| 48 |
|
| 49 |
=back |
| 50 |
|
| 51 |
For full details see the documentations for L<HTML::Clean> itself. |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
=head1 OPTIONS |
| 56 |
|
| 57 |
=over 4 |
| 58 |
|
| 59 |
=item C<-V> |
| 60 |
|
| 61 |
Print the version of the program. |
| 62 |
|
| 63 |
=item C<-v> |
| 64 |
|
| 65 |
Verbose mode. Print out the original and final file sizes, plus the |
| 66 |
compression percent. For example: |
| 67 |
|
| 68 |
5261 4065 22% /tmp/development-es.html |
| 69 |
5258 4061 22% /tmp/development-fr.html |
| 70 |
4651 3683 20% /tmp/development.html |
| 71 |
|
| 72 |
=back |
| 73 |
|
| 74 |
=head1 SEE ALSO |
| 75 |
|
| 76 |
For the library, see L<HTML::Clean> |
| 77 |
|
| 78 |
=head1 AUTHOR |
| 79 |
|
| 80 |
C<htmlclean> is written by Paul Lindner, <[email protected]> |
| 81 |
|
| 82 |
=head1 COPYRIGHT |
| 83 |
|
| 84 |
Copyright (c> 1998 by ITU under the same terms as Perl. |
| 85 |
|
| 86 |
=cut |
| 87 |
|
| 88 |
usage() if ($#ARGV == -1); |
| 89 |
usage() if ($#ARGV >=0 && $ARGV[0] eq '-?'); |
| 90 |
|
| 91 |
|
| 92 |
use HTML::Clean; |
| 93 |
use Getopt::Long; |
| 94 |
my (%opts); |
| 95 |
|
| 96 |
$Getopt::Long::getopt_compat = 1; # avoid parsing +'s as options (doesn't work!) |
| 97 |
&Getopt::Long::config(qw(no_ignore_case no_getopt_compat)); |
| 98 |
&GetOptions(\%opts, qw(v V t=s 1 2 3 4 5 6 7 8 9)); |
| 99 |
|
| 100 |
&print_version if ($opts{'V'}); |
| 101 |
&usage if ($#ARGV == -1); # we MUST have at least one file |
| 102 |
|
| 103 |
my($verbose) = $opts{'v'}; |
| 104 |
my $level = 9; |
| 105 |
foreach my $i (1, 2, 3, 4, 5, 6, 7, 8, 9) { |
| 106 |
$level = $i if ($opts{$i}); |
| 107 |
} |
| 108 |
|
| 109 |
&main($level, \@ARGV); |
| 110 |
exit 0; |
| 111 |
|
| 112 |
sub main { |
| 113 |
my($level, $files) = @_; |
| 114 |
|
| 115 |
my $h = new HTML::Clean(); # Just a empty holder.. |
| 116 |
print_error('initializing...') if (!$h); |
| 117 |
$h->level($level); |
| 118 |
|
| 119 |
foreach my $f (@$files) { |
| 120 |
my $result = $h->initialize($f); |
| 121 |
print_error($f) if ($result == 0); |
| 122 |
|
| 123 |
my $d = $h->data(); |
| 124 |
my $origlen = length($$d); |
| 125 |
|
| 126 |
# add options to control these... |
| 127 |
$h->compat(); |
| 128 |
$h->strip(); |
| 129 |
|
| 130 |
my $newlen = length($$d); |
| 131 |
my $pct = 0; |
| 132 |
if ($origlen > 0) { |
| 133 |
$pct = (100 * ($origlen - $newlen)) / $origlen; |
| 134 |
} |
| 135 |
printf "%6d %6d %2d%% %s\n", $origlen, $newlen, $pct, $f if ($verbose); |
| 136 |
|
| 137 |
# Okay, now move the files around.. |
| 138 |
rename($f, "$f.bak") || die "Cannot rename '$f': $!\n"; |
| 139 |
open(output, ">$f") || die "Cannot overwrite '$f': $!\n"; |
| 140 |
print output $$d; |
| 141 |
close(output); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
sub print_error { |
| 147 |
my($msg) = @_; |
| 148 |
print STDERR <<END; |
| 149 |
$0: $msg ($!) |
| 150 |
END |
| 151 |
exit(1); |
| 152 |
} |
| 153 |
|
| 154 |
|