| 1 | # Copyright 2009, 2010, 2011, 2012, 2013 Kevin Ryde | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # This file is part of Distlinks. | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Distlinks is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # the terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # version. | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # Distlinks is distributed in the hope that it will be useful, but WITHOUT | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # more details. | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # with Distlinks. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | package App::Distlinks::URIterator; | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | #use Devel::Comments; | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | our $VERSION = 11; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # filename => | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # content => | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 8 | my ($class, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | ### URIterator: %options | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | return bless \%options, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub filename { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filename: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return $self->{'filename'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my %open_paren = ('}' => '{', | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | ')' => '(', | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ']' => '[', | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | '>' => '<'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | my $scheme = qr{(https?|s?ftp|news):/+[[:alnum:]]}; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my %barehost_table = (www => 'http://', | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | ftp => 'ftp://', | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | rsync => 'rsync://'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my $barehost_re = join('|', keys %barehost_table); | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my $scheme_or_host = qr/\b($scheme|(?<barehost>$barehost_re)\d*\.)/o; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | my $urlchar_not = '\\\\<>"\'! [:cntrl:]'; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | my $urlchar_last = qr/\\?[^]),.$urlchar_not]/; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my $urlchar = qr/\\?[^$urlchar_not]/; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # paired {} allowed like @uref{hello@comma{}world} | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | my $texinfo = qr<(?<texinfo>\@ur(ef|l)\{(?<url>([^\r\n,{}]|\{\})+)(,.*)?\})>o; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my $quoted = qr<`(?<url>$scheme_or_host[^' \t\r\n]*)'>o; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $angles = qr/<(?<url>$scheme_or_host[^> \t\r\n]*)>/o; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my $uangle = qr/<UR[IL]:(?<url>[[:alnum:]][^> \t\r\n]*)>/o; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | my $bare = qr{(?<url>$scheme$urlchar*$urlchar_last)}o; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | my $href = qr{(?<href>href=(\'(?<url>[^\'\n]*?)\'|\"(?<url>[^\"\n]*?)\"))}; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 17 | ▶ | sub next { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=13 SCAR=31.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 17 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | ### URIterator pos: pos($self->{'content'}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 17 | ▶ | ($self->{'content'} =~ m{$texinfo | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |$quoted | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |$angles | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |$uangle | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |$bare | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |$href | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | }ogx) | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | or return; | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | my $url = $+{'url'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | 9 | my $pos = $-[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 9 | my $base; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | ### match: substr($self->{'content'},$-[0],$+[0]-$-[0]) | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 9 | ▶ | if ($+{'texinfo'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | $url =~ s/\@comma\{\}/,/g; # my @comma macro | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | } elsif ($+{'href'}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $base = $self->{'base'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # C backslashes | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | $url =~ s/\\t/\t/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | $url =~ s/\\r/\r/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | $url =~ s/\\n/\n/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | # others literal, in particular \# in emacs ru-refcard.tex | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | $url =~ s/\\(.)/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # unclosed trailing paren | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 9 | ▶ | while ($url =~ /([])}>])$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (index($url,$open_paren{$1}) < 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | substr ($url, -1,1, ''); | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # disallow things with variable substitutions "$FOO", "$(FOO)" etc | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # $url =~ /\$$|\$[({]/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 9 | ▶ | if ($url =~ /\$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ### skip dollar subst: $url | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | my $url_raw = $url; | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | 9 | ▶ | if (my $barehost = $+{'barehost'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $url = $barehost_table{$barehost} . $url; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 9 | require URI; | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 9 | my $uri; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | ### $base | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 9 | ▶ | if (defined $base) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $uri = URI->new_abs ($url, $base); | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 9 | $uri = URI->new ($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 9 | ▶ | if (! $uri->scheme) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $url = "http://$url"; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 9 | ▶ | if (($uri->scheme eq 'http' || $uri->scheme eq 'ftp') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$uri->scheme eq "http" || $uri->scheme eq "ftp" and not defined $uri->host
$uri->scheme eq "http" or $uri->scheme eq "ftp"
MC/DC0%0/3
$uri->scheme eq "http" || $uri->scheme eq "ftp" and not defined $uri->host
$uri->scheme eq "http"
$uri->scheme eq "ftp"
not defined $uri->host
Truth table20%1/5
A or B and not(C)
A= $uri->scheme eq "http"B= $uri->scheme eq "ftp"C= not defined $uri->host
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | && ! defined $uri->host) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | ### skip no host part: $url | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 9 | return App::Distlinks::URIfound->new | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | (iterator => $self, | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | pos => $pos, | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | uri => $uri, | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | url_raw => $url_raw); | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | package App::Distlinks::URIfound; | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 9 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 9 | return bless { @args }, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub iterator { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
iterator: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return $self->{'iterator'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub filename { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filename: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | return $self->{'iterator'}->filename; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 9 | ▶ | sub uri { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uri: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 9 | return $self->{'uri'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub url_raw { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url_raw: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | return $self->{'url_raw'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub pos { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pos: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | return $self->{'pos'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub line_and_column { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
line_and_column: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | return @{$self->{'line_and_column'} | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | ||= [ _pos_to_line_and_column ($self->iterator->{'content'}, | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"line_and_column"} ||= [_pos_to_line_and_column($self->iterator->{"content"}, $self->pos)]
MC/DC0%0/1
$self->{"line_and_column"} ||= [_pos_to_line_and_column($self->iterator->{"content"}, $self->pos)]
$self->{"line_and_column"}
Truth table0%0/2
A
A= $self->{"line_and_column"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | $self->pos) ]}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub _pos_to_line_and_column { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pos_to_line_and_column: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my ($str, $pos) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | require Text::Tabs; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $str = substr ($str, 0, $pos); | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my $nlpos = rindex ($str, "\n"); | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | my $lastline = substr ($str, $nlpos+1); | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | $lastline = Text::Tabs::expand ($lastline); | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $colnum = 1 + length ($lastline); | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my $linenum = 1 + scalar($str =~ tr/\n//); | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | return ($linenum, $colnum); | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # # $url =~ s/\)[,.]?$//; # close paren | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # # $url =~ s/(\.[a-z]+)\.$/$1/; # full stop after suffix | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||