lib/PXML/Util.pm

filter: Statementstmt 100% 121 / 121 Branchbran 84.6% 22 / 26 Conditioncond 54.1% 13 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 41 / 41 Podpod 0.0% 0 / 7 totaltotal 85.4% 200 / 234 CC 23 SCAR 9.6
CC
23
Cov
91%
pxml_deferred_map
8.3
__ANON__
4.0
pxml_eager_map
3.2
CRAP 2.6
« lib/FP/Stream.pm ↑ Summary examples/primes »
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 PXML::Util - utility functions for PXML trees
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 PXML::Util;
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 Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 our @EXPORT = qw();
33 2 our @EXPORT_OK = qw(pxml_deferred_map pxml_eager_map
34 pxml_map_elements pxml_map_elements_exhaustively
35 );
36 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
37  
38 2 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use FP::Stream ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use FP::Hash "hash_perhaps_ref";
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use PXML "is_pxml_element";
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # Mapping PXML data (works not just on elements).
47  
48 # Taking value first, like in OO syntax (saves a `flip` or similar
49 # call, too).
50  
51 # Deferred mapping, which means that $elementfn receives the original
52 # element with unmapped body; the body needs to be mapped or replaced
53 # explicitely.
54  
55 100 ▶sub pxml_deferred_map {
Subroutine pxml_deferred_map: called CC=8 SCAR=21.2
Pod undocumented
56 100 ▶ @_ >= 2 and @_ <= 4 or fp_croak_arity "2-4";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 or fp_croak_arity("2-4") 100 0 100
Condition33%1/3
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
00100
MC/DC0%0/2
@_ >= 2 and @_ <= 4
@_ >= 2 @_ <= 4
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 4
ABresult
0X0
100
111
57  
58 # Elementfn receives (element, up-list, inferior-map), where
59 # up-list is a linked list to the parents and inferior-map is a
60 # function of one argument that will do the same as the
61 # pxml_deferred_map call (but keep the up-list). Otherfn is
62 # called for leafs (non-sequences) and receives (value, up-list).
63  
64 100 my ($v, $elementfn, $maybe_otherfn, $maybe_uplist) = @_;
65  
66 42 ▶ my $make_inferior_map = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 42 my ($uplist) = @_;
68 73 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 73 pxml_deferred_map($_[0], $elementfn, $maybe_otherfn, $uplist)
70 }
71 100 };
72  
73 100 ▶ my $uplist = $maybe_uplist // null;
Condition66%2/3
$maybe_uplist // &null()
l!l&&r!l&&!r
73270
MC/DC0%0/2
$maybe_uplist // &null()
$maybe_uplist &null()
Truth table66%2/3
A // B
A= $maybe_uplistB= &null()
ABresult
1X1
011
000
74  
75 100 ▶ if (my $r = ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = ref $v) { } 42 58 100
76 42 my $uplist2 = cons $v, $uplist;
77 42 ▶ if (blessed($v) and $v->isa("PXML::Element")) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v) and $v->isa('PXML::Element')) { } 37 5 42
Condition66%2/3
builtin::blessed($v) and $v->isa("PXML::Element")
!ll&&!rl&&r
5037
MC/DC50%1/2
builtin::blessed($v) and $v->isa("PXML::Element")
builtin::blessed($v) $v->isa("PXML::Element")
Truth table66%2/3
A and B
A= builtin::blessed($v)B= $v->isa("PXML::Element")
ABresult
0X0
100
111
78  
79 # XX TCO?
80 37 &$elementfn($v, $uplist, &$make_inferior_map($uplist2))
81 } else {
82 5 stream_map(&$make_inferior_map($uplist), stream_mixed_flatten($v))
83 }
84 } else {
85 58 ▶ $maybe_otherfn ? &$maybe_otherfn($v, $uplist) : $v
Branch: 1 branch
BranchTrueFalseTotal
$maybe_otherfn ? : 34 24 58
86 }
87 }
88  
89 # 'Eager' mapping, meaning, the body is mapped already when $elementfn
90 # receives an element.
91  
92 26 ▶sub pxml_eager_map {
Subroutine pxml_eager_map: called CC=3 SCAR=11.8
Pod undocumented
93 26 ▶ @_ >= 2 and @_ <= 4 or fp_croak_arity "2-4";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 or fp_croak_arity("2-4") 26 0 26
Condition33%1/3
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
0026
MC/DC0%0/2
@_ >= 2 and @_ <= 4
@_ >= 2 @_ <= 4
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 4
ABresult
0X0
100
111
94  
95 # The functions receive (value, up-list), where up-list is a
96 # linked list to the parents
97 26 my ($v, $elementfn, $maybe_otherfn, $maybe_uplist) = @_;
98  
99 pxml_deferred_map(
100 $v,
101 35 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 35 my ($e, $uplist, $inferior_map) = @_;
103  
104 # XX TCO?
105 35 &$elementfn($e->body_map($inferior_map), $uplist)
106 },
107 26 $maybe_otherfn,
108 $maybe_uplist
109 )
110 }
111  
112 4 ▶sub t_data {
Subroutine t_data: called CC=1 SCAR=0.0
Pod undocumented
113 4 require PXML::XHTML;
114 4 import PXML::XHTML qw(P B A CODE);
115 4 P("foo", B("bar", undef, stream_iota(5)->take(4)))
116 }
117  
118 1 ▶TEST { t_data->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
119 2 '<p>foo<b>bar5678</b></p>';
120  
121 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 pxml_eager_map(
123 t_data,
124 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 2 ▶ $_[0]->name_update(sub { $_[0] . "A" })
Subroutine __ANON__: called CC=1 SCAR=0.0
126 2 },
127 7 ▶ sub { ($_[0] // "-") . "." }
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0] // "-"
l!l
61
MC/DC100%1/1
$_[0] // "-"
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
128 1 )->string
129 }
130 2 '<pA>foo.<bA>bar.-.5.6.7.8.</bA></pA>';
131  
132 2 ▶use FP::Ops "the_method";
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 13 ▶sub uplist_show {
Subroutine uplist_show: called CC=1 SCAR=0.0
Pod undocumented
135 13 my ($uplist) = @_;
136 13 "[" . $uplist->map (the_method("name"))->strings_join("|") . "]"
137 }
138  
139 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
140 pxml_eager_map(
141 t_data,
142 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 2 my ($e, $uplist) = @_;
144 $e->body_update(
145 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
146 2 cons(uplist_show($uplist), $_[0])
147 }
148 )
149 2 },
150 7 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
151 7 my ($v, $uplist) = @_;
152 7 ▶ uplist_show($uplist) . ($v // "-") . "."
Condition100%2/2
$v // "-"
l!l
61
MC/DC100%1/1
$v // "-"
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
153 }
154 1 )->string
155 }
156 2 '<p>[][p]foo.<b>[p][b|p]bar.[b|p]-.[b|p]5.[b|p]6.[b|p]7.[b|p]8.</b></p>';
157  
158 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 pxml_deferred_map(
160 t_data,
161 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 2 my ($e, $uplist, $inferior_map) = @_;
163 $e->body_update(
164 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
165 2 my ($body) = @_;
166 cons(
167 uplist_show($uplist),
168 $e->name eq "b"
169 2 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
$e->name eq 'b' ? : 1 1 2
170 1 my $s = stream_mixed_flatten $body;
171 1 cons(&$inferior_map(car $s), cdr $s )
172 }
173 : &$inferior_map($body)
174 )
175 }
176 2 );
177 },
178 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
179 2 my ($v, $uplist) = @_;
180 2 ▶ uplist_show($uplist) . ($v // "-") . "."
Condition50%1/2
$v // "-"
l!l
20
MC/DC0%0/1
$v // "-"
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
181 }
182 1 )->string
183 }
184 2 '<p>[][p]foo.<b>[p][b|p]bar.5678</b></p>';
185  
186 1 ▶sub pxml_map_elements {
Subroutine pxml_map_elements: called CC=3 SCAR=11.3
Pod undocumented
187 1 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 1 0 1
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
001
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
188 1 my ($v, $name_to_mapper, $maybe_otherfn) = @_;
189 pxml_eager_map(
190 $v,
191 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
192 5 my ($e, $uplist) = @_;
193 5 ▶ if (my ($mapper) = hash_perhaps_ref $name_to_mapper, $e->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($mapper) = hash_perhaps_ref($name_to_mapper, $e->name)) { } 3 2 5
194 3 &$mapper($e, $uplist)
195 } else {
196 2 $e
197 }
198 },
199 1 $maybe_otherfn
200 );
201 }
202  
203 # A variant of pxml_map_elements that applies mapper functions to
204 # their result until there's none left or the result matches the same
205 # function again (the latter rule is so that mapper functions can
206 # return the same element type that they match, i.e. modify elements
207 # instead of replacing them)
208  
209 # (do you have a better name?)
210 23 ▶sub pxml_map_elements_exhaustively {
Subroutine pxml_map_elements_exhaustively: called CC=3 SCAR=11.0
Pod undocumented
211 23 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 23 0 23
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
0023
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
212 23 my ($v, $name_to_mapper, $maybe_otherfn) = @_;
213 pxml_eager_map(
214 $v,
215 26 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
216 26 my ($e, $uplist) = @_;
217 LP: {
218 26 my $name = $e->name;
219 33 ▶ if (my ($mapper) = hash_perhaps_ref $name_to_mapper, $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($mapper) = hash_perhaps_ref($name_to_mapper, $name)) { } 21 12 33
220 21 my $v = &$mapper($e, $uplist);
221 21 ▶ if (is_pxml_element $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pxml_element($v)) { } 16 5 21
222 16 $e = $v;
223 16 ▶ if ($e->name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->name eq $name) { } 9 7 16
224 9 $e
225 } else {
226 redo LP
227 7 }
228 } else {
229 5 $v
230 }
231 } else {
232 12 $e
233 }
234 }
235 },
236 23 $maybe_otherfn
237 );
238 }
239  
240 2 ▶sub t_exh {
Subroutine t_exh: called CC=1 SCAR=0.0
Pod undocumented
241 2 my ($map) = @_;
242 &$map(
243 P(A({ href => "fun" }, "hey"), B(CODE("boo")), B("fi")),
244 { # a mapper does not go into an endless loop:
245 a =>
246 3 ▶ sub { my ($e, $uplist) = @_; $e->attribute_set(href => "foo") },
Subroutine __ANON__: called CC=1 SCAR=0.0
247  
248 # b mapper's output, if a, still goes through the above as
249 # well in the 'exhaustively' test; also, return value does not
250 # need to be an element. (XX NOTE that a list around an
251 # element, even if the element is the only list value, will
252 # stop exhaustive processing!)
253 4 ▶ b => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
254 4 my ($e, $uplist) = @_;
255 4 ▶ if (is_pxml_element stream_mixed_flatten($e->body)->first) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pxml_element(stream_mixed_flatten($e->body)->first)) { } 2 2 4
256 2 A({ name => "x" }, $e->body)
257 } else {
258 2 $e->body
259 }
260 }
261 }
262 2 )->string
263 }
264  
265 1 ▶TEST { t_exh \&pxml_map_elements }
Subroutine __ANON__: called CC=1 SCAR=0.0
266 2 '<p><a href="foo">hey</a>' . '<a name="x"><code>boo</code></a>' . 'fi</p>';
267  
268 1 ▶TEST { t_exh \&pxml_map_elements_exhaustively }
Subroutine __ANON__: called CC=1 SCAR=0.0
269 2 '<p><a href="foo">hey</a>'
270 . '<a href="foo" name="x"><code>boo</code></a>'
271 . 'fi</p>';
272  
273 2 1
« lib/FP/Stream.pm ↑ Summary examples/primes »