HTML::Feature::Engine - HTML::Feature::Engine documentation


HTML-Feature documentation Contained in the HTML-Feature distribution.

Index


Code Index:

NAME

Top

HTML::Feature::Engine -

SYNOPSIS

Top

  use HTML::Feature::Engine;

DESCRIPTION

Top

HTML::Feature::Engine is

METHODS

Top

new

run

AUTHOR

Top

Takeshi Miki <miki@cpan.org>

LICENSE

Top

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

SEE ALSO

Top


HTML-Feature documentation Contained in the HTML-Feature distribution.

package HTML::Feature::Engine;
use strict;
use warnings;
use HTML::Feature::Engine::TagStructure;
use HTML::Feature::Engine::LDRFullFeed;
use HTML::Feature::Engine::GoogleADSection;
use UNIVERSAL::require;
use Encode;
use Carp;
use base qw(HTML::Feature::Base);

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new(@_);
    $self->_setup;
    return $self;
}

sub run {
    my $self     = shift;
    my $html_ref = shift;
    my $url      = shift;
    my $c        = $self->context;
    my $result   = HTML::Feature::Result->new;
    eval {
        LABEL: for my $engine ( @{ $self->{engines} } )
        {
            $result = $engine->run( $html_ref, $url, $result );
            if ( $result->{matched_engine} ) {
                if ( defined $c->{enc_type} ) {
                    $result->title(
                        Encode::encode( $c->{enc_type}, $result->title ) );
                    $result->desc(
                        Encode::encode( $c->{enc_type}, $result->desc ) );
                    $result->text(
                        Encode::encode( $c->{enc_type}, $result->text ) );
                }
                last LABEL;
            }
        }
    };
    if ($@) {
        carp("can not parse data");
        return HTML::Feature::Result->new;
    }
    return $result;
}

sub _setup {
    my $self   = shift;
    my $c      = $self->context;
    my $config = $c->config;
    if ( !defined $config->{engines} || @{ $config->{engines} } < 1 ) {
        my $engine = HTML::Feature::Engine::TagStructure->new( context => $c );
        push( @{ $self->{engines} }, $engine );
    }
    else {
        for my $class ( @{ $config->{engines} } ) {
            unless ( $class->can('new') ) {
                $class->require or die $@;
            }
            my $engine = $class->new( context => $c );
            push( @{ $self->{engines} }, $engine );
        }
    }
}

1;
__END__