Changeset 217

Show
Ignore:
Timestamp:
03/01/06 18:26:07
Author:
miyagawa
Message:

Filter::TTP: Added html_paranoia option to use HTML::Parser. Fixes #76

Files:

Legend:

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

    r185 r217  
    22use strict; 
    33use base qw( Plagger::Plugin ); 
     4 
    45use URI::Find; 
    56 
     
    1617    my $body = $args->{entry}->body; 
    1718 
     19    my $count; 
     20    if ($self->conf->{html_paranoia}) { 
     21        ($count, $body) = $self->paranoia_rewrite($body); 
     22    } else { 
     23        ($count, $body) = $self->rewrite_ttp($body); 
     24    } 
     25 
     26    if ($count) { 
     27        $context->log(info => "Rewrite $count ttp:// link(s) to http://"); 
     28    } 
     29 
     30    $args->{entry}->body($body); 
     31} 
     32 
     33sub rewrite_ttp { 
     34    my($self, $body) = @_; 
     35 
    1836    local @URI::ttp::ISA = qw(URI::http); 
    1937 
     
    2240        return ($uri->scheme eq 'ttp') ? qq{<a href="h$orig_uri">$orig_uri</a>} : $orig_uri; 
    2341    }); 
    24     $finder->find(\$body); 
    2542 
    26     $args->{entry}->body($body); 
     43    my $count = $finder->find(\$body); 
     44 
     45    ($count, $body); 
     46
     47 
     48sub paranoia_rewrite { 
     49    my($self, $body) = @_; 
     50    require HTML::Parser; 
     51 
     52    my($count, $output); 
     53 
     54    my $p = HTML::Parser->new(api_version => 3); 
     55    $p->handler( default => sub { $output .= $_[0] }, "text" ); 
     56    $p->handler( text => sub { 
     57        my($c, $body) = $self->rewrite_ttp($_[0]); 
     58        $count  += $c; 
     59        $output .= $body; 
     60    }, "text"); 
     61 
     62    $p->parse($body); 
     63    $p->eof; 
     64 
     65    ($count, $output); 
    2766} 
    2867 
     
    4483adopted way of linking an URL without leaking a referer. 
    4584 
     85=head1 CONFIG 
     86 
     87=over 4 
     88 
     89=item html_paranoia 
     90 
     91When set to 1, uses HTML::Parser to avoid replacing C<ttp://> inside 
     92HTML elements. Defaults to 0. 
     93 
     94=back 
     95 
    4696=head1 AUTHOR 
    4797 
    4898Matsuno Tokuhiro 
    4999 
     100Tatsuhiko Miyagawa 
     101 
    50102=head1 SEE ALSO 
    51103 
    52 L<Plagger> 
     104L<Plagger>, L<HTML::Parser> 
    53105 
    54106=cut