| 1 | package DTL::Fast::Filter::Urlize; | ||||||||||||
| 2 | 3 | ▶ | use strict; use utf8; use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 3 | ▶ | use parent 'DTL::Fast::Filter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 3 | $DTL::Fast::FILTER_HANDLERS{'urlize'} = __PACKAGE__; | |||||||||||
| 6 | |||||||||||||
| 7 | 3 | ▶ | use DTL::Fast::Utils qw(unescape); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 3 | our $DOMAINS_RE = qr/com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|ru|ÑÑ|[a-z]{2,3}/; | |||||||||||
| 10 | |||||||||||||
| 11 | 3 | our $URL_RE = qr | |||||||||||
| 12 | { | ||||||||||||
| 13 | (?:(?:http|ftp|https)\://)? # protocol | ||||||||||||
| 14 | (?:\w+\:\w+\@)? # username and password | ||||||||||||
| 15 | (?:(?:www|ftp)\.)? # domain prefixes | ||||||||||||
| 16 | (?:[-\w]+\.)+ # domain name | ||||||||||||
| 17 | (?:$DOMAINS_RE) # top level domain | ||||||||||||
| 18 | (?:\:\d{1,5})? # port number | ||||||||||||
| 19 | (?:/[-\w~._]*)*? # directories and files | ||||||||||||
| 20 | (?:\?[^\s#]+?)? # query string no spaces or sharp | ||||||||||||
| 21 | (?:\#[^\s]+?)? # anchor | ||||||||||||
| 22 | }x; | ||||||||||||
| 23 | |||||||||||||
| 24 | 3 | our $EMAIL_RE = qr | |||||||||||
| 25 | { | ||||||||||||
| 26 | (?:[-\w_~\.]+\@) # user name | ||||||||||||
| 27 | (?:[-\w]+\.)+ # domain name | ||||||||||||
| 28 | (?:$DOMAINS_RE) # top level domain | ||||||||||||
| 29 | }x; | ||||||||||||
| 30 | |||||||||||||
| 31 | #@Override | ||||||||||||
| 32 | sub filter | ||||||||||||
| 33 | { | ||||||||||||
| 34 | 18 | ▶ | my $self = shift; # self | ||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 35 | 18 | my $filter_manager = shift; # filter_manager | |||||||||||
| 36 | 18 | my $value = shift; # value | |||||||||||
| 37 | 18 | shift; #context | |||||||||||
| 38 | |||||||||||||
| 39 | 18 | $value =~ s | |||||||||||
| 40 | { | ||||||||||||
| 41 | (?:($EMAIL_RE)|($URL_RE)) | ||||||||||||
| 42 | ($|\s|\.|,|!) # after link | ||||||||||||
| 43 | }{ | ||||||||||||
| 44 | 20 | $self->wrap_email($1,$3).$self->wrap_url($2,$3) | |||||||||||
| 45 | }gxesi; | ||||||||||||
| 46 | |||||||||||||
| 47 | 18 | $filter_manager->{'safe'} = 1; | |||||||||||
| 48 | |||||||||||||
| 49 | 18 | return $value; | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | sub wrap_url | ||||||||||||
| 53 | { | ||||||||||||
| 54 | 20 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
wrap_url: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 55 | 20 | my $text = shift; | |||||||||||
| 56 | 20 | ▶ | return '' if not $text; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 18 | ▶ | my $appendix = shift // ''; | ||||||||||
|
Condition100%2/2
shift() // ""
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 58 | |||||||||||||
| 59 | 18 | my $uri = $text; | |||||||||||
| 60 | 18 | ▶ | $uri = 'http://'.$uri if | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | $uri !~ m{^(http|ftp|https)://}i; | ||||||||||||
| 62 | |||||||||||||
| 63 | 18 | ▶ | return sprintf '<a href="%s" rel="nofollow">%s</a>%s' | ||||||||||
|
Condition50%1/2
$uri // "undef"
MC/DC0%0/1
$uri // "undef"
$uri
Truth table50%1/2
A
A= $uri
| |||||||||||||
| 64 | , $uri // 'undef' | ||||||||||||
| 65 | , DTL::Fast::html_protect(unescape($self->normalize_text($text))) | ||||||||||||
| 66 | , $appendix | ||||||||||||
| 67 | ; | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | sub wrap_email | ||||||||||||
| 71 | { | ||||||||||||
| 72 | 20 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
wrap_email: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 73 | 20 | my $text = shift; | |||||||||||
| 74 | 20 | ▶ | return '' if not $text; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 2 | ▶ | my $appendix = shift // ''; | ||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 76 | |||||||||||||
| 77 | 2 | my $uri = $text; | |||||||||||
| 78 | |||||||||||||
| 79 | 2 | return sprintf '<a href="mailto:%s" rel="nofollow">%s</a>%s' | |||||||||||
| 80 | , $uri | ||||||||||||
| 81 | , DTL::Fast::html_protect(unescape($self->normalize_text($text))) | ||||||||||||
| 82 | , $appendix | ||||||||||||
| 83 | ; | ||||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | sub normalize_text | ||||||||||||
| 87 | { | ||||||||||||
| 88 | 10 | ▶ | shift; | ||||||||||
|
Subroutine
normalize_text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 89 | 10 | return shift; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 3 | 1; | |||||||||||