Changeset 415
- Timestamp:
- 03/24/06 17:44:42
- Files:
-
- trunk/plagger/lib/Plagger.pm (modified) (1 diff)
- trunk/plagger/lib/Plagger/Plugin.pm (modified) (1 diff)
- trunk/plagger/lib/Plagger/Plugin/Filter/Base.pm (modified) (1 diff)
- trunk/plagger/lib/Plagger/Plugin/Filter/Regexp.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plagger/lib/Plagger.pm
r392 r415 239 239 240 240 sub log { 241 my($self, $level, $msg ) = @_;241 my($self, $level, $msg, %opt) = @_; 242 242 243 243 # hack to get the original caller as Plugin or Rule 244 my $caller; 245 my $i = 0; 246 while (my $c = caller($i++)) { 247 last if $c !~ /Plugin|Rule/; 248 $caller = $c; 249 } 250 $caller ||= caller(0); 244 my $caller = $opt{caller}; 245 unless ($caller) { 246 my $i = 0; 247 while (my $c = caller($i++)) { 248 last if $c !~ /Plugin|Rule/; 249 $caller = $c; 250 } 251 $caller ||= caller(0); 252 } 251 253 252 254 chomp($msg); trunk/plagger/lib/Plagger/Plugin.pm
r300 r415 63 63 } 64 64 65 sub log { 66 my $self = shift; 67 Plagger->context->log(@_, caller => ref($self)); 68 } 69 65 70 1; trunk/plagger/lib/Plagger/Plugin/Filter/Base.pm
r407 r415 23 23 24 24 if ($count) { 25 $ context->log(info => "Rewrite $count");25 $self->log(info => "Filtered $count occurence(s)"); 26 26 } 27 27 trunk/plagger/lib/Plagger/Plugin/Filter/Regexp.pm
r281 r415 1 1 package Plagger::Plugin::Filter::Regexp; 2 2 use strict; 3 use base qw( Plagger::Plugin );3 use base qw( Plagger::Plugin::Filter::Base ); 4 4 5 use HTML::Entities; 5 sub init { 6 my $self = shift; 7 $self->SUPER::init(@_); 6 8 7 sub register { 8 my($self, $context) = @_; 9 $context->register_hook( 10 $self, 11 'update.entry.fixup' => \&update, 12 ); 9 unless ($self->conf->{regexp}) { 10 Plagger->context->error("regexp is missing"); 11 return; 12 } 13 13 } 14 14 15 sub update { 16 my($self, $context, $args) = @_; 17 my $body = $args->{entry}->body; 18 19 my $regexp = $self->conf->{regexp}; 20 unless ($regexp) { 21 $context->log(error => "regexp is missing"); 22 return; 23 } 24 25 my $count; 26 if ($self->conf->{text_only}) { 27 ($count, $body) = $self->rewrite_text_only($body, $regexp); 28 } else { 29 ($count, $body) = $self->rewrite($body, $regexp); 30 } 31 32 if ($count) { 33 $context->log(info => "Replaced $count time(s) using $regexp"); 34 } 35 36 $args->{entry}->body($body); 37 } 38 39 sub rewrite { 40 my($self, $body, $regexp) = @_; 15 sub filter { 16 my($self, $body) = @_; 41 17 42 18 local $_ = $body; 43 my $count = eval $ regexp;19 my $count = eval $self->conf->{regexp}; 44 20 45 21 if ($@) { 46 Plagger->context->log(error => "Error: $@ in $regexp");22 Plagger->context->log(error => "Error: $@ in " . $self->conf->{regexp}); 47 23 return (0, $body); 48 24 } 49 25 50 26 return ($count, $_); 51 }52 53 sub rewrite_text_only {54 my($self, $body, $regexp) = @_;55 require HTML::Parser;56 57 my($count, $output);58 59 my $p = HTML::Parser->new(api_version => 3);60 $p->handler( default => sub { $output .= $_[0] }, "text" );61 $p->handler( text => sub {62 my($c, $body) = $self->rewrite( decode_entities($_[0]), $regexp );63 $count += $c;64 $output .= encode_entities($body, q("<>&));65 }, "text");66 67 $p->parse($body);68 $p->eof;69 70 ($count, $output);71 27 } 72 28
