package Statocles::Plugin::HTMLLint;
# ABSTRACT: Check HTML for common errors and issues
$Statocles::Plugin::HTMLLint::VERSION = '0.064';
use Statocles::Base 'Class';
with 'Statocles::Plugin';
BEGIN {
    eval { require HTML::Lint::Pluggable; HTML::Lint::Pluggable->VERSION( 0.06 ); 1 }
        or die "Error loading Statocles::Plugin::HTMLLint. To use this plugin, install HTML::Lint::Pluggable";
};


has plugins => (
    is => 'ro',
    isa => ArrayRef,
    default => sub { [qw( HTML5 TinyEntitesEscapeRule )] },
);


sub check_pages {
    my ( $self, $event ) = @_;
    my @plugins = @{ $self->plugins };

    for my $page ( @{ $event->pages } ) {
        if ( $page->DOES( 'Statocles::Page::Document' ) ) {
            my $html = $page->render( site => $event->emitter );
            my $page_url = $page->path;

            my $lint = HTML::Lint::Pluggable->new;
            $lint->load_plugins( @plugins );
            $lint->parse( $html );

            if ( $lint->errors ) {
                $event->emitter->log->warn( "Lint failures on $page_url:" );
                for my $error ( $lint->errors ) {
                    $event->emitter->log->warn( "-" . $error->as_string );
                }
            }

        }
    }
}


sub register {
    my ( $self, $site ) = @_;
    $site->on( build => sub { $self->check_pages( @_ ) } );
}

1;

__END__

=pod

=head1 NAME

Statocles::Plugin::HTMLLint - Check HTML for common errors and issues

=head1 VERSION

version 0.064

=head1 SYNOPSIS

    # site.yml
    site:
        class: Statocles::Site
        args:
            plugins:
                lint:
                    $class: Statocles::Plugin::HTMLLint

=head1 DESCRIPTION

This plugin checks all of the HTML to ensure it's correct and complete. If something
is missing, this plugin will write a warning to the screen.

=head1 ATTRIBUTES

=head2 plugins

The L<HTML::Lint::Pluggable> plugins to use. Defaults to a generic set of
plugins good for HTML5: 'HTML5' and 'TinyEntitesEscapeRule'

=head1 METHODS

=head2 check_pages

    $plugin->check_pages( $event );

Check the pages inside the given
L<Statocles::Event::Pages|Statocles::Event::Pages> event.

=head2 register

Register this plugin to install its event handlers. Called automatically.

=head1 AUTHOR

Doug Bell <preaction@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Doug Bell.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
