Generalized: &a_href_cz -> &a_href_cc
[www.jankratochvil.net.git] / html-test.pl
1 #! /usr/bin/perl
2 #
3 # $Id$
4
5
6 use strict;
7 use warnings;
8 use My::ModPerlPm;
9 require LWP::UserAgent;
10 require HTTP::Status;
11 require LWP;
12 use Carp qw(confess cluck);
13 use Getopt::Long;
14 use Sys::Hostname::Long;
15 use URI::Escape;
16
17
18 my $URL_BASE="http://".hostname_long().":7680";
19 my $URL_VALIDATOR_BASE="http://validator.w3.org/check?uri=";
20 my $URL_VALIDATOR_BASE_LOCAL="http://localhost/cgi-bin/check.cgi?uri=";
21
22
23 my $opt_validate;
24 my $opt_local;
25 die if !GetOptions(
26                 "validate!",\$opt_validate,
27                 "local!",\$opt_local,
28                 );
29
30 my($first_pattern)=@ARGV;
31 die if @ARGV>=2;
32
33
34 $|=1;
35
36 my $UA=LWP::UserAgent->new();
37 $UA->env_proxy();
38
39 my $first_seen=!$first_pattern;
40 My::ModPerlPm->list("sub"=>sub {
41         my($p)=@_;
42         require $p->{"file"};
43         eval 'require '.$p->{"module"}.'; 1;'
44                         or cluck "Error loading module ".$p->{"module"}.": $@";
45         my $result=eval '$'.$p->{"module"}.'::HTML_TEST;';
46         return if defined $result && !$result;
47         my $url=$URL_BASE.$p->{"url"};
48         my $url_matches=1 if $first_pattern && $url=~/$first_pattern/o;
49         die "Pattern amiguous on: $url\n" if $first_seen && $url_matches;
50         if (!$first_seen && !($first_seen=($url=~/$first_pattern/o))) {
51                 print "_";
52                 return;
53                 }
54         print ".";
55         if ($opt_validate) {
56                 $url=($opt_local ? $URL_VALIDATOR_BASE_LOCAL : $URL_VALIDATOR_BASE).uri_escape($url);
57                 }
58         my $request=HTTP::Request->new("GET",$url);
59         $request->header("Cache-control"=>"no-cache");
60         my $response=$UA->request($request);
61         if ($response->code()==HTTP::Status::RC_OK()) {
62                 return if !$opt_validate;
63                 local $_=$response->content();
64                 my   $valid=/\bclass="valid"\s*>/;
65                 my $invalid=/\bclass="invalid"\s*>/;
66                 die "\nUnexpected response: $url\n" if $valid==$invalid;
67                 return if $valid;
68                 die "\n$url\n";
69                 }
70         die "\n$url: ".$response->code()."\n";
71         });
72 print "\n";
73 die "Nothing seen for: $first_pattern\n" if !$first_seen;