File: webtidy5

package info (click to toggle)
libhtml-tidy5-perl 1.06-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: perl: 1,853; makefile: 14
file content (82 lines) | stat: -rwxr-xr-x 2,089 bytes parent folder | download | duplicates (3)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl

use 5.010001;
use warnings;
use strict;

use Getopt::Long;
use HTML::Tidy5;

my $help;
my $context;

my $tidy = HTML::Tidy5->new;

GetOptions(
    'help|version'  => \$help,
    'context:i'     => \$context,
    'noerrors'      => sub { $tidy->ignore( type => [ TIDY_ERROR ] ) },
    'nowarnings'    => sub { $tidy->ignore( type => [ TIDY_WARNING ] ) },
) or $help = 1;

if ( !@ARGV || $help ) {
    print "webtidy v$HTML::Tidy5::VERSION using tidy v" . HTML::Tidy5::tidy_library_version() . "\n";
    print <DATA>;
    exit 1;
}


for my $url ( @ARGV ) {
    my @lines;
    if ( $url =~ /^https?:/ ) {
        if ( !eval { require LWP::Simple; 1; } ) {
            warn q{Can't retrieve URLs without LWP::Simple installed};
            next;
        }

        my $content = LWP::Simple::get( $url );
        if ( $content ) {
            @lines = split( /\n/, $content );
            $_ = "$_\n" for @lines;
        } else {
            warn "Unable to fetch $url\n";
            next;
        }
    } else {
        open( my $fh, '<', $url ) or die "Can't open $url: $!";
        @lines = <$fh>;
        close $fh or die $!;
    }

    $tidy->parse( $url, @lines );
    for my $message ( $tidy->messages ) {
        print $message->as_string(), "\n";
        if ( defined $context ) {
            $context += 0;
            my $lineno = $message->line - 1;

            my $start = $lineno-$context;
            $start = 0 if $start < 0;

            my $end = $lineno+$context;
            $end = $#lines if $end > $#lines;

            for my $i ( $start..$end ) {
                printf( '%5d: %s', $i+1, $lines[$i] );
            }
            print "\n";
        }
    }
    $tidy->clear_messages();
} # for files

__END__
Usage: webtidy [filename or url]... (filename - reads STDIN)
    --context[=n]   Show the offending line (and n surrounding lines)
    --noerrors      Ignore errors
    --nowarnings    Ignore warnings

    --help          This message

webtidy is free software.  You may modify or distribute it under the
terms of the Artistic License v2.0.