Validator->RSS

LTjake on 2004-08-06T14:51:25

Ben Hammersley has a neat bit of code to take the W3C's validator results and spit back an RSS feed.

It's pretty neat, but it only reports validation errors -- nothing for success, nor connection errors.

By letting WebService::Validator::HTML::W3C do the hard work, I was able to add those few details to Ben's cool idea.

#!/usr/bin/perl

use strict;
use warnings;

use WebService::Validator::HTML::W3C;
use CGI;
use XML::RSS;

my $validator = WebService::Validator::HTML::W3C->new( detailed => 1 );
my $query     = CGI->new;
my $rss       = XML::RSS->new;
my $url       = $query->param( 'url' );
my $link      = "http://validator.w3.org/check?uri=$url";

$rss->channel( title => "Validation results for $url" );
$rss->channel( link  => $link );

if ( $validator->validate( $url ) ) {
	if ( $validator->is_valid ) {
		$rss->add_item(
			title       => 'This Page Is Valid!',
			link        => $link,
			description => "$url is valid."
		);
	}
	else {
		foreach my $error ( @{ $validator->errors } ) {
			$rss->add_item(
				title       => 'Line ' . $error->line . ', ' . $error->msg,
				link        => $link,
				description => 'Line ' . $error->line . ', ' . $error->msg
			);
		}
	}
}
else {
	$rss->add_item(
		title       => 'Validation Error',
		link        => $link,
		description => $validator->validator_error
	);
}

print $query->header( 'application/rss+xml' ), $rss->as_string;