Muster/Hook/Map.pm

filter: Statementstmt 24.7% 29 / 117 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 17.1% 41 / 239 CC 42 SCAR 71.2
CC
42
Cov
14%
process
1722.0
page_depth
6.0
register
2.0
CRAP 1239.2
« Muster/LeafFile/EXIF.pm ↑ Summary Muster/Hook/DeriveFields.pm »
1 package Muster::Hook::Map;
2 1 $Muster::Hook::Map::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::Map - Muster map and pmap directive.
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 DESCRIPTION
12  
13 L<Muster::Hook::Map> creates two directives, map and pmap (pretty map),
14 used to make lists of pages.
15  
16 =cut
17  
18 1 ▶use Mojo::Base 'Muster::Hook::Directives';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Muster::Hook::Links;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use HTML::LinkList;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 METHODS
30  
31 L<Muster::Hook::Map> inherits all methods from L<Muster::Hook::Directives>.
32  
33 =head2 register
34  
35 Do some intialization.
36  
37 =cut
38 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
39 0 my $self = shift;
40 0 my $hookmaster = shift;
41 0 my $conf = shift;
42  
43 0 $self->{metadb} = $hookmaster->{metadb};
44  
45 0 ▶ $hookmaster->add_hook('map' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 my %args = @_;
47  
48 return $self->do_directives(
49 no_scan=>1,
50 directive=>'map',
51 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 my %args2 = @_;
53  
54 0 return $self->process(directive=>'map',%args2);
55 },
56 0 %args,
57 );
58 },
59 0 );
60 0 ▶ $hookmaster->add_hook('pmap' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my %args = @_;
62  
63 return $self->do_directives(
64 no_scan=>1,
65 directive=>'pmap',
66 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 my %args2 = @_;
68  
69 0 return $self->process(directive=>'pmap',%args2);
70 },
71 0 %args,
72 );
73 },
74 0 );
75 0 return $self;
76 } # register
77  
78 =head2 process
79  
80 Process maps.
81 If there are no matching pages, returns an empty string.
82  
83 =cut
84 0 ▶sub process {
Subroutine process: not called CC=41 SCAR=74.5
Pod documented
85 0 my $self = shift;
86 0 my %args = @_;
87  
88 0 my $directive = $args{directive};
89 0 my $leaf = $args{leaf};
90 0 my $phase = $args{phase};
91 0 my @p = @{$args{params}};
92 0 my %params = @p;
93 0 my $pagename = $leaf->pagename;
94 0 my $show = $params{show};
95 0 delete $params{show};
96 0 ▶ my $map_type = (defined $params{map_type} ? $params{map_type} : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'map_type'} ? : 0 0 0
97  
98 0 ▶ if (! exists $params{pages}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"pages"} or exists $params{"pagenames"} or exists $params{"titles"} or exists $params{"where"}) 0 0 0
Condition0%0/9
exists $params{"pages"} or exists $params{"pagenames"} or exists $params{"titles"} or exists $params{"where"}
l!l&&r!l&&!r
000
exists $params{"pages"} or exists $params{"pagenames"} or exists $params{"titles"}
l!l&&r!l&&!r
000
exists $params{"pages"} or exists $params{"pagenames"}
l!l&&r!l&&!r
000
MC/DC0%0/4
exists $params{"pages"} or exists $params{"pagenames"} or exists $params{"titles"} or exists $params{"where"}
exists $params{"pages"} exists $params{"pagenames"} exists $params{"titles"} exists $params{"where"}
Truth table0%0/5
A or B or C or D
A= exists $params{"pages"}B= exists $params{"pagenames"}C= exists $params{"titles"}D= exists $params{"where"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
99 and ! exists $params{pagenames}
100 and ! exists $params{titles}
101 and ! exists $params{where})
102 {
103 0 return "ERROR: missing pages/pagenames/where parameter";
104 }
105 0 ▶ if ($phase eq $Muster::Hooks::PHASE_SCAN)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq $Muster::Hooks::PHASE_SCAN) 0 0 0
106 {
107 0 return "";
108 }
109  
110 0 my @matching_pages = ();
111 0 ▶ if (exists $params{pages})
Branch: 4 branches
BranchTrueFalseTotal
if (exists $params{'pages'}) { } 0 0 0
elsif (exists $params{'titles'} and exists $params{'relto'}) { } 0 0 0
elsif (exists $params{'pagenames'}) { } 0 0 0
elsif (exists $params{'where'}) { } 0 0 0
112 {
113 0 my $pages = $self->{metadb}->query_pagespec($params{pages});
114 0 ▶ @matching_pages = @{$pages} if $pages;
Branch: 1 branch
BranchTrueFalseTotal
if $pages 0 0 0
115 }
116 ▶ elsif (exists $params{titles} and exists $params{relto})
Condition0%0/3
exists $params{"titles"} and exists $params{"relto"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params{"titles"} and exists $params{"relto"}
exists $params{"titles"} exists $params{"relto"}
Truth table0%0/3
A and B
A= exists $params{"titles"}B= exists $params{"relto"}
ABresult
0X0
100
111
117 {
118 # titles are separated by | and may have spaces which should be _
119 0 $params{titles} =~ s/ /_/g;
120 @matching_pages =
121 0 map { $self->{metadb}->bestlink($params{relto}, $_) } split /\|/, $params{titles};
122 }
123 elsif (exists $params{pagenames})
124 {
125 @matching_pages =
126 0 map { $self->{metadb}->bestlink($pagename, $_) } split ' ', $params{pagenames};
127 }
128 elsif (exists $params{where})
129 {
130 0 my $pages = $self->{metadb}->query("SELECT page FROM flatfields WHERE " . $params{where});
131 0 ▶ @matching_pages = @{$pages} if $pages;
Branch: 1 branch
BranchTrueFalseTotal
if $pages 0 0 0
132 }
133 0 ▶ if (!scalar @matching_pages)
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @matching_pages) 0 0 0
134 {
135 0 return "";
136 }
137  
138 0 my $result = '';
139 0 ▶ if ($directive eq 'map')
Branch: 1 branch
BranchTrueFalseTotal
if ($directive eq 'map') { } 0 0 0
140 {
141 0 my %labels = ();
142 0 my @urls = ();
143 0 foreach my $pn (@matching_pages)
144 {
145 0 $labels{$pn} = basename($pn);
146 # top-level index page is treated differently
147 0 ▶ my $relpage = ($leaf->pagename eq 'index'
Branch: 1 branch
BranchTrueFalseTotal
$leaf->pagename eq 'index' ? : 0 0 0
148 ? $pn
149 : File::Spec->abs2rel($pn, $leaf->pagename));
150 0 push @urls, $relpage;
151 }
152 0 $result = HTML::LinkList::link_list(
153 urls=>\@urls,
154 labels=>\%labels,
155 );
156 }
157 else # pmap
158 {
159 0 my @link_list = ();
160 0 my %page_labels = ();
161 0 my %page_desc = ();
162 my $count = ($params{count}
163 ? ($params{count} < @matching_pages
164 ? $params{count}
165 : scalar @matching_pages
166 0 ▶ )
Branch: 2 branches
BranchTrueFalseTotal
$params{'count'} ? : 0 0 0
$params{'count'} < @matching_pages ? : 0 0 0
167 : scalar @matching_pages);
168 0 my $min_depth = 100;
169 0 my $max_depth = 0;
170 0 ▶ for (my $i=0; $i < $count; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
171 {
172 0 my $page = $matching_pages[$i];
173 0 my $page_info = $self->{metadb}->page_or_file_info($page);
174 0 my $pd = page_depth($page);
175 0 ▶ if ($pd < $min_depth)
Branch: 1 branch
BranchTrueFalseTotal
if ($pd < $min_depth) 0 0 0
176 {
177 0 $min_depth = $pd;
178 }
179 0 ▶ if ($pd > $max_depth)
Branch: 1 branch
BranchTrueFalseTotal
if ($pd > $max_depth) 0 0 0
180 {
181 0 $max_depth = $pd;
182 }
183 0 ▶ my $urlto = ($leaf->pagename eq 'index'
Branch: 1 branch
BranchTrueFalseTotal
$leaf->pagename eq 'index' ? : 0 0 0
184 ? $page
185 : File::Spec->abs2rel($page, $leaf->pagename));
186 0 push @link_list, $urlto;
187  
188 0 ▶ if (defined $show)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $show) 0 0 0
189 {
190 0 ▶ if (defined $show
Branch: 1 branch
BranchTrueFalseTotal
if (defined $show and defined $page_info and $show =~ /title/ou) 0 0 0
Condition0%0/6
defined $show and defined $page_info and $show =~ /title/ou
!ll&&!rl&&r
000
defined $show and defined $page_info
!ll&&!rl&&r
000
MC/DC0%0/3
defined $show and defined $page_info and $show =~ /title/ou
defined $show defined $page_info $show =~ /title/ou
Truth table0%0/4
A and B and C
A= defined $showB= defined $page_infoC= $show =~ /title/ou
ABCresult
0XX0
10X0
1100
1111
191 and defined $page_info
192 and $show =~ /title/o)
193 {
194 0 $page_labels{$urlto}=$page_info->{title};
195 0 $page_labels{$urlto} =~ s/ & / &amp; /go;
196 }
197 0 ▶ if (defined $show
Branch: 1 branch
BranchTrueFalseTotal
if (defined $show and defined $page_info and $show =~ /desc/ou) 0 0 0
Condition0%0/6
defined $show and defined $page_info and $show =~ /desc/ou
!ll&&!rl&&r
000
defined $show and defined $page_info
!ll&&!rl&&r
000
MC/DC0%0/3
defined $show and defined $page_info and $show =~ /desc/ou
defined $show defined $page_info $show =~ /desc/ou
Truth table0%0/4
A and B and C
A= defined $showB= defined $page_infoC= $show =~ /desc/ou
ABCresult
0XX0
10X0
1100
1111
198 and defined $page_info
199 and $show =~ /desc/o)
200 {
201 0 ▶ if ($page_info->{description})
Branch: 1 branch
BranchTrueFalseTotal
if ($page_info->{"description"}) 0 0 0
202 {
203 0 $page_desc{$urlto}=$page_info->{description};
204 }
205 }
206 }
207 } # for the pages
208  
209 0 my $current_url = "/$pagename/";
210  
211 # if all the pages are at the same depth, and the map_type is
212 # not set, then set the map_type to 'list'
213 0 ▶ $map_type = 'list' if (!$map_type and $min_depth == $max_depth);
Branch: 1 branch
BranchTrueFalseTotal
if not $map_type and $min_depth == $max_depth 0 0 0
Condition0%0/3
not $map_type and $min_depth == $max_depth
!ll&&!rl&&r
000
MC/DC0%0/2
not $map_type and $min_depth == $max_depth
not $map_type $min_depth == $max_depth
Truth table0%0/3
not(A) and B
A= not $map_typeB= $min_depth == $max_depth
ABresult
0X0
100
111
214  
215 0 ▶ my $tree = ($map_type eq 'nav'
Branch: 3 branches
BranchTrueFalseTotal
$map_type eq 'nav' ? : 0 0 0
$map_type eq 'breadcrumb' ? : 0 0 0
$map_type eq 'list' ? : 0 0 0
216 ? HTML::LinkList::nav_tree(paths=>\@link_list,
217 preserve_paths=>1,
218 labels=>\%page_labels,
219 descriptions=>\%page_desc,
220 current_url=> $current_url,
221 %params)
222 : ($map_type eq 'breadcrumb'
223 ? HTML::LinkList::breadcrumb_trail(current_url=>$current_url,
224 labels=>\%page_labels)
225 : ($map_type eq 'list'
226 ? HTML::LinkList::link_list(urls=>\@link_list,
227 labels=>\%page_labels,
228 descriptions=>\%page_desc,
229 current_url => $current_url,
230 %params)
231 : HTML::LinkList::full_tree(paths=>\@link_list,
232 preserve_paths=>1,
233 labels=>\%page_labels,
234 descriptions=>\%page_desc,
235 current_url => $current_url,
236 %params)
237 )));
238  
239 $result = ($params{no_div}
240 ? $tree
241 : ($map_type eq 'nav'
242 ? "<nav>$tree</nav>\n"
243 : ($params{class}
244 0 ▶ ? "<div class='$params{class}'>$tree</div>\n"
Branch: 3 branches
BranchTrueFalseTotal
$params{'no_div'} ? : 0 0 0
$map_type eq 'nav' ? : 0 0 0
$params{'class'} ? : 0 0 0
245 : "<div class='map'>$tree</div>\n")
246 )
247 );
248 } # else pmap
249  
250 0 ▶ if ($params{prepend})
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"prepend"}) 0 0 0
251 {
252 0 $result = $params{prepend} . $result;
253 }
254 0 return $result;
255 } # process
256  
257 =head2 page_depth
258  
259 Calculate the page depth of the given page.
260  
261 =cut
262 0 ▶sub page_depth {
Subroutine page_depth: not called CC=2 SCAR=17.9
Pod documented
263 0 my $page = shift;
264  
265 0 ▶ return 0 if ($page eq 'index'); # root is zero
Branch: 1 branch
BranchTrueFalseTotal
if $page eq "index" 0 0 0
266 0 return scalar ($page =~ tr!/!/!) + 1;
267 } # page_depth
268 1 1;
« Muster/LeafFile/EXIF.pm ↑ Summary Muster/Hook/DeriveFields.pm »