Noss/Util.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 83.3% 10 / 12 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 87.8% 65 / 74 CC 10 SCAR 15.1
CC
10
Cov
90%
resolve_url
7.3
dir
4.0
BEGIN
1.0
CRAP 4.5
« Noss/Timestamp.pm ↑ Summary Noss/BaseConfig.pm »
1 package WWW::Noss::Util;
2 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 our $VERSION = '2.05';
6  
7 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our @EXPORT_OK = qw(dir resolve_url);
9  
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶sub dir {
Subroutine dir: called CC=4 SCAR=13.9
Pod documented
13  
14 2 my ($dir, %param) = @_;
15 2 ▶ my $hidden = $param{ hidden } // 0;
Condition100%2/2
$param{"hidden"} // 0
l!l
11
MC/DC100%1/1
$param{"hidden"} // 0
$param{"hidden"}
Truth table100%2/2
A
A= $param{"hidden"}
Aresult
11
00
16  
17 2 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Failed to open $dir as a directory: $!\n" 2 0 2
18 or die "Failed to open $dir as a directory: $!\n";
19 2 my @f = sort grep { ! /^\.\.?$/ } readdir $dh;
20 2 closedir $dh;
21  
22 2 ▶ unless ($hidden) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hidden) 1 1 2
23 1 @f = grep { ! /^\./ } @f;
24 }
25  
26 2 return map { File::Spec->catfile($dir, $_) } @f;
27  
28 }
29  
30 9 ▶sub resolve_url {
Subroutine resolve_url: called CC=7 SCAR=19.9
Pod documented
31  
32 9 my ($url, $from) = @_;
33  
34 9 my ($proto, $root, $path) = $from =~ /^(\w+:\/\/)?([^\/]+)(.*)$/;
35 9 ▶ $proto //= '';
Condition50%1/2
$proto //= ""
l!l
90
MC/DC0%0/1
$proto //= ""
$proto
Truth table50%1/2
A
A= $proto
Aresult
11
00
36  
37 9 ▶ if ($url =~ /^\w+:\/\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^\w+://]u) 5 4 9
38 5 return $url;
39 }
40  
41 4 ▶ if ($proto eq 'shell://' or $proto eq 'file://') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "shell://" or $proto eq "file://") 0 4 4
Condition33%1/3
$proto eq "shell://" or $proto eq "file://"
l!l&&r!l&&!r
004
MC/DC0%0/2
$proto eq "shell://" or $proto eq "file://"
$proto eq "shell://" $proto eq "file://"
Truth table33%1/3
A or B
A= $proto eq "shell://"B= $proto eq "file://"
ABresult
1X1
011
000
42 0 return undef;
43 }
44  
45 4 ▶ if ($url =~ /^\/\//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($url =~ m[^//]u) { } 1 3 4
elsif ($url =~ m[^/]u) { } 1 2 3
46 1 $url =~ s/^\/\///;
47 1 return $proto . $url;
48 } elsif ($url =~ /^\//) {
49 1 return $proto . $root . $url;
50 } else {
51 2 $url =~ s/^\.\/+//;
52 2 $root =~ s/\/+[^\/]*$//;
53 2 return $proto . $root . '/' . $url;
54 }
55  
56 }
57  
58 3 1;
59  
60 =head1 NAME
61  
62 WWW::Noss::Util - Misc. utility functions for noss
63  
64 =head1 USAGE
65  
66 use WWW::Noss::Util qw(dir resolve_url);
67  
68 my @files = dir('/');
69  
70 my $full_url = resolve_url('/pages', 'https://example.com/home');
71  
72 =head1 DESCRIPTION
73  
74 B<WWW::Noss::Util> is a module that provides various utility functions for
75 L<noss>. This is a private module, please consult the L<noss> manual for user
76 documentation.
77  
78 =head1 SUBROUTINES
79  
80 Subroutines are not exported automatically.
81  
82 =over 4
83  
84 =item @children = dir($dir, [ %param ])
85  
86 Returns list of children files under directory C<$dir>. C<%param> is an
87 optional hash of additional parameters.
88  
89 The following are valid fields in C<%param>:
90  
91 =over 2
92  
93 =item hidden
94  
95 Boolean determining whether to include hidden files or not. Defaults to false.
96  
97 =back
98  
99 =item $full_url = resolve_url($url, $from)
100  
101 Resolves URL C<$url> found on the page linked by C<$from>. Retuns C<undef> if
102 the URL could not be resolved.
103  
104 =back
105  
106 =head1 AUTHOR
107  
108 Written by Samuel Young, E<lt>samyoung@posteo.comE<gt>.
109  
110 This project's source can be found on its
111 L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
112 requests are welcome!
113  
114 =head1 COPYRIGHT
115  
116 Copyright (C) 2025-2026 Samuel Young
117  
118 This program is free software: you can redistribute it and/or modify
119 it under the terms of the GNU General Public License as published by
120 the Free Software Foundation, either version 3 of the License, or
121 (at your option) any later version.
122  
123 =head1 SEE ALSO
124  
125 L<noss>
126  
127 =cut
128  
129 # vim: expandtab shiftwidth=4
« Noss/Timestamp.pm ↑ Summary Noss/BaseConfig.pm »