Changeset 1984

Show
Ignore:
Timestamp:
09/28/06 04:10:27
Author:
miyagawa
Message:

Added callback parameter. Release 0.03

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • HTML-ResolveLink/trunk/Changes

    r1787 r1984  
    11Revision history for Perl extension HTML::ResolveLink 
     2 
     30.03  Thu Sep 28 04:03:56 JST 2006 
     4        - Added 'callback' parameter for new() which is called whenever URL is resolved. 
     5          Added POD and test for it. 
    26 
    370.02  2006-02-28T09:36:47Z 
  • HTML-ResolveLink/trunk/MANIFEST

    r1787 r1984  
    55t/00_compile.t 
    66t/01_resolve.t 
    7 META.yml                                 Module meta-data (added by MakeMaker) 
  • HTML-ResolveLink/trunk/lib/HTML/ResolveLink.pm

    r1787 r1984  
    22 
    33use strict; 
    4 our $VERSION = '0.02'; 
     4our $VERSION = '0.03'; 
    55use base qw(HTML::Parser); 
    66 
     
    2222    $p{base} = URI->new($p{base}) unless ref $p{base}; 
    2323    $self->{resolvelink_base} = $p{base}; 
     24    $self->{resolvelink_callback} = $p{callback} if $p{callback}; 
    2425 
    2526    $self; 
     
    4647        # relative link:  
    4748        unless (defined $uri->scheme) { 
     49            my $old = $uri; 
    4850            $uri = $uri->abs($base); 
    4951            $attr->{$a} = $uri->as_string; 
     52            if ($self->{resolvelink_callback}) { 
     53                $self->{resolvelink_callback}->($uri, $old); 
     54            } 
    5055            $self->{resolvelink_count}++; 
    5156        } 
     
    111116  my $resolver = HTML::ResolveLink->new( 
    112117      base => 'http://www.example.com/foo/bar.html', 
     118      callback => sub { 
     119         my($uri, $old) = @_; 
     120         # ... 
     121      }, 
    113122  ); 
    114123  $html = $resolver->resolve($html); 
    115124 
    116   if ($resolver->resolved_count) { 
    117       ... 
    118   } 
    119  
    120125=head1 DESCRIPTION 
    121126 
     
    143148  my $resolver = HTML::ResolveLink->new( 
    144149      base => 'http://www.example.com/', 
     150      callback => \&callback, 
    145151  ); 
    146152 
    147153C<base> is a required parameter, which is used to resolve the relative 
    148154URI found in the document. 
     155 
     156C<callback> is an optional parameter, which is a callback subroutine 
     157reference which would take new resolved URI and the original path as 
     158arguments. 
     159 
     160Here's an example code to illustrate how to use callback function. 
     161 
     162  my $count; 
     163  my $resolver = HTML::ResolveLink->new( 
     164      base => $base, 
     165      callback => sub { 
     166          my($uri, $old) = @_; 
     167          warn "$old is resolved to $uri"; 
     168          $count++; 
     169      }, 
     170  ); 
     171 
     172  $html = $resolver->resolve($html); 
     173 
     174  if ($count) { 
     175      warn "HTML::ResolveLink resolved $count links"; 
     176  } 
    149177 
    150178=item resolve 
  • HTML-ResolveLink/trunk/t/01_resolve.t

    r1787 r1984  
    11use strict; 
    2 use Test::More tests => 4
     2use Test::More tests => 9
    33use HTML::ResolveLink; 
    44 
    55my $base = "http://www.example.com/base/"; 
     6my $count = 0; 
     7my $log; 
    68 
    7 my $resolver = HTML::ResolveLink->new(base => $base); 
     9my $resolver = HTML::ResolveLink->new( 
     10    base => $base, 
     11    callback => sub { 
     12        my($uri, $old) = @_; 
     13        $log .= "$old => $uri\n"; 
     14        $count++; 
     15    }, 
     16); 
     17 
    818my $html = $resolver->resolve(<<'HTML'); 
    919<a href="/foo">foo</a><img src="/bar.gif" alt="foo &amp; bar" /> foobar 
     
    2232HTML 
    2333 
    24 is $resolver->resolved_count, 3; 
     34is $count, 3; 
     35like $log, qr!/foo => http://www.example.com/foo!; 
     36like $log, qr!bar.gif => http://www.example.com/bar.gif!; 
     37like $log, qr!foo.html => http://www.example.com/base/foo.html!; 
    2538 
     39$count = 0; 
    2640$html = $resolver->resolve(<<'HTML'); 
    2741<base href="http://www.google.com/"> 
     
    3953    ; 
    4054 
     55is $count, 2; 
    4156is $resolver->resolved_count, 2; 
    4257 
     58$resolver = HTML::ResolveLink->new(base => $base); # reset 
     59 
     60$html = $resolver->resolve(<<'HTML'); 
     61<a href="baz">&amp;</a> 
     62&quot;foo&quot; 
     63HTML 
     64 
     65is $html, <<'HTML', 'HTML entities'; 
     66<a href="http://www.example.com/base/baz">&amp;</a> 
     67&quot;foo&quot; 
     68HTML 
     69    ;