Zapp/Util.pm

filter: Statementstmt 38.6% 53 / 137 Branchbran 41.4% 29 / 70 Conditioncond 30.7% 12 / 39 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 33.3% 5 / 15 Podpod 100% 8 / 8 totaltotal 38.0% 110 / 289 CC 54 SCAR 37.4
CC
54
Cov
38%
__ANON__
132.0
build_data_from_params
42.0
ansi_colorize
38.5
CRAP 42.3
« Zapp/Task/Test.pm ↑ Summary Zapp/Controller/Trigger.pm »
1 package Zapp::Util;
2 # ABSTRACT: General utilities for Zapp
3  
4 33 ▶use Mojo::Base 'Exporter', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Text::Balanced qw( extract_delimited );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 our @EXPORT_OK = qw(
7 build_data_from_params get_path_from_schema get_slot_from_data
8 get_path_from_data prefix_field rename_field parse_zapp_attrs
9 ansi_colorize
10 );
11  
12 #pod =sub build_data_from_params
13 #pod
14 #pod Build a data structure from the parameters in the given request,
15 #pod optionally with the given prefix. Parameter names are paths in the
16 #pod resulting data structure.
17 #pod
18 #pod # ?name.first=Turanga&name.last=Leela
19 #pod { name => { first => 'Turanga', last => 'Leela' } }
20 #pod
21 #pod # ?crew[0]=Fry&crew[1]=Leela&crew[2]=Zoidberg%20Why%20Not
22 #pod { crew => [ 'Fry', 'Leela', 'Zoidberg Why Not' ] }
23 #pod
24 #pod # ?[0].name=Hubert&[0].age=160&[1].name=Cubert&[1].age=12
25 #pod [ { name => 'Hubert', age => 160 }, { name => 'Cubert', age => 12 } ]
26 #pod
27 #pod =cut
28  
29 0 ▶sub build_data_from_params( $c, $prefix='' ) {
Subroutine build_data_from_params: not called CC=6 SCAR=37.4
Pod documented
30 0 my $data = '';
31 # XXX: Move to Yancy (Util? Controller?)
32 0 ▶ my $dot = $prefix ? '.' : '';
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
33 0 my @params = grep /^$prefix(?:\[\d+\]|\Q$dot\E\w+)/, $c->req->params->names->@*;
34 0 for my $param ( @params ) {
35 0 my $value = $c->param( $param );
36 0 my $path = $param =~ s/^$prefix//r;
37 0 my $slot = get_slot_from_data( $path, \$data );
38 0 $$slot = $value;
39 }
40 0 my @uploads = grep $_->name =~ /^$prefix(?:\[\d+\]|\.\w+)/, $c->req->uploads->@*;
41 0 for my $upload ( @uploads ) {
42 0 my $path = $upload->name =~ s/^$prefix//r;
43 0 my $slot = get_slot_from_data( $path, \$data );
44 0 $$slot = $upload;
45 }
46 0 ▶ return $data ne '' ? $data : undef;
Branch: 1 branch
BranchTrueFalseTotal
$data ne '' ? : 0 0 0
47 }
48  
49 #pod =sub get_slot_from_data
50 #pod
51 #pod Get a reference to the given path in the given data. Can be used to set values
52 #pod at paths.
53 #pod
54 #pod =cut
55  
56 ▶sub get_slot_from_data( $path, $data ) {
Subroutine get_slot_from_data: called CC=5 SCAR=22.3
Pod documented
57 9 my $slot = $data;
58 9 for my $part ( $path =~ m{((?:\w+|\[\d+\]))(?=\.|\[|$)}g ) {
59 13 ▶ if ( $part =~ /^\[(\d+)\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^\[(\d+)\]$/u) { } 0 13 13
60 0 my $part_i = $1;
61 0 ▶ if ( !ref $$slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $$slot) 0 0 0
62 0 $$slot = [];
63 }
64 0 $slot = \( $$slot->[ $part_i ] );
65 0 next;
66 }
67 else {
68 13 ▶ if ( !ref $$slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $$slot) 0 13 13
69 0 $$slot = {};
70 }
71 13 $slot = \( $$slot->{ $part } );
72 }
73 }
74 9 return $slot;
75 }
76  
77 #pod =sub get_path_from_data
78 #pod
79 #pod Get the value for a given path out of the given data.
80 #pod
81 #pod =cut
82  
83 ▶sub get_path_from_data( $path, $data ) {
Subroutine get_path_from_data: called CC=1 SCAR=0.0
Pod documented
84 9 my $slot = get_slot_from_data( $path, \$data );
85 9 return $$slot;
86 }
87  
88 #pod =sub get_path_from_schema
89 #pod
90 #pod Get the schema for a given path out of the given JSON Schema. Will traverse
91 #pod object C<properties> and array C<items> to find the schema.
92 #pod
93 #pod =cut
94  
95 ▶sub get_path_from_schema( $path, $schema ) {
Subroutine get_path_from_schema: not called CC=3 SCAR=24.8
Pod documented
96 0 my $slot = $schema;
97 0 for my $part ( $path =~ m{((?:\w+|\[\d*\]))(?=\.|\[|$)}g ) {
98 0 ▶ if ( $part =~ /^\[\d*\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^\[\d*\]$/u) { } 0 0 0
99 0 $slot = $slot->{ items };
100 0 next;
101 }
102 else {
103 0 $slot = $slot->{ properties }{ $part };
104 }
105 }
106 0 return $slot;
107 }
108  
109 #pod =sub prefix_field
110 #pod
111 #pod Add a prefix to any field in the given HTML or L<Mojo::DOM> object.
112 #pod Prefixes are added to the C<name> and C<id> attributes of any form inputs, and
113 #pod the C<for> attribute of any labels.
114 #pod
115 #pod =cut
116  
117 ▶sub prefix_field( $dom, $prefix ) {
Subroutine prefix_field: not called CC=2 SCAR=17.9
Pod documented
118 0 ▶ if ( ref $dom ne 'Mojo::DOM' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dom ne "Mojo::DOM") 0 0 0
119 0 $dom = Mojo::DOM->new( $dom );
120 }
121  
122 $dom->find( 'input,select,textarea' )->each(
123 sub {
124 0 ▶ my ( $el ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
125 0 my $name = $el->attr( 'name' );
126 0 ▶ my $joiner = $name =~ /^\[/ ? '' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^\[/u ? : 0 0 0
127 0 $el->attr( name => join $joiner, $prefix, $name );
128 0 $el->attr( id => $el->attr( 'name' ) );
129 },
130 0 );
131 $dom->find( 'label' )->each(
132 sub {
133 0 ▶ my ( $el ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
134 0 my $for = $el->attr( 'for' );
135 0 ▶ my $joiner = $for =~ /^\[/ ? '' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$for =~ /^\[/u ? : 0 0 0
136 0 $el->attr( for => join $joiner, $prefix, $for );
137 },
138 0 );
139  
140 0 return $dom;
141 }
142  
143 #pod =sub rename_field
144 #pod
145 #pod Rename any form fields in the given HTML or L<Mojo::DOM> object using
146 #pod the provided mapping. This changes the field C<name> and C<id>
147 #pod attributes, and also the corresponding label C<for> attribute.
148 #pod
149 #pod =cut
150  
151 ▶sub rename_field( $dom, %map ) {
Subroutine rename_field: not called CC=2 SCAR=17.9
Pod documented
152 0 ▶ if ( ref $dom ne 'Mojo::DOM' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dom ne "Mojo::DOM") 0 0 0
153 0 $dom = Mojo::DOM->new( $dom );
154 }
155  
156 $dom->find( 'input,select,textarea' )->each(
157 sub {
158 0 ▶ my ( $el ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
159 0 my $name = $el->attr( 'name' );
160 0 ▶ $el->attr( name => $name =~ s{$name}{$map{ $name } // $name}er );
Condition0%0/3
$map{$name} // $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$map{$name} // $name
$map{$name} $name
Truth table0%0/3
A // B
A= $map{$name}B= $name
ABresult
1X1
011
000
161 0 $el->attr( id => $el->attr( 'name' ) );
162 },
163 0 );
164 $dom->find( 'label' )->each(
165 sub {
166 0 ▶ my ( $el ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 0 my $for = $el->attr( 'for' );
168 0 ▶ $el->attr( for => $for =~ s{$for}{$map{ $for } // $for}re );
Condition0%0/3
$map{$for} // $for
l!l&&r!l&&!r
000
MC/DC0%0/2
$map{$for} // $for
$map{$for} $for
Truth table0%0/3
A // B
A= $map{$for}B= $for
ABresult
1X1
011
000
169 },
170 0 );
171  
172 0 return $dom;
173 }
174  
175 #pod =sub parse_zapp_attrs
176 #pod
177 #pod Parse special C<data-zapp> attributes in the given HTML or L<Mojo::DOM> object.
178 #pod These attributes add dynamic features to templates for L<Zapp::Task>, L<Zapp::Type>,
179 #pod or L<Zapp::Trigger>.
180 #pod
181 #pod =over
182 #pod
183 #pod =item data-zapp-if="<expression>"
184 #pod
185 #pod Display this element if the given C<< <expression> >> is true. The expression can
186 #pod contain string literals, data paths, and the following operators:
187 #pod
188 #pod == != > < >= <=
189 #pod eq ne gt lt ge le
190 #pod
191 #pod If the expression is true, the element is given the C<zapp-visible> class and
192 #pod will appear. Expressions are evaluated in Perl when rendering the template and
193 #pod in JavaScript when the user is modifying the form.
194 #pod
195 #pod =cut
196  
197 # XXX: Add data-zapp-array here
198  
199 ▶sub parse_zapp_attrs( $dom, $data ) {
Subroutine parse_zapp_attrs: not called CC=2 SCAR=17.9
Pod documented
200 0 ▶ if ( ref $dom ne 'Mojo::DOM' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dom ne "Mojo::DOM") 0 0 0
201 0 $dom = Mojo::DOM->new( $dom );
202 }
203  
204 $dom->find( '[data-zapp-if]' )->each(
205 sub {
206 0 ▶ my ( $el ) = @_;
Subroutine __ANON__: not called CC=11 SCAR=48.8
207 0 my ( $lhs, $op, $rhs ) = split /\s*(==|!=|>|<|>=|<=|eq|ne|gt|lt|ge|le)\s*/, $el->attr( 'data-zapp-if' ), 3;
208 #; say "Expr: " . $el->attr( 'data-zapp-if' );
209 #; say "LHS: $lhs; OP: $op; RHS: $rhs";
210 0 ▶ if ( !$op ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $op) { } 0 0 0
211 # Boolean LHS
212 0 my ( $false, $path ) = $lhs =~ /^(!)?\s*(\S+)/;
213 0 my $value = get_path_from_data( $path, $data );
214 0 ▶ if ( ( !$false && $value ) || ( $false && !$value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $false and $value or $false and not $value) 0 0 0
Condition0%0/9
not $false and $value or $false and not $value
l!l&&r!l&&!r
000
not $false and $value
!ll&&!rl&&r
000
$false and not $value
!ll&&!rl&&r
000
MC/DC0%0/4
not $false and $value or $false and not $value
not $false $value $false not $value
Truth table0%0/7
not(A) and B or C and not(D)
A= not $falseB= $valueC= $falseD= not $value
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
215 #; say "False: $false; Value: $value";
216 0 $el->attr( class => join ' ', $el->attr( 'class' ), 'zapp-visible' );
217 }
218 }
219 else {
220 0 my ( $lhs_value, $rhs_value );
221 0 ▶ if ( $lhs_value = extract_delimited( $lhs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lhs_value = &extract_delimited($lhs)) { } 0 0 0
222 0 $lhs_value =~ s/^['"`]|['"`]$//g;
223 }
224 else {
225 0 $lhs_value = get_path_from_data( $lhs, $data );
226 }
227 0 ▶ if ( $rhs_value = extract_delimited( $rhs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs_value = &extract_delimited($rhs)) { } 0 0 0
228 0 $rhs_value =~ s/^['"`]|['"`]$//g;
229 }
230 else {
231 0 $rhs_value = get_path_from_data( $rhs, $data );
232 }
233  
234 my %ops = (
235 0 map { $_ => eval "sub { shift() $_ shift() }" } qw( == != > < >= <= eq ne gt lt ge le ),
236 );
237 #; say "LHS: $lhs_value ($lhs); OP: $op; RHS: $rhs_value ($rhs)";
238 0 ▶ if ( $ops{ $op } && $ops{ $op }->( $lhs_value, $rhs_value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ops{$op} and $ops{$op}($lhs_value, $rhs_value)) 0 0 0
Condition0%0/3
$ops{$op} and $ops{$op}($lhs_value, $rhs_value)
!ll&&!rl&&r
000
MC/DC0%0/2
$ops{$op} and $ops{$op}($lhs_value, $rhs_value)
$ops{$op} $ops{$op}($lhs_value, $rhs_value)
Truth table0%0/3
A and B
A= $ops{$op}B= $ops{$op}($lhs_value, $rhs_value)
ABresult
0X0
100
111
239 0 ▶ $el->attr( class => join ' ', $el->attr( 'class' )//(), 'zapp-visible' );
Condition0%0/3
$el->attr("class") // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$el->attr("class") // ()
$el->attr("class") ()
Truth table0%0/3
A // B
A= $el->attr("class")B= ()
ABresult
1X1
011
000
240 }
241 }
242 },
243 0 );
244  
245 0 return $dom;
246 }
247  
248 # 256 colors
249 # 0x00-0x07: standard colors (same as the 4-bit colours)
250 # 0x08-0x0F: high intensity colors
251 # 0x10-0xE7: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
252 # 0xE8-0xFF: grayscale from black to white in 24 steps
253  
254 33 my %colors;
255 33 $colors{8} = {
256 # Foreground # Background
257 30 => 'black', 40 => 'black',
258 31 => 'maroon', 41 => 'maroon',
259 32 => 'green', 42 => 'green',
260 33 => 'olive', 43 => 'olive',
261 34 => 'navy', 44 => 'navy',
262 35 => 'purple', 45 => 'purple',
263 36 => 'teal', 46 => 'teal',
264 37 => 'silver', 47 => 'silver',
265 90 => 'gray', 100 => 'gray',
266 91 => 'red', 101 => 'red',
267 92 => 'lime', 102 => 'lime',
268 93 => 'yellow', 103 => 'yellow',
269 94 => 'blue', 104 => 'blue',
270 95 => 'fuchsia', 105 => 'fuchsia',
271 96 => 'aqua', 106 => 'aqua',
272 97 => 'white', 107 => 'white',
273 };
274  
275 $colors{256} = {
276 # First 15 colors are mapped from above
277 264 ( map { $_ - 30 => $colors{8}{$_} } 30..37 ),
278 264 ( map { $_ - 82 => $colors{8}{$_} } 90..97 ),
279 # Next 216 are cubes calculated thusly
280 (
281 198 map { my $r = $_;
282 198 map { my $g = $_;
283 1188 map { my $b = $_;
284 7128 16 + 36*$r + 6*$g + $b => sprintf 'rgb(%d,%d,%d)', $r*36, $g*36, $b*36,
285 } 0..5
286 } 0..5
287 } 0..5
288 ),
289  
290 # Final 24 are shades of gray
291 33 ( map { 232 + $_ => sprintf 'rgb(%d,%d,%d)', (8 + $_*10)x3 } 0..23 ),
292 };
293  
294 #pod =sub ansi_colorize
295 #pod
296 #pod Apply ANSI coloring and formatting to the given text. This is used to render
297 #pod output from a program that uses ANSI escape sequences.
298 #pod
299 #pod my $html = ansi_colorize( $stdout );
300 #pod
301 #pod =cut
302  
303 ▶sub ansi_colorize( $text ) {
Subroutine ansi_colorize: called CC=26 SCAR=36.5
Pod documented
304 3 my @parts = split /\e\[([\d;]*)m/, $text;
305 3 ▶ return $parts[0] if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 0 3 3
306 3 my $output = shift @parts;
307 3 my %context;
308 3 ▶ while ( my $code = shift @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = shift @parts) 6 3 9
309 6 my @styles = split /;/, $code;
310 6 ▶ if ( !@styles ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@styles) 0 6 6
311 0 @styles = ( 0 );
312 }
313 6 ▶ while ( my $style = shift @styles ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $style = shift @styles) 9 6 15
314 # 0 reset
315 9 ▶ if ( $style == 0 ) {
Branch: 11 branches
BranchTrueFalseTotal
if ($style == 0) { } 0 9 9
elsif ($style == 1) { } 1 8 9
elsif ($style == 22) { } 0 8 8
elsif ($style == 4) { } 1 7 8
elsif ($style == 24) { } 0 7 7
elsif ($style >= 30 and $style <= 37 or $style >= 90 and $style <= 97) { } 2 5 7
elsif ($style == 38) { } 2 3 5
elsif ($style == 39) { } 0 3 3
elsif ($style >= 40 and $style <= 47 or $style >= 100 and $style <= 107) { } 1 2 3
elsif ($style == 48) { } 2 0 2
elsif ($style == 49) { } 0 0 0
316 0 %context = ();
317 }
318 # 1 bold
319 elsif ( $style == 1 ) {
320 1 $context{ bold } = 'font-weight: bold';
321 }
322 # 22 unbold
323 elsif ( $style == 22 ) {
324 0 delete $context{ bold };
325 }
326 # 4 underline
327 elsif ( $style == 4 ) {
328 1 $context{ underline } = 'text-decoration: underline';
329 }
330 # 24 not underlined
331 elsif ( $style == 24 ) {
332 0 delete $context{ underline };
333 }
334 # 30-37,90-97 foreground color
335 ▶ elsif ( ( $style >= 30 && $style <= 37 ) || ( $style >= 90 && $style <= 97 ) ) {
Condition77%7/9
$style >= 30 and $style <= 37 or $style >= 90 and $style <= 97
l!l&&r!l&&!r
115
$style >= 30 and $style <= 37
!ll&&!rl&&r
061
$style >= 90 and $style <= 97
!ll&&!rl&&r
501
MC/DC50%2/4
$style >= 30 and $style <= 37 or $style >= 90 and $style <= 97
$style >= 30 $style <= 37 $style >= 90 $style <= 97
Truth table42%3/7
A and B or C and D
A= $style >= 30B= $style <= 37C= $style >= 90D= $style <= 97
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
336 2 $context{ color } = 'color: ' . $colors{8}{ $style };
337 }
338 elsif ( $style == 38 ) {
339 2 my $type = shift @styles;
340 # 38;5 256-color
341 2 ▶ if ( $type == 5 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 5) { } 1 1 2
elsif ($type == 2) { } 1 0 1
342 1 $context{ color } = 'color: ' . $colors{256}{ shift @styles };
343 }
344 # 38;2 RGB color (0-255)
345 elsif ( $type == 2 ) {
346 1 my ( $r, $g, $b ) = splice @styles, 0, 3;
347 1 $context{ color } = sprintf 'color: rgb(%d,%d,%d)', $r, $g, $b;
348 }
349 }
350 # 39 reset foreground
351 elsif ( $style == 39 ) {
352 0 delete $context{ color };
353 }
354 # 40-47,100-107 background color
355 ▶ elsif ( ( $style >= 40 && $style <= 47 ) || ( $style >= 100 && $style <= 107 ) ) {
Condition55%5/9
$style >= 40 and $style <= 47 or $style >= 100 and $style <= 107
l!l&&r!l&&!r
102
$style >= 40 and $style <= 47
!ll&&!rl&&r
021
$style >= 100 and $style <= 107
!ll&&!rl&&r
200
MC/DC25%1/4
$style >= 40 and $style <= 47 or $style >= 100 and $style <= 107
$style >= 40 $style <= 47 $style >= 100 $style <= 107
Truth table28%2/7
A and B or C and D
A= $style >= 40B= $style <= 47C= $style >= 100D= $style <= 107
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
356 1 $context{ background } = 'background: ' . $colors{8}{ $style };
357 }
358 elsif ( $style == 48 ) {
359 2 my $type = shift @styles;
360 # 48;5 256-color
361 2 ▶ if ( $type == 5 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 5) { } 1 1 2
elsif ($type == 2) { } 1 0 1
362 1 $context{ background } = 'background: ' . $colors{256}{ shift @styles };
363 }
364 # 48;2 RGB color (0-255)
365 elsif ( $type == 2 ) {
366 1 my ( $r, $g, $b ) = splice @styles, 0, 3;
367 1 $context{ background } = sprintf 'background: rgb(%d,%d,%d)', $r, $g, $b;
368 }
369 }
370 # 49 reset background
371 elsif ( $style == 49 ) {
372 0 delete $context{ background };
373 }
374 }
375  
376 6 $output .= sprintf( '<span style="%s">', join '; ', sort values %context )
377 . shift( @parts )
378 . '</span>';
379 }
380  
381 3 return $output;
382 }
383  
384 33 1;
385  
386 __END__
« Zapp/Task/Test.pm ↑ Summary Zapp/Controller/Trigger.pm »