Changeset 728

Show
Ignore:
Timestamp:
05/13/06 06:07:25
Author:
miyagawa
Message:

Filter::TruePermalink? now can rewrite enclosure URL too.

Files:

Legend:

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

    r614 r728  
    4343    my($self, $context, $args) = @_; 
    4444 
    45     my $orig = $args->{entry}->permalink; 
     45    $self->rewrite($args->{entry}->permalink, sub { $args->{entry}->link(@_) }); 
     46    for my $enclosure ($args->{entry}->enclosures) { 
     47        $self->rewrite($enclosure->url, sub { $enclosure->url( URI->new(@_) ) }); 
     48    } 
     49
     50 
     51sub rewrite { 
     52    my($self, $link, $callback) = @_; 
     53 
     54    my $context = Plagger->context; 
     55 
     56    my $orig = $link; # copy 
    4657    my $count = 0; 
     58    my $rewritten; 
    4759 
    4860    for my $plugin (@{ $self->{plugins}}) { 
    4961        my $match = $plugin->{match} || '.'; # anything 
    50         next unless $args->{entry}->permalink =~ m/$match/i; 
     62        next unless $link =~ m/$match/i; 
    5163 
    5264        if ($plugin->{rewrite}) { 
    53             local $_ = $args->{entry}->permalink; 
     65            local $_ = $link; 
    5466            $count += eval $plugin->{rewrite}; 
    5567            if ($@) { 
    5668                $context->error("$@ in $plugin->{rewrite}"); 
    5769            } 
    58             $args->{entry}->link($_); 
     70            $callback->($_); 
     71            $rewritten = $_; 
    5972        } elsif ($plugin->{query_param}) { 
    60             my $link = URI->new($args->{entry}->permalink)->query_param($plugin->{query_param}) 
    61                 or $context->error("No query param $plugin->{query_param} in " . $args->{entry}->permalink); 
    62             $args->{entry}->link($link); 
     73            my $param = URI->new($link)->query_param($plugin->{query_param}) 
     74                or $context->error("No query param $plugin->{query_param} in " . $link); 
     75            $callback->($param); 
    6376            $count++; 
     77            $rewritten = $param; 
     78            last; 
    6479        } 
    6580    } 
    6681 
    6782    if ($count) { 
    68         $context->log(info => "Permalink $orig rewritten to " . $args->{entry}->permalink); 
     83        $context->log(info => "Link $orig rewritten to $rewritten"); 
    6984    } 
    7085} 
     
    89104this plugin. 
    90105 
    91 This plugin rewrites I<link> attribute of C<$entry>, rather than I<permalink>. 
     106This plugin rewrites I<link> attribute of C<$entry>, rather than 
     107I<permalink>. If C<$entry> has enclosures, this plugin also tries to 
     108rewrite url of them. 
    92109 
    93110=head1 PATTERN FILES