| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use 5.010001; |
| 4 |
use warnings; |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Getopt::Long; |
| 8 |
use HTML::Tidy5; |
| 9 |
|
| 10 |
my $help; |
| 11 |
my $context; |
| 12 |
|
| 13 |
my $tidy = HTML::Tidy5->new; |
| 14 |
|
| 15 |
GetOptions( |
| 16 |
'help|version' => \$help, |
| 17 |
'context:i' => \$context, |
| 18 |
'noerrors' => sub { $tidy->ignore( type => [ TIDY_ERROR ] ) }, |
| 19 |
'nowarnings' => sub { $tidy->ignore( type => [ TIDY_WARNING ] ) }, |
| 20 |
) or $help = 1; |
| 21 |
|
| 22 |
if ( !@ARGV || $help ) { |
| 23 |
print "webtidy v$HTML::Tidy5::VERSION using tidy v" . HTML::Tidy5::tidy_library_version() . "\n"; |
| 24 |
print <DATA>; |
| 25 |
exit 1; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
for my $url ( @ARGV ) { |
| 30 |
my @lines; |
| 31 |
if ( $url =~ /^https?:/ ) { |
| 32 |
if ( !eval { require LWP::Simple; 1; } ) { |
| 33 |
warn q{Can't retrieve URLs without LWP::Simple installed}; |
| 34 |
next; |
| 35 |
} |
| 36 |
|
| 37 |
my $content = LWP::Simple::get( $url ); |
| 38 |
if ( $content ) { |
| 39 |
@lines = split( /\n/, $content ); |
| 40 |
$_ = "$_\n" for @lines; |
| 41 |
} else { |
| 42 |
warn "Unable to fetch $url\n"; |
| 43 |
next; |
| 44 |
} |
| 45 |
} else { |
| 46 |
open( my $fh, '<', $url ) or die "Can't open $url: $!"; |
| 47 |
@lines = <$fh>; |
| 48 |
close $fh or die $!; |
| 49 |
} |
| 50 |
|
| 51 |
$tidy->parse( $url, @lines ); |
| 52 |
for my $message ( $tidy->messages ) { |
| 53 |
print $message->as_string(), "\n"; |
| 54 |
if ( defined $context ) { |
| 55 |
$context += 0; |
| 56 |
my $lineno = $message->line - 1; |
| 57 |
|
| 58 |
my $start = $lineno-$context; |
| 59 |
$start = 0 if $start < 0; |
| 60 |
|
| 61 |
my $end = $lineno+$context; |
| 62 |
$end = $#lines if $end > $#lines; |
| 63 |
|
| 64 |
for my $i ( $start..$end ) { |
| 65 |
printf( '%5d: %s', $i+1, $lines[$i] ); |
| 66 |
} |
| 67 |
print "\n"; |
| 68 |
} |
| 69 |
} |
| 70 |
$tidy->clear_messages(); |
| 71 |
} # for files |
| 72 |
|
| 73 |
__END__ |
| 74 |
Usage: webtidy [filename or url]... (filename - reads STDIN) |
| 75 |
--context[=n] Show the offending line (and n surrounding lines) |
| 76 |
--noerrors Ignore errors |
| 77 |
--nowarnings Ignore warnings |
| 78 |
|
| 79 |
--help This message |
| 80 |
|
| 81 |
webtidy is free software. You may modify or distribute it under the |
| 82 |
terms of the Artistic License v2.0. |