Changeset 415

Show
Ignore:
Timestamp:
03/24/06 17:44:42
Author:
miyagawa
Message:
  • Filter::Regexp to use Filter::Base now
  • $plugin has log method now to support subclass to get proper caller
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger.pm

    r392 r415  
    239239 
    240240sub log { 
    241     my($self, $level, $msg) = @_; 
     241    my($self, $level, $msg, %opt) = @_; 
    242242 
    243243    # 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    } 
    251253 
    252254    chomp($msg); 
  • trunk/plagger/lib/Plagger/Plugin.pm

    r300 r415  
    6363} 
    6464 
     65sub log { 
     66    my $self = shift; 
     67    Plagger->context->log(@_, caller => ref($self)); 
     68} 
     69 
    65701; 
  • trunk/plagger/lib/Plagger/Plugin/Filter/Base.pm

    r407 r415  
    2323 
    2424    if ($count) { 
    25         $context->log(info => "Rewrite $count"); 
     25        $self->log(info => "Filtered $count occurence(s)"); 
    2626    } 
    2727 
  • trunk/plagger/lib/Plagger/Plugin/Filter/Regexp.pm

    r281 r415  
    11package Plagger::Plugin::Filter::Regexp; 
    22use strict; 
    3 use base qw( Plagger::Plugin ); 
     3use base qw( Plagger::Plugin::Filter::Base ); 
    44 
    5 use HTML::Entities; 
     5sub init { 
     6    my $self = shift; 
     7    $self->SUPER::init(@_); 
    68 
    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    } 
    1313} 
    1414 
    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) = @_; 
     15sub filter { 
     16    my($self, $body) = @_; 
    4117 
    4218    local $_ = $body; 
    43     my $count = eval $regexp
     19    my $count = eval $self->conf->{regexp}
    4420 
    4521    if ($@) { 
    46         Plagger->context->log(error => "Error: $@ in $regexp"); 
     22        Plagger->context->log(error => "Error: $@ in " . $self->conf->{regexp}); 
    4723        return (0, $body); 
    4824    } 
    4925 
    5026    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); 
    7127} 
    7228