| 1 | package Text::Hatena::AutoLink; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Regexp::Assemble; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use base qw(Class::Data::Inheritable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 25 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 26 | 0 | my $text = shift; | |||||||||||||||||||||||||||
| 27 | 0 | my $html = ''; | |||||||||||||||||||||||||||
| 28 | 0 | my $ra = $class->ra; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | while ($text && $ra->match($text)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$text and $ra->match($text)
MC/DC0%0/2
$text and $ra->match($text)
$text
$ra->match($text)
Truth table0%0/3
A and B
A= $textB= $ra->match($text)
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $html .= substr($text, 0, $ra->mbegin->[0]) if $ra->mbegin->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $handler = $class->syntax->{$ra->matched} || $syntax->{$ra->matched}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$class->syntax->{$ra->matched} || $$syntax{$ra->matched}
MC/DC0%0/2
$class->syntax->{$ra->matched} || $$syntax{$ra->matched}
$class->syntax->{$ra->matched}
$$syntax{$ra->matched}
Truth table0%0/3
A || B
A= $class->syntax->{$ra->matched}B= $$syntax{$ra->matched}
| |||||||||||||||||||||||||||||
| 32 | 0 | $html .= $handler->($ra->mvar()); | |||||||||||||||||||||||||||
| 33 | 0 | $text = substr($text, $ra->mend->[0]); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $html .= $text if $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | return $html; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub ra { | ||||||||||||||||||||||||||
|
Subroutine
ra: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 40 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | unless ($ra) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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}); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | return $ra; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub unbracket { | ||||||||||||||||||||||||||
|
Subroutine
unbracket: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 53 | 0 | my $mvar = shift; | |||||||||||||||||||||||||||
| 54 | 0 | return $mvar->[1]; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub http { | ||||||||||||||||||||||||||
|
Subroutine
http: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | my $mvar = shift; | |||||||||||||||||||||||||||
| 59 | 0 | my $url = $mvar->[0]; | |||||||||||||||||||||||||||
| 60 | 0 | return sprintf('<a href="%s">%s</a>', $url, $url); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub http_image { | ||||||||||||||||||||||||||
|
Subroutine
http_image: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 64 | 0 | my $mvar = shift; | |||||||||||||||||||||||||||
| 65 | 0 | my $url = $mvar->[1]; | |||||||||||||||||||||||||||
| 66 | 0 | my $size = ''; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($mvar->[2] && $mvar->[2] =~ /^:([hw])(\d+)$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mvar->[2] and $mvar->[2] =~ /^:([hw])(\d+)$/o
MC/DC0%0/2
$mvar->[2] and $mvar->[2] =~ /^:([hw])(\d+)$/o
$mvar->[2]
$mvar->[2] =~ /^:([hw])(\d+)$/o
Truth table0%0/3
A and B
A= $mvar->[2]B= $mvar->[2] =~ /^:([hw])(\d+)$/o
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $hw = $1 eq 'h' ? 'height' : 'width'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $size = sprintf(qq|$hw="%d" |, $2); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | return sprintf('<a href="%s"><img src="%s" alt="%s" %s/></a>', | |||||||||||||||||||||||||||
| 72 | $url, $url, $url, $size); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub http_title { | ||||||||||||||||||||||||||
|
Subroutine
http_title: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 0 | my $mvar = shift; | |||||||||||||||||||||||||||
| 77 | 0 | my $url = $mvar->[1]; | |||||||||||||||||||||||||||
| 78 | 0 | my $title = $mvar->[2]; | |||||||||||||||||||||||||||
| 79 | 0 | return sprintf('<a href="%s">%s</a>', $url, $title); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub mailto { | ||||||||||||||||||||||||||
|
Subroutine
mailto: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 0 | my $mvar = shift; | |||||||||||||||||||||||||||
| 84 | 0 | my $addr = $mvar->[1]; | |||||||||||||||||||||||||||
| 85 | 0 | return sprintf('<a href="mailto:%s">mailto:%s</a>', $addr, $addr); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||