| 1 | package Plagger::Plugin::Filter::Base; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | sub register { | ||
| 6 | my($self, $context) = @_; | ||
| 7 | $context->register_hook( | ||
| 8 | $self, | ||
| 9 | 'update.entry.fixup' => \&update, | ||
| 10 | ); | ||
| 11 | } | ||
| 12 | |||
| 13 | sub update { | ||
| 14 | my($self, $context, $args) = @_; | ||
| 15 | my $body = $args->{entry}->body; | ||
| 16 | |||
| 17 | my $count; | ||
| 18 | if ($self->conf->{text_only}) { | ||
| 19 | ($count, $body) = $self->filter_textonly($body); | ||
| 20 | } else { | ||
| 21 | ($count, $body) = $self->filter($body); | ||
| 22 | } | ||
| 23 | |||
| 24 | if ($count) { | ||
| 25 | $self->log(info => "Filtered $count occurence(s)"); | ||
| 26 | } | ||
| 27 | |||
| 28 | $args->{entry}->body($body); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub filter { | ||
| 32 | my $self = shift; | ||
| 33 | Plagger->context->error(ref($self) . " should override filter"); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub filter_textonly { | ||
| 37 | my($self, $body) = @_; | ||
| 38 | require HTML::Parser; | ||
| 39 | |||
| 40 | my($count, $output); | ||
| 41 | |||
| 42 | my $p = HTML::Parser->new(api_version => 3); | ||
| 43 | $p->handler( default => sub { $output .= $_[0] }, "text" ); | ||
| 44 | $p->handler( text => sub { | ||
| 45 | my($c, $body) = $self->filter($_[0]); | ||
| 46 | $count += $c; | ||
| 47 | $output .= $body; | ||
| 48 | }, "text"); | ||
| 49 | |||
| 50 | $p->parse($body); | ||
| 51 | $p->eof; | ||
| 52 | |||
| 53 | ($count, $output); | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ |