line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Hatena::AutoLink; |
2
|
4
|
|
|
4
|
|
144459
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
217
|
|
3
|
4
|
|
|
4
|
|
24
|
use warnings; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
123
|
|
4
|
4
|
|
|
4
|
|
23
|
use Carp; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
554
|
|
5
|
4
|
|
|
4
|
|
6004
|
use Regexp::Assemble; |
|
4
|
|
|
|
|
121892
|
|
|
4
|
|
|
|
|
181
|
|
6
|
4
|
|
|
4
|
|
123
|
use base qw(Class::Data::Inheritable); |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
41
|
|
7
|
4
|
|
|
4
|
|
3861
|
use vars qw($VERSION); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
3039
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
__PACKAGE__->mk_classdata('syntax'); |
10
|
|
|
|
|
|
|
__PACKAGE__->syntax({}); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$VERSION = '0.20'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $ra; |
15
|
|
|
|
|
|
|
my $syntax = { |
16
|
|
|
|
|
|
|
'\[\](.+?)\[\]' => \&unbracket, |
17
|
|
|
|
|
|
|
'(?:\[)?(ftp:\/\/[A-Za-z0-9~\/._\?\&=\-%#\+:\;,\@\']+)(?:\])?' => \&http, |
18
|
|
|
|
|
|
|
'(?:\[)?(https?:\/\/[A-Za-z0-9~\/._\?\&=\-%#\+:\;,\@\']+)(?:\])?' => \&http, |
19
|
|
|
|
|
|
|
'\[(https?:\/\/[A-Za-z0-9~\/._\?\&=\-%#\+:\;,\@\']+(?:jpg|jpeg|gif|png|bmp)):image(:[hw]\d+)?\]' => \&http_image, |
20
|
|
|
|
|
|
|
'\[(https?:\/\/[A-Za-z0-9~\/._\?\&=\-%#\+:\;,\@\']+):title=([^\]]+)\]' => \&http_title, |
21
|
|
|
|
|
|
|
'(?:\[)?mailto:(\w[\w\.-]+\@\w[\w\.\-]*\w)(?:\])?' => \&mailto, |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub parse { |
25
|
0
|
|
|
0
|
|
|
my $class = shift; |
26
|
0
|
|
|
|
|
|
my $text = shift; |
27
|
0
|
|
|
|
|
|
my $html = ''; |
28
|
0
|
|
|
|
|
|
my $ra = $class->ra; |
29
|
0
|
|
0
|
|
|
|
while ($text && $ra->match($text)) { |
30
|
0
|
0
|
|
|
|
|
$html .= substr($text, 0, $ra->mbegin->[0]) if $ra->mbegin->[0]; |
31
|
0
|
|
0
|
|
|
|
my $handler = $class->syntax->{$ra->matched} || $syntax->{$ra->matched}; |
32
|
0
|
|
|
|
|
|
$html .= $handler->($ra->mvar()); |
33
|
0
|
|
|
|
|
|
$text = substr($text, $ra->mend->[0]); |
34
|
|
|
|
|
|
|
} |
35
|
0
|
0
|
|
|
|
|
$html .= $text if $text; |
36
|
0
|
|
|
|
|
|
return $html; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub ra { |
40
|
0
|
|
|
0
|
|
|
my $class = shift; |
41
|
0
|
0
|
|
|
|
|
unless ($ra) { |
42
|
0
|
|
|
|
|
|
$ra = Regexp::Assemble->new( |
43
|
|
|
|
|
|
|
flags => 'i', |
44
|
|
|
|
|
|
|
track => 1, |
45
|
|
|
|
|
|
|
reduce => 1, |
46
|
|
|
|
|
|
|
); |
47
|
0
|
|
|
|
|
|
$ra->add(keys %$syntax, keys %{$class->syntax}); |
|
0
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
return $ra; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub unbracket { |
53
|
0
|
|
|
0
|
|
|
my $mvar = shift; |
54
|
0
|
|
|
|
|
|
return $mvar->[1]; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub http { |
58
|
0
|
|
|
0
|
|
|
my $mvar = shift; |
59
|
0
|
|
|
|
|
|
my $url = $mvar->[0]; |
60
|
0
|
|
|
|
|
|
return sprintf('%s', $url, $url); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub http_image { |
64
|
0
|
|
|
0
|
|
|
my $mvar = shift; |
65
|
0
|
|
|
|
|
|
my $url = $mvar->[1]; |
66
|
0
|
|
|
|
|
|
my $size = ''; |
67
|
0
|
0
|
0
|
|
|
|
if ($mvar->[2] && $mvar->[2] =~ /^:([hw])(\d+)$/o) { |
68
|
0
|
0
|
|
|
|
|
my $hw = $1 eq 'h' ? 'height' : 'width'; |
69
|
0
|
|
|
|
|
|
$size = sprintf(qq|$hw="%d" |, $2); |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
return sprintf('', |
72
|
|
|
|
|
|
|
$url, $url, $url, $size); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub http_title { |
76
|
0
|
|
|
0
|
|
|
my $mvar = shift; |
77
|
0
|
|
|
|
|
|
my $url = $mvar->[1]; |
78
|
0
|
|
|
|
|
|
my $title = $mvar->[2]; |
79
|
0
|
|
|
|
|
|
return sprintf('%s', $url, $title); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub mailto { |
83
|
0
|
|
|
0
|
|
|
my $mvar = shift; |
84
|
0
|
|
|
|
|
|
my $addr = $mvar->[1]; |
85
|
0
|
|
|
|
|
|
return sprintf('mailto:%s', $addr, $addr); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |