package Plagger::Plugin::Filter::StripImage;
use strict;
use base qw( Plagger::Plugin );

use HTML::Parser;

sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'update.entry.fixup' => \&update,
    );
}

sub update {
    my($self, $context, $args) = @_;
    my $body = $self->filter($args->{entry}->body);
    $args->{entry}->body($body);
}

sub filter {
    my($self, $body) = @_;
    my $html;
    $self->conf->{ban} ||= [];
    push($self
    my $parser = HTML::Parser->new(
	api_version => 3,
	handlers => {
	    start => [$self->starthandler(\$html), 'tagname, attr, text'],
	    default => [$self->othershandler(\$html), 'text'],
	},
    );
    $parser->parse($body);
    $parser->eof;
    return $html;
}

sub starthandler{
    my ($self, $html) = @_;
    return sub {
	my ($tagname, $attr, $text) = @_;
	$$html .= $text if($tagname ne "img");

	my $ret=0;
	foreach my $url (@{$self->conf->{ban}}){
	    if(index($attr->{src}, $url) < $[){
		$ret++;
		last;
	    }
	}
	$$html .= $text if !$ret;
    };
}

sub othershandler{
    my ($self, $html) = @_;
    return sub {
	my $text = shift;
	$$html .= $text;
    };
}

1;
__END__
example config.
  - module: Filter::StripRSSAd
    config:
      ban:
        - "http://rss.rssad.jp/rss/img"
        - "http://imageads.googleadservices.com/pagead/ads"
