.htmlgen/FunctionalPerl/Htmlgen/Nav.pm

filter: Statementstmt 78.6% 103 / 131 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 68.8% 31 / 45 Podpod 0.0% 0 / 2 totaltotal 67.0% 134 / 200 CC 9 SCAR 12.6
CC
9
Cov
69%
__ANON__
20.0
path0_to_sortkey
6.0
__ANON__
6.0
CRAP 3.5
« lib/PXML/Element.pm ↑ Summary lib/Chj/xopendir.pm »
1 #
2 # Copyright (c) 2015-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FunctionalPerl::Htmlgen::Nav -- configurable navigation bar data structure
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18  
19 =head1 NOTE
20  
21 This is alpha software! Read the status section in the package README
22 or on the L<website|http://functional-perl.org/>.
23  
24 =cut
25  
26 package FunctionalPerl::Htmlgen::Nav;
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 our @EXPORT = qw();
34 2 our @EXPORT_OK = qw(_nav entry);
35 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
36  
37 # Constructors
38  
39 1 ▶sub _nav ($items, $nav_bar) {
Subroutine _nav: called CC=1 SCAR=0.0
40 1 my $nav
41 = FunctionalPerl::Htmlgen::Nav::TopEntry->new($items, $nav_bar, undef);
42 1 $nav->index_set(nav_index($nav))
43 }
44  
45 21 ▶sub entry ($path0, @subentries) {
Subroutine entry: called CC=1 SCAR=0.0
Pod undocumented
46 21 FunctionalPerl::Htmlgen::Nav::RealEntry->new_(
47 path0 => $path0,
48 subentries => list(@subentries)
49 );
50 }
51  
52 # Classes
53  
54 2 package FunctionalPerl::Htmlgen::Nav::Entry {
55 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶ use FP::List qw(list_of); # should that be re-exported by
Subroutine BEGIN: called CC=1 SCAR=0.0
57 # FP::Predicates?
58 2 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶ use FP::fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶ use FP::Equal qw(equal);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
64 [
65 2 list_of(instance_of "FunctionalPerl::Htmlgen::Nav::Entry"),
66 "subentries"
67 ]
68 2 ];
69  
70 0 ▶ sub subentries_of_subentries($self) {
Subroutine subentries_of_subentries: not called CC=1 SCAR=6.9
71 0 $self->subentries->map(the_method "subentries")
72 }
73  
74 # An FP::List of all nav levels, including level 0, but unlike the
75 # result of nav_bar_level0, level 0 here is directly representing the
76 # top level of the {nav} config value (unknown pages are dropped, and
77 # non-existing pages show up). Feel free to drop it and replace with
78 # nav_bar_level0.
79  
80 0 ▶ sub nav_bar_levels ($self, $viewed_at_item, $upitems) {
Subroutine nav_bar_levels: not called CC=1 SCAR=6.9
81 fix(
82 0 ▶ sub ($rec, $nav, $downitems, $upitem) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
83  
84 # only show subnavs until the given location
85 # ($viewed_at_item), and then one more (to go down)?
86 0 ▶ if (my ($item, $rest) = $downitems->perhaps_first_and_rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $downitems->perhaps_first_and_rest) { } 0 0 0
87 0 my $entries = $nav->subentries;
88 0 ▶ my $active = $entries->filter(sub { $_[0] eq $item })->xone;
Subroutine __ANON__: not called CC=1 SCAR=6.9
89  
90 0 cons($self->nav_bar->($entries, $item, $viewed_at_item),
91 &$rec($active, $rest, $item))
92 } else {
93  
94 # the lowest level of the navigation stack: if the
95 # current item ($upitem, should also be the
96 # $viewed_at_item) has subentries, then show that
97 # (immediate) level as the last one. XX: this
98 # would better be shown in a different way; use
99 # (CSS based) mouse over submenu popups
100 # (advantage: show them on ~all the other items,
101 # too)?
102 0 ▶ if (is_pair(my $es = $upitem->subentries)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair(my $es = $upitem->subentries)) { } 0 0 0
103 0 ▶ equal($upitem, $viewed_at_item) or die "bug?";
Branch: 1 branch
BranchTrueFalseTotal
equal($upitem, $viewed_at_item) or die "bug?" 0 0 0
104  
105 # hack: passing $upitem as the item here (*no*
106 # item should be marked as selected, this
107 # fulfills the purpose)
108 0 cons($self->nav_bar->($es, $upitem, $viewed_at_item),
109 null)
110 } else {
111 0 null
112 }
113 }
114 }
115 0 )->($self, $upitems->reverse, undef)
116  
117 # an empty upitems list would be at odds with the given
118 # undef $upitem, but that doesn't happen.
119 }
120  
121 2 _END_
122 }
123  
124 2 package FunctionalPerl::Htmlgen::Nav::TopEntry {
125 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
126 2 ▶ use FP::Ops qw(the_method string_cmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
127 2 ▶ use FP::Combinators qw(compose);
Subroutine BEGIN: called CC=1 SCAR=0.0
128 2 ▶ use FP::Array_sort;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 2 ▶ use FP::HashSet qw(hashset_to_predicate
Subroutine BEGIN: called CC=1 SCAR=0.0
130 2 array_to_hashset);
131  
132 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
133 2 [\&is_procedure, "nav_bar"],
134 [maybe(instance_of "FunctionalPerl::Htmlgen::Nav::Index"), "index"]
135 ],
136 2 "FunctionalPerl::Htmlgen::Nav::Entry";
137  
138 0 ▶ sub FP_Show_show ($self, $show) {
Subroutine FP_Show_show: not called CC=1 SCAR=6.9
139 0 ("nav(" . $self->subentries->map($show)->strings_join(", ") . ")")
140 }
141  
142 # nav level 0: this is special since it does not just
143 # show what the navigation defines, but show all
144 # pages that are not in deeper levels
145  
146 0 ▶ sub item_is_in_lower_hierarchy($self) {
Subroutine item_is_in_lower_hierarchy: not called CC=1 SCAR=6.9
147 0 compose(
148 hashset_to_predicate(
149 array_to_hashset(
150 $self->subentries_of_subentries->flatten->map(
151 the_method "path0"
152 )->array
153 )
154 ),
155 the_method "path0"
156 )
157 }
158  
159 # In the toplevel of the nav hierarchy, still also show pages that are
160 # missing in the nav declaration; thus, use the nav declaration to
161 # *order* the pages instead:
162 0 ▶ sub path0_to_sortkey($self) {
Subroutine path0_to_sortkey: not called CC=2 SCAR=17.9
163 0 my $sortprio = do {
164 0 my $i = 1;
165 +{
166 map {
167 0 my $file = $_;
168 0 ▶ $file .= ".md" unless /\.\w{1,7}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.\w{1,7}\z/ 0 0 0
169 0 $file => sprintf('-%04d', $i++)
170 } $self->subentries->map(the_method "path0")->values
171 }
172 };
173 0 ▶ sub($path0) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
174 0 ▶ $$sortprio{$path0} || $path0
Branch: 1 branch
BranchTrueFalseTotal
$sortprio->{$path0} or $path0 0 0 0
175 }
176 0 }
177  
178 0 ▶ sub path0_navigation_cmp($self) {
Subroutine path0_navigation_cmp: not called CC=1 SCAR=6.9
179 0 on $self->path0_to_sortkey, \&string_cmp
180 }
181  
182 0 ▶ sub nav_bar_level0 ($self, $items, $item_selected, $viewed_at_item) {
Subroutine nav_bar_level0: not called CC=1 SCAR=6.9
183 0 my $shownitems
184 = $items->filter(complement $self->item_is_in_lower_hierarchy)
185 ->sort(on the_method("path0"), $self->path0_navigation_cmp);
186 0 $self->nav_bar->($shownitems, $item_selected, $viewed_at_item)
187 }
188  
189 2 _END_
190 }
191  
192 2 package FunctionalPerl::Htmlgen::Nav::RealEntry {
193 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
194 2 ▶ use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
195 2 ▶ use FP::Equal qw(equal);
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 2 ▶ use FP::Struct [[\&is_string, "path0"]],
Subroutine BEGIN: called CC=1 SCAR=0.0
198 2 "FunctionalPerl::Htmlgen::Nav::Entry", "FP::Abstract::Equal";
199  
200 0 ▶ sub FP_Show_show ($self, $show) {
Subroutine FP_Show_show: not called CC=1 SCAR=6.9
201 0 ("entry("
202 . $self->subentries->map($show)->cons(&$show($self->path0))
203 ->strings_join(", ") . ")")
204 }
205  
206 0 ▶ sub FP_Equal_equal ($self, $v) {
Subroutine FP_Equal_equal: not called CC=2 SCAR=17.9
207 0 ▶ ($self->path0 eq $v->path0 and equal($self->subentries, $v->subentries))
Branch: 1 branch
BranchTrueFalseTotal
$self->path0 eq $v->path0 and equal($self->subentries, $v->subentries) 0 0 0
208 }
209  
210 2 _END_
211 }
212  
213 2 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
214 2 ▶use FP::fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 # Build "index" data structure where each path0 is resolved back to an
217 # FP::List of items out of its location. Also to resolve path0 to its
218 # nav object.
219  
220 2 package FunctionalPerl::Htmlgen::Nav::Index {
221 2 ▶ use FP::List qw(null);
Subroutine BEGIN: called CC=1 SCAR=0.0
222  
223 2 ▶ use FP::Struct ["p0_to_upitems", "p0_to_item"];
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 0 ▶ sub path0_to_upitems ($self, $p0) {
Subroutine path0_to_upitems: not called CC=2 SCAR=17.9
226  
227 # now includes the $p0 item itself
228 0 ▶ $self->p0_to_upitems->{$p0}
Condition0%0/3
$self->p0_to_upitems->{$p0} // FunctionalPerl::Htmlgen::Nav::entry($p0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->p0_to_upitems->{$p0} // FunctionalPerl::Htmlgen::Nav::entry($p0)
$self->p0_to_upitems->{$p0} FunctionalPerl::Htmlgen::Nav::entry($p0)
Truth table0%0/3
A // B
A= $self->p0_to_upitems->{$p0}B= FunctionalPerl::Htmlgen::Nav::entry($p0)
ABresult
1X1
011
000
229 // FunctionalPerl::Htmlgen::Nav::entry($p0) # not null
230 }
231  
232 0 ▶ sub path0_to_item ($self, $p0) {
Subroutine path0_to_item: not called CC=2 SCAR=17.9
233 0 ▶ $self->p0_to_item->{$p0} // FunctionalPerl::Htmlgen::Nav::entry($p0)
Condition0%0/3
$self->p0_to_item->{$p0} // FunctionalPerl::Htmlgen::Nav::entry($p0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->p0_to_item->{$p0} // FunctionalPerl::Htmlgen::Nav::entry($p0)
$self->p0_to_item->{$p0} FunctionalPerl::Htmlgen::Nav::entry($p0)
Truth table0%0/3
A // B
A= $self->p0_to_item->{$p0}B= FunctionalPerl::Htmlgen::Nav::entry($p0)
ABresult
1X1
011
000
234 }
235 2 _END_
236 }
237  
238 1 ▶sub nav_index($nav) {
Subroutine nav_index: called CC=1 SCAR=0.0
Pod undocumented
239 1 my (%p0_to_upitems, %p0_to_item);
240 22 ▶ my $index_level = fix sub ($index_level, $items, $upitems) {
Subroutine __ANON__: called CC=1 SCAR=0.0
241 $items->subentries->for_each(
242 21 ▶ sub($item) {
Subroutine __ANON__: called CC=1 SCAR=0.0
243 21 my $p0 = $item->path0;
244 21 my $upitems1 = $upitems->cons($item);
245 21 $p0_to_upitems{$p0} = $upitems1;
246 21 $p0_to_item{$p0} = $item;
247 21 &$index_level($item, $upitems1);
248 }
249 22 );
250 1 };
251 1 &$index_level($nav, null);
252 1 FunctionalPerl::Htmlgen::Nav::Index->new(\%p0_to_upitems, \%p0_to_item);
253 }
254  
255 2 1
« lib/PXML/Element.pm ↑ Summary lib/Chj/xopendir.pm »