blib/lib/App/Greple/md/Image.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 69.2% 18 / 26 Conditioncond 51.8% 14 / 27 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 10 / 10 Podpod 33.3% 1 / 3 totaltotal 73.2% 104 / 142 CC 25 SCAR 24.5
CC
25
Cov
80%
local_path
19.0
git_top
9.4
remote_urls
4.1
CRAP 11.5
« blib/lib/App/Greple/md/Blocks.pm ↑ Summary blib/lib/App/Greple/md.pm »
1 package App::Greple::md::Image;
2  
3 # The files of the images of a Markdown file. They are shown by
4 # Text::ANSI::Image; this module finds them.
5  
6 221 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 221 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 221 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 221 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 221 our @EXPORT_OK = qw(local_path);
12  
13 221 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 221 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 221 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # The local file of the image of a URL, or undef. Nothing is fetched
18 # from the network.
19 #
20 # images/a.png, /path/a.png
21 # a path, relative to the directory of the Markdown file
22 # https://raw.githubusercontent.com/OWNER/REPO/BRANCH/PATH
23 # https://github.com/OWNER/REPO/blob/BRANCH/PATH (or raw, ?raw=true)
24 # PATH in the work tree of the git repository which has the
25 # Markdown file, when one of its remotes is github.com/OWNER/REPO.
26 # The README files of CPAN distributions point to their images
27 # this way, for MetaCPAN. The branch is not looked at: the file
28 # is the one in the work tree.
29 24 ▶sub local_path {
Subroutine local_path: called CC=16 SCAR=29.4
Pod documented
30 24 my($url, $dir) = @_;
31 24 $dir = File::Spec->rel2abs($dir);
32 24 ▶ if ($url !~ m{^[a-z][a-z0-9+.-]*:}i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /^[a-z][a-z0-9+.-]*:/iu) 17 7 24
33 17 ▶ return $url =~ m{^/} ? $url : "$dir/$url";
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^/]u ? : 0 17 17
34 }
35 7 ▶ my($owner, $repo, $rest) =
Branch: 2 branches
BranchTrueFalseTotal
$url =~ m[^https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/(.+)]iu ? : 3 4 7
$url =~ m[^https://github\.com/([^/]+)/([^/]+)/(?:blob|raw)/(.+)]iu ? : 1 3 4
36 $url =~ m{^https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/(.+)}i ? ($1, $2, $3) :
37 $url =~ m{^https://github\.com/([^/]+)/([^/]+)/(?:blob|raw)/(.+)}i ? ($1, $2, $3) :
38 return;
39 4 $repo =~ s/\.git$//;
40 4 $rest =~ s/[?#].*//s;
41 4 ▶ my $top = git_top($dir) // return;
Condition50%1/2
git_top($dir) // (return)
l!l
40
MC/DC0%0/1
git_top($dir) // (return)
git_top($dir)
Truth table50%1/2
A
A= git_top($dir)
Aresult
11
00
42 4 ▶ grep { m{github\.com[:/]\Q$owner\E/\Q$repo\E(?:\.git)?/?$}i } remote_urls($top)
Branch: 1 branch
BranchTrueFalseTotal
grep {m[github\.com[:/]\Q$owner\E/\Q$repo\E(?:\.git)?/?$]iu;} remote_urls($top) or return 3 1 4
43 or return;
44 # BRANCH/PATH: the branch may have slashes, so the path is the
45 # longest tail that names a file in the work tree. No part of it
46 # may go up, and the file must be in the work tree.
47 0 my @part = map { s/%([0-9A-Fa-f]{2})/chr hex $1/ger } split m{/}, $rest, -1;
48 3 ▶ return if grep { $_ eq '' or $_ eq '.' or $_ eq '..' or m{/} } @part;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "" or $_ eq "." or $_ eq ".." or m[/]u;} @part 1 2 3
Condition55%5/9
$_ eq '' || $_ eq '.' || $_ eq '..' || m[/]u
l!l&&r!l&&!r
208
$_ eq "" or $_ eq "." or $_ eq ".."
l!l&&r!l&&!r
028
$_ eq "" or $_ eq "."
l!l&&r!l&&!r
0010
MC/DC25%1/4
$_ eq '' || $_ eq '.' || $_ eq '..' || m[/]u
$_ eq "" $_ eq "." $_ eq ".." m[/]u
Truth table40%2/5
A or B or C || D
A= $_ eq ""B= $_ eq "."C= $_ eq ".."D= m[/]u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
49 2 ▶ my $real_top = realpath($top) // return;
Condition50%1/2
realpath($top) // (return)
l!l
20
MC/DC0%0/1
realpath($top) // (return)
realpath($top)
Truth table50%1/2
A
A= realpath($top)
Aresult
11
00
50 2 for my $i (1 .. $#part) {
51 2 my $path = join '/', $top, @part[$i .. $#part];
52 2 ▶ -f $path or next;
Branch: 1 branch
BranchTrueFalseTotal
-f $path or next 2 0 2
53 2 ▶ my $real = realpath($path) // next;
Condition50%1/2
realpath($path) // (next)
l!l
20
MC/DC0%0/1
realpath($path) // (next)
realpath($path)
Truth table50%1/2
A
A= realpath($path)
Aresult
11
00
54 2 ▶ return $real if index($real, "$real_top/") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($real, "$real_top/") == 0 2 0 2
55 }
56 0 return;
57 }
58  
59 # The top of the work tree of the git repository which has the
60 # directory, found by its .git (a directory, or a file in a worktree).
61 4 ▶sub git_top {
Subroutine git_top: called CC=7 SCAR=22.4
Pod undocumented
62 4 my $dir = shift;
63 4 state %top;
64 4 ▶ $top{$dir} //= do {
Condition66%2/3
$top{$dir} //= do { my $d = $dir; $d = dirname($d) until -e "$d/.git" or $d eq "/" or $d eq "."; -e "$d/.git" ? $d : "" }
l!l&&r!l&&!r
310
MC/DC0%0/2
$top{$dir} //= do { my $d = $dir; $d = dirname($d) until -e "$d/.git" or $d eq "/" or $d eq "."; -e "$d/.git" ? $d : "" }
$top{$dir} do { my $d = $dir; $d = dirname($d) until -e "$d/.git" or $d eq "/" or $d eq "."; -e "$d/.git" ? $d : "" }
Truth table66%2/3
A //= B
A= $top{$dir}B= do { my $d = $dir; $d = dirname($d) until -e "$d/.git" or $d eq "/" or $d eq "."; -e "$d/.git" ? $d : "" }
ABresult
1X1
011
000
65 1 my $d = $dir;
66 1 ▶ $d = dirname($d) until -e "$d/.git" or $d eq '/' or $d eq '.';
Branch: 1 branch
BranchTrueFalseTotal
(-e "$d/.git" or $d eq "/" or $d eq ".") or do { $d = dirname($d) } 1 0 1
Condition33%2/6
-e "$d/.git" or $d eq "/" or $d eq "."
l!l&&r!l&&!r
100
-e "$d/.git" or $d eq "/"
l!l&&r!l&&!r
100
MC/DC0%0/3
-e "$d/.git" or $d eq "/" or $d eq "."
-e "$d/.git" $d eq "/" $d eq "."
Truth table25%1/4
A or B or C
A= -e "$d/.git"B= $d eq "/"C= $d eq "."
ABCresult
1XX1
01X1
0011
0000
67 1 ▶ -e "$d/.git" ? $d : '';
Branch: 1 branch
BranchTrueFalseTotal
-e "$d/.git" ? : 1 0 1
68 };
69 4 ▶ $top{$dir} || undef;
Branch: 1 branch
BranchTrueFalseTotal
$top{$dir} or undef 4 0 4
70 }
71  
72 # The URLs of the remotes of a repository, from git itself (no shell).
73 4 ▶sub remote_urls {
Subroutine remote_urls: called CC=4 SCAR=14.0
Pod undocumented
74 4 my $top = shift;
75 4 state %urls;
76 4 ▶ $urls{$top} //= do {
Condition66%2/3
$urls{$top} //= do { my @u; if (open my $fh, "-|", "git", "-C", $top, "config", "--get-regexp", "^remote\..*\.url$") { @u = map({/^\S+\s+(\S+)/u ? $1 : ();} readline $fh); close $fh; } ; \@u }
l!l&&r!l&&!r
310
MC/DC0%0/2
$urls{$top} //= do { my @u; if (open my $fh, "-|", "git", "-C", $top, "config", "--get-regexp", "^remote\..*\.url$") { @u = map({/^\S+\s+(\S+)/u ? $1 : ();} readline $fh); close $fh; } ; \@u }
$urls{$top} do { my @u; if (open my $fh, "-|", "git", "-C", $top, "config", "--get-regexp", "^remote\..*\.url$") { @u = map({/^\S+\s+(\S+)/u ? $1 : ();} readline $fh); close $fh; } ; \@u }
Truth table66%2/3
A //= B
A= $urls{$top}B= do { my @u; if (open my $fh, "-|", "git", "-C", $top, "config", "--get-regexp", "^remote\..*\.url$") { @u = map({/^\S+\s+(\S+)/u ? $1 : ();} readline $fh); close $fh; } ; \@u }
ABresult
1X1
011
000
77 1 my @u;
78 1 ▶ if (open my $fh, '-|', 'git', '-C', $top, 'config', '--get-regexp', '^remote\..*\.url$') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "-|", "git", "-C", $top, "config", "--get-regexp", "^remote\..*\.url$") 1 0 1
79 1 ▶ @u = map { /^\S+\s+(\S+)/ ? $1 : () } <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
/^\S+\s+(\S+)/u ? : 1 0 1
80 1 close $fh;
81 }
82 1 \@u;
83 };
84 4 @{$urls{$top}};
85 }
86  
87 221 1;
88  
89 __END__
« blib/lib/App/Greple/md/Blocks.pm ↑ Summary blib/lib/App/Greple/md.pm »