Skip to content

Commit

Permalink
title_of
Browse files Browse the repository at this point in the history
  • Loading branch information
cho45 committed Dec 1, 2011
1 parent 07a834c commit 2fd7ab7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
7 changes: 5 additions & 2 deletions benchmark/benchmark.pl
Expand Up @@ -2,7 +2,8 @@
use strict;
use warnings;
use utf8;
use Benchmark qw(:all) ;
use Time::HiRes;
use Benchmark qw(:all :hireswallclock) ;

my $text = <<EOS;
* Xatena
Expand Down Expand Up @@ -68,7 +69,7 @@
my $markdown = Text::Markdown->new;
my $textile = Text::Textile->new;

cmpthese(-1, {
my $results = timethese(-1, {
"Text::Xatena $Text::Xatena::VERSION" => sub {
my $html = $thx->format($text);
},
Expand All @@ -83,6 +84,8 @@
},
});

cmpthese($results);

__END__
MacBook Air 1.8GHz Intel Core i7 / 4GB RAM
Rate Text::Hatena 0.20 Text::Markdown 1.000031 Text::Textile 2.12 Text::Xatena 0.14
Expand Down
3 changes: 1 addition & 2 deletions lib/Text/Xatena/Inline.pm
Expand Up @@ -75,8 +75,7 @@ match qr<\[((?:https?|ftp)://[^\s:]+(?::\d+)?[^\s:]+)(:(?:title(?:=([^[]+))?|bar
$title = $self->cache->get($uri);
if (not defined $title) {
eval {
my $res = $self->ua->get($uri);
($title) = ($res->decoded_content =~ qr|<title[^>]*>([^<]*)</title>|i);
$title = $self->title_of($uri);
$self->cache->set($uri, $title, "30 days");
};
if ($@) {
Expand Down
19 changes: 15 additions & 4 deletions lib/Text/Xatena/Inline/Aggressive.pm
Expand Up @@ -11,10 +11,21 @@ sub cache { $_[0]->{cache} }
sub ua { $_[0]->{ua} || $ua }

sub new {
my ($class, @args) = @_;
my $self = $class->SUPER::new(@args);
$self->{aggressive} = 1;
$self;
my ($class, @args) = @_;
my $self = $class->SUPER::new(@args);
$self->{aggressive} = 1;
$self;
}

sub title_of {
my ($self, $uri) = @_;
my $res = $self->ua->get($uri);
if ($res->is_success) {
my ($title) = ($res->decoded_content =~ qr|<title[^>]*>([^<]*)</title>|i);
$title;
} else {
$res->status_line;
}
}

1;
Expand Down
61 changes: 54 additions & 7 deletions t/04_inline_aggressive.t
@@ -1,6 +1,8 @@
use utf8;
use strict;
use warnings;
use lib 't/lib';
use Encode;
use Text::Xatena::Test;
use Cache::MemoryCache;
use LWP::UserAgent;
Expand All @@ -11,12 +13,27 @@ local $Text::Xatena::Test::INLINE_ARGS = [ cache => Cache::MemoryCache->new ];
no warnings 'redefine';
*LWP::UserAgent::get = sub {
my ($self, $uri) = @_;
local $_ = $uri;
if (qr|http://example.com/|) {
HTTP::Response->new(200, "OK", [], "<title>Example Web Page</title>");
} else {
die "unknown url";
}

my $res = {
'http://example.com/' => sub {
HTTP::Response->new(200, "OK", ['Content-Type' => 'text/html'], "<title>Example Web Page</title>");
},
'http://example.com/utf-8' => sub {
HTTP::Response->new(200, "OK", ['Content-Type' => 'text/html'], encode("utf-8", "<title>エグザンプルウェブページ</title>"));
},
'http://example.com/shift_jis' => sub {
HTTP::Response->new(200, "OK", ['Content-Type' => 'text/html'], encode("shift_jis", "<meta charset='shift_jis'><title>エグザンプルウェブページ</title>"));
},
'http://example.com/euc-jp' => sub {
HTTP::Response->new(200, "OK", ['Content-Type' => 'text/html'], encode("euc-jp", "<meta charset='euc-jp'><title>エグザンプルウェブページ</title>"));
},
'http://example.com/shift_jis_ct' => sub {
HTTP::Response->new(200, "OK", ['Content-Type' => 'text/html; charset=shift_jis'], encode("shift_jis", "<title>エグザンプルウェブページ</title>"));
},
};

my $sub = $res->{$uri} or BAIL_OUT("Unexpected web access");
$sub->();
};
};

Expand Down Expand Up @@ -117,7 +134,7 @@ http://example.com/
</q>
</p>
=== test
=== http title
--- input
[http://example.com/:title]
--- expected
Expand All @@ -126,3 +143,33 @@ http://example.com/
Example Web Page
</a>
</p>
=== http title
--- input
[http://example.com/utf-8:title]
--- expected
<p>
<a href="http://example.com/utf-8">
エグザンプルウェブページ
</a>
</p>
=== http title
--- input
[http://example.com/shift_jis:title]
--- expected
<p>
<a href="http://example.com/shift_jis">
エグザンプルウェブページ
</a>
</p>
=== http title
--- input
[http://example.com/euc-jp:title]
--- expected
<p>
<a href="http://example.com/euc-jp">
エグザンプルウェブページ
</a>
</p>

0 comments on commit 2fd7ab7

Please sign in to comment.