Created
August 19, 2010 17:49
-
-
Save jjn1056/538467 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Plack::Middleware::Debug::W3CValidate; | |
our $VERSION = '0.01'; | |
use 5.008; | |
use strict; | |
use warnings; | |
use parent qw(Plack::Middleware::Debug::Base); | |
use WebService::Validator::HTML::W3C; | |
use Plack::Util::Accessor qw(validator_uri); | |
sub get_validator { | |
my $self = shift @_; | |
my %opts = ($self->validator_uri ? (validator_uri=>$self->validator_uri) : ()); | |
return WebService::Validator::HTML::W3C->new(detailed=>1, %opts); | |
} | |
sub flatten_body { | |
my ($self, $res) = @_; | |
my $body = $res->[2]; | |
if(ref $body eq 'ARRAY') { | |
return join "", @$body; | |
} elsif(defined $body) { | |
my $slurped; | |
while (defined(my $line = $body->getline)) { | |
$slurped .= $line if length $line; | |
} | |
return $slurped; | |
} | |
} | |
sub run { | |
my($self, $env, $panel) = @_; | |
my $v = $self->get_validator; | |
$panel->title("W3C Validation"); | |
$panel->nav_title("W3C Validation"); | |
return sub { | |
my $res = shift @_; | |
my $slurped_body = $self->flatten_body($res); | |
if($v->validate_markup($slurped_body)) { | |
if ( $v->is_valid ) { | |
$panel->nav_subtitle("Page validated."); | |
} else { | |
my %errors = | |
map { "Line: ". $_->line . ", Column: ". $_->col => $_->msg} | |
@{$v->errors}; | |
$panel->nav_subtitle('Not valid. Error Count: '.$v->num_errors ); | |
$panel->content(sub { | |
$self->render_hash(\%errors); | |
}); | |
} | |
} else { | |
$panel->content("Failed to validate the website: ".$v->validator_error); | |
} | |
} | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment