.htmlgen/FunctionalPerl/Htmlgen/Toc.pm

filter: Statementstmt 98.6% 141 / 143 Branchbran 81.2% 13 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 98.1% 52 / 53 Podpod 0.0% 0 / 2 totaltotal 96.3% 209 / 217 CC 10 SCAR 3.0
CC
10
Cov
97%
level_add
3.0
subnodes_head_update
2.1
subnodes_add
2.0
CRAP 1.3
« lib/FP/Equal/t.pm ↑ Summary lib/Chj/IO/WrappedFile.pm »
1 #
2 # Copyright (c) 2014-2019 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::Toc - building a table of contents
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 Expands this syntax in a PXML document:
19  
20 <with_toc> <h2>..</h2> <h3>..</h3>.. <h2>..</h2> </with_toc>
21  
22 into a table of contents and then the original contents but with
23 numbering added to the hX section headers.
24  
25 =head1 SEE ALSO
26  
27 This is a L<FunctionalPerl::Htmlgen::PXMLMapper>
28  
29 =head1 NOTE
30  
31 This is alpha software! Read the status section in the package README
32 or on the L<website|http://functional-perl.org/>.
33  
34 =cut
35  
36 package FunctionalPerl::Htmlgen::Toc;
37  
38 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44  
45 2 ▶use FP::StrictList;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use FP::Stream qw(stream_mixed_state_fold_right);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use PXML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use FP::Predicates qw(is_natural);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 17 ▶sub rindices_numberstring($rindices) {
Subroutine rindices_numberstring: called CC=2 SCAR=6.9
54 is_null($rindices)
55 ? ""
56 17 ▶ : $rindices->reverse->map(sub($i) {"$i."})->strings_join("") . " "
Branch: 1 branch
BranchTrueFalseTotal
is_null($rindices) ? : 7 10 17
57 }
58  
59 1 ▶TEST { rindices_numberstring(list()) } "";
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 ▶TEST { rindices_numberstring(list(2, 1)) } "1.2. ";
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 2 package FunctionalPerl::Htmlgen::Toc::TocNodeBase {
63 2 ▶ use FP::StrictList;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶ use FP::Predicates;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶ use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 2 ▶ use FP::Struct [[\&is_strictlist, "subnodes"]];
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 0 ▶ sub subnodes_length($self) {
Subroutine subnodes_length: not called CC=1 SCAR=6.9
71 0 $self->subnodes->length
72 }
73  
74 14 ▶ sub subnodes_add ($self, $node) {
Subroutine subnodes_add: called CC=2 SCAR=7.1
75 14 ▶ is_instance_of $node, "FunctionalPerl::Htmlgen::Toc::TocNode"
Branch: 1 branch
BranchTrueFalseTotal
is_instance_of($node, "FunctionalPerl::Htmlgen::Toc::TocNode") or die "wrong type" 14 0 14
76 or die "wrong type";
77  
78 # (^ XX so poor, neither do I have a TypedList yet, nor the
79 # syntax to add it to the method declaration)
80 14 ▶ $self->subnodes_update(sub($l) { cons $node, $l })
Subroutine __ANON__: called CC=1 SCAR=0.0
81 14 }
82  
83 3 ▶ sub subnodes_head_update ($self, $fn) {
Subroutine subnodes_head_update: called CC=2 SCAR=7.6
84 3 my $ss = $self->subnodes;
85 3 ▶ if (is_null $ss) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($ss)) { } 0 3 3
86 0 die "skipped level" ## nicer message and all?
87 } else {
88 3 $self->subnodes_set(cons(&$fn($ss->first), $ss->rest))
89 }
90 }
91  
92 17 ▶ sub level_add ($self, $levelsdown, $node) {
Subroutine level_add: called CC=3 SCAR=11.0
93 17 ▶ is_natural $levelsdown or die "wrong type: $levelsdown";
Branch: 1 branch
BranchTrueFalseTotal
is_natural($levelsdown) or die "wrong type: $levelsdown" 17 0 17
94 17 ▶ if ($levelsdown > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($levelsdown > 1) { } 3 14 17
95 $self->subnodes_head_update(
96 3 ▶ sub($subnode) {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 3 $subnode->level_add($levelsdown - 1, $node)
98 }
99 )
100 3 } else {
101 14 $self->subnodes_add($node)
102 }
103 }
104  
105 # just for debugging? $indices is a list of 1-based indices
106 7 ▶ sub ref ($self, $indices) {
Subroutine ref: called CC=2 SCAR=6.9
107 7 ▶ if (is_null $indices) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($indices)) { } 3 4 7
108 3 $self
109 } else {
110 4 my ($i, $indices1) = $indices->first_and_rest;
111 4 my $subnodes = $self->subnodes;
112 4 $subnodes->ref($subnodes->length - $i)->ref($indices1)
113 }
114 }
115  
116 # used during document mapping (while the toc is collected)
117 29 ▶ sub numberstring($self) {
Subroutine numberstring: called CC=2 SCAR=6.9
118 29 my $ss = $self->subnodes;
119 29 my $len = $ss->length;
120  
121 # the first is the one that was added last
122 29 ▶ $len ? $len . "." . $ss->first->numberstring : "";
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 16 13 29
123  
124 # (O(n^2) complexity)
125 }
126  
127 # build the TOC html. Need to get the numberstring differently
128 # now.
129 15 ▶ sub html_with_parents ($self, $rindices) {
Subroutine html_with_parents: called CC=2 SCAR=6.9
130 15 my $maybe_name = $self->name;
131 15 my $shown = [
132 FunctionalPerl::Htmlgen::Toc::rindices_numberstring($rindices),
133 $self->header_pxml_for_toc
134 ];
135 DIR(
136 { class => "toc" },
137 (
138 defined $maybe_name
139 ? A({ href => "#" . $maybe_name }, $shown)
140 : $shown
141 ),
142 $self->subnodes->array__reverse__map_with_length(
143 9 ▶ sub ($node, $num) {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 9 LI($node->html_with_parents(cons $num, $rindices))
145 }
146 )
147 15 ▶ )
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_name ? : 9 6 15
148 }
149  
150 2 _END_
151 }
152  
153 2 package FunctionalPerl::Htmlgen::Toc::TocNode {
154 2 ▶ use FP::Predicates "is_string";
Subroutine BEGIN: called CC=1 SCAR=0.0
155 2 ▶ use PXML "is_pxml_element";
Subroutine BEGIN: called CC=1 SCAR=0.0
156 2 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
159 2 [\&is_string, "name"], # as used in the <a name="">
160 # already in the document
161 [\&is_pxml_element, "header"]
162 ],
163 2 "FunctionalPerl::Htmlgen::Toc::TocNodeBase";
164  
165 9 ▶ sub header_pxml_for_toc($self) {
Subroutine header_pxml_for_toc: called CC=1 SCAR=0.0
166  
167 # XX keep some formatting?
168 9 $self->header->text
169 }
170  
171 2 _END_
172 }
173  
174 2 package FunctionalPerl::Htmlgen::Toc::TocRootNode {
175 2 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 2 ▶ use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 2 ▶ use FP::Struct ["header_pxml_for_toc"],
Subroutine BEGIN: called CC=1 SCAR=0.0
179 2 "FunctionalPerl::Htmlgen::Toc::TocNodeBase";
180  
181 6 ▶ sub name($self) {undef}
Subroutine name: called CC=1 SCAR=0.0
182  
183 6 ▶ sub html($self) {
Subroutine html: called CC=1 SCAR=0.0
184 6 DIV({ class => "toc_box" }, $self->html_with_parents(null))
185 }
186  
187 2 _END__
188 }
189  
190 2 our $empty_toc = FunctionalPerl::Htmlgen::Toc::TocRootNode->new(strictnull,
191 H3({ class => "toc_title" }, "Contents"));
192  
193 14 ▶sub tocnode ($name, $header) {
Subroutine tocnode: called CC=1 SCAR=0.0
194 14 FunctionalPerl::Htmlgen::Toc::TocNode->new(strictnull, $name, $header);
195 }
196  
197 2 our $ttoc;
198 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 1 my $t1 = $empty_toc->level_add(1, tocnode("a", H1("First")));
200 1 my $t11 = $t1->level_add(2, tocnode("b", H2("Sub-First")));
201 1 my $t2 = $t11->level_add(1, tocnode("c", H1("Second")));
202 1 $ttoc = $t2;
203 1 [map { $_->numberstring } $t11, $t2]
204 }
205 2 ['1.1.', '2.'];
206  
207 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
208 1 my $t_paths = list(list(1), list(2), list(1, 1));
209 1 ▶ $t_paths->map (sub($path) { $ttoc->ref($path)->name })->array
Subroutine __ANON__: called CC=1 SCAR=0.0
210 }
211 2 [qw(a c b)];
212  
213 1 ▶TEST { $ttoc->html->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
214 2 '<div class="toc_box">'
215 . '<dir class="toc"><h3 class="toc_title">Contents</h3>'
216 . '<li><dir class="toc"><a href="#a">1. First</a>'
217 . '<li><dir class="toc"><a href="#b">1.1. Sub-First</a></dir></li>'
218 . '</dir></li>'
219 . '<li><dir class="toc"><a href="#c">2. Second</a></dir></li>'
220 . '</dir></div>';
221  
222 32 ▶sub process__with_toc__body ($body, $first_level, $toc, $parents) {
Subroutine process__with_toc__body: called CC=1 SCAR=0.0
223  
224 # -> (processed_body, toc)
225 stream_mixed_state_fold_right(
226 sub ($v, $toc, $rest) {
227 if (is_pxml_element($v)) {
228 if (my ($_level) = $v->name =~ /^[hH](\d+)$/s) {
229 my $level = $_level - $first_level + 1;
230 is_natural $level
231 or die "The given <with_toc> tag dictates that the "
232 . "top-most header level is '$first_level', but "
233 . "encountering <"
234 . $v->name . ">";
235  
236 my $anchor = $parents->find(
237 sub($e) {
238 $e->name eq "a" and $e->maybe_attribute("name")
239 }
240 ) // die "bug, missing anchor element";
241  
242 my $toc = $toc->level_add($level,
243 tocnode($anchor->maybe_attribute("name"), $v));
244  
245 my ($tail, $toc2) = &$rest($toc);
246  
247 (
248 cons(
249 $v->body_update(
250 sub($body) {
251 cons($toc->numberstring . " ", $body)
252 }
253 ),
254 $tail
255 ),
256 $toc2
257 )
258 } else {
259  
260 # map $v's body
261 my ($body2, $toc2)
262 = process__with_toc__body($v->body, $first_level, $toc,
263 cons($v, $parents));
264  
265 my ($tail, $toc3) = &$rest($toc2);
266  
267 (cons($v->body_set($body2), $tail), $toc3)
268 }
269 } else {
270 my ($tail, $toc2) = &$rest($toc);
271 (cons($v, $tail), $toc2)
272 }
273 },
274 sub($toc) {
275  
276 # return counter back up the chain
277 (null, $toc)
278 },
279 32 $body
280 )->($toc);
281 }
282  
283 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
284 1 my ($body, $toc) = process__with_toc__body(["foo"], 1, $empty_toc, null);
285 1 [$body->string, $toc->html->string]
286 }
287 [
288 2 'foo',
289 '<div class="toc_box"><dir class="toc"><h3 class="toc_title">Contents</h3></dir></div>'
290 ];
291  
292 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
293 1 HTML(
294 (
295 process__with_toc__body ["foo", A({ name => "a" }, H1("hed")),
296 "bar"],
297 1, $empty_toc, null
298 )[0]
299 )->string
300 }
301 2 '<html>foo<a name="a"><h1>1. hed</h1></a>bar</html>';
302  
303 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
304 1 HTML(
305 (
306 process__with_toc__body ["foo", A({ name => "a" }, H1("hed")),
307 P("bar")],
308 1, $empty_toc, null
309 )[0]
310 )->string
311 }
312 2 '<html>foo<a name="a"><h1>1. hed</h1></a><p>bar</p></html>';
313  
314 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
315 1 HTML(
316 (
317 process__with_toc__body [
318 cons "foo",
319 [
320 A({ name => "a" }, H1("hed")),
321 A({ name => "b" }, H2("hud")),
322 ["", P("bar")]
323 ]
324 ],
325 1,
326 $empty_toc,
327 null
328 )[0]
329 )->string
330 }
331 2 '<html>foo<a name="a"><h1>1. hed</h1></a><a name="b"><h2>1.1. hud</h2></a><p>bar</p></html>';
332  
333 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
334 1 HTML(
335 (
336 process__with_toc__body [" ", P("blabla"),
337 A({ name => "a" }, H1("for one"))],
338 1, $empty_toc, null
339 )[0]
340 )->string
341 }
342 2 '<html> <p>blabla</p><a name="a"><h1>1. for one</h1></a></html>';
343  
344 # now the "EXPORT":
345  
346 2 ▶use FP::Struct [] => "FunctionalPerl::Htmlgen::PXMLMapper";
Subroutine BEGIN: called CC=1 SCAR=0.0
347  
348 4 ▶sub match_element_names($self) { ["with_toc"] }
Subroutine match_element_names: called CC=1 SCAR=0.0
Pod undocumented
349  
350 4 ▶sub map_element ($self, $e, $uplist) {
Subroutine map_element: called CC=2 SCAR=6.9
Pod undocumented
351 4 ▶ my ($body, $toc)
Condition100%2/2
$e->maybe_attribute("level") // 2
l!l
31
MC/DC100%1/1
$e->maybe_attribute("level") // 2
$e->maybe_attribute("level")
Truth table100%2/2
A
A= $e->maybe_attribute("level")
Aresult
11
00
352 = process__with_toc__body($e->body, $e->maybe_attribute("level") // 2,
353 $empty_toc, null);
354 4 [$toc->html, $body]
355 }
356  
357 2 _END_
« lib/FP/Equal/t.pm ↑ Summary lib/Chj/IO/WrappedFile.pm »