Description: |
The w3 official CSS validator is a really helpful tool, but unfortunately, it is not possible to have its sources.
This script to submit a CSS file to be tested, and returns a summary of the results.
If an error or a warning occured, it will gives all the relevant infos, and if everyting looks fine, it will return a pretty printed version of the CSS. You can also look at the return code of the problem to determine if something went wrong
A small glitch: The HTML parsing doesn't remove newlines, which makes the errors spanning on several lines
|
#!/usr/bin/perl -w
use strict;
#
# W3C CSS Validator by Philippe Le Hégaret & Sijtsche de Jong
#
use HTTP::Request::Common qw(POST);
use HTML::Parser;
use LWP::UserAgent;
use Getopt::Long;
use vars qw(
$validator $ua $req $parser $error $VERSION
$file $warnings $usermedium $profile $help
);
$VERSION = 0.02;
my $inside = 0;
$error = 0; # Script return code. 0: CSS OK, 2: CSS has problems
$file = undef;
$warnings = 2;
$profile = 'css2';
$usermedium = 'all';
$help = undef;
my $results = GetOptions (
"file=s" => \$file,
"warnings=s" => \$warnings,
"profile=s" => \$profile,
"usermedium=s"=> \$usermedium,
"help" => \&usage,
);
$file ||= usage();
$validator = 'http://jigsaw.w3.org/css-validator/validator';
$ua = LWP::UserAgent->new;
$req = POST $validator, Content_Type => 'form-data',
Content => [
file => [ $file ],
warning => $warnings,
profile => $profile,
usermedium => $usermedium,
];
$parser = HTML::Parser->new(
report_tags => [qw(div ul li p)],
ignore_elements => [qw(h2 h3)],
start_h => [ \&start_h, 'tagname, attr' ],
end_h => [ sub { $inside-- if shift eq 'div'; }, 'tagnam
+e' ],
text_h => [ sub { print shift if $inside > 0 }, 'dtext' ]
);
$parser->parse( $ua->request($req)->content );
print "\n";
exit $error;
sub start_h {
my ( $tag, $attr ) = @_;
if ( $tag eq 'div' && $attr->{id}
&& ( $attr->{id} eq 'warnings' || $attr->{id} eq 'errors' ) )
{
$error = 2;
$inside = -1;
print "\n*** ", uc( $attr->{id} ), "\n";
}
$inside++ if $tag eq 'div';
}
sub usage {
die <<"DIE";
W3 CSS Validator Wrapper v.$VERSION
usage: $0 -f stylesheet.css [-w 2] [-p css2] [-u all] [-h]
-f, --file : Filename of the CSS
-w, --warnings : Warnings level
2 : All
1 : Normal report
0 : Most important
no : No warnings
-p, --profile : Stylesheet Profile
none : No special profile
css1 : CSS version 1
mobile : mobile
css2 : CSS version 2
css3 : CSS version 3 (unsupported)
svg : SVG
-u, --usermedium : User Medium
all, aural, braille, embossed, handheld, print, projec
+tion,
screen, tty, tv, presentation,
atsc-tv (unsupported)
-h, --help : Dislay this screen and exits
DIE
}
|