lib/App/Followme/NestedText.pm

filter: Statementstmt 99.1% 225 / 227 Branchbran 91.9% 103 / 112 Conditioncond 83.3% 10 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 20 / 20 Podpod 42.8% 6 / 14 totaltotal 93.8% 369 / 393 CC 67 SCAR 22.4
CC
67
Cov
96%
parse_almost_yaml_block
16.0
parse_almost_xml_block
14.0
format_almost_xml_value
11.0
CRAP 9.4
« lib/App/Followme/UploadSite.pm ↑ Summary lib/App/Followme/Web.pm »
1 package App::Followme::NestedText;
2  
3 19 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 our $VERSION = "2.03";
10  
11 19 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 19 require Exporter;
14 19 our @ISA = qw(Exporter);
15 19 our @EXPORT = qw(nt_parse_almost_yaml_file nt_parse_almost_xml_file
16 nt_parse_almost_yaml_string nt_parse_almost_xml_string
17 nt_write_almost_yaml_file nt_write_almost_xml_file
18 nt_merge_items);
19  
20 #----------------------------------------------------------------------
21 # Merge items from two nested lists
22  
23 54 ▶sub nt_merge_items {
Subroutine nt_merge_items: called CC=8 SCAR=20.8
Pod undocumented
24 54 my ($old_config, $new_config) = @_;
25  
26 54 my $final_config;
27 54 my $ref = ref $old_config;
28  
29 54 ▶ if ($ref eq ref $new_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq ref $new_config) { } 54 0 54
30 54 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 21 33 54
elsif ($ref eq 'HASH') { } 23 10 33
31 21 $final_config = [];
32 21 @$final_config = @$old_config;
33 21 my %old = map {$_ => 1} @$old_config;
34  
35 21 foreach my $item (@$new_config) {
36 29 ▶ push(@$final_config, $item) unless $old{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless $old{$item} 25 4 29
37 }
38  
39 } elsif ($ref eq 'HASH') {
40 23 $final_config = {};
41 23 %$final_config = %$old_config;
42  
43 23 foreach my $name (keys %$new_config) {
44 43 ▶ if (exists $old_config->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old_config->{$name}) { } 32 11 43
45 $final_config->{$name} = nt_merge_items($old_config->{$name},
46 32 $new_config->{$name});
47 } else {
48 11 $final_config->{$name} = $new_config->{$name};
49 }
50 }
51  
52 } else {
53 10 $final_config = $new_config;
54 }
55  
56 } else {
57 0 $final_config = $new_config;
58 }
59  
60 54 return $final_config;
61 }
62  
63 #----------------------------------------------------------------------
64 # Read file in "almost yaml" format
65  
66 22 ▶sub nt_parse_almost_yaml_file {
Subroutine nt_parse_almost_yaml_file: called CC=2 SCAR=7.0
Pod documented
67 22 my ($filename) = @_;
68  
69 22 my %configuration;
70 22 my $page = fio_read_page($filename);
71  
72 22 eval {%configuration = nt_parse_almost_yaml_string($page)};
73 22 ▶ die "$filename: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 22 22
74  
75 22 return %configuration;
76 }
77  
78 #----------------------------------------------------------------------
79 # Read file in "almost xml" format
80  
81 3 ▶sub nt_parse_almost_xml_file {
Subroutine nt_parse_almost_xml_file: called CC=2 SCAR=7.0
Pod documented
82 3 my ($filename) = @_;
83  
84 3 my %rss;
85 3 my $page = fio_read_page($filename);
86  
87 3 eval {%rss = nt_parse_almost_xml_string($page)};
88 3 ▶ die "$filename: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
89  
90 3 return %rss;
91 }
92  
93 #----------------------------------------------------------------------
94 # Read string in "almost yaml" Format
95  
96 100 ▶sub nt_parse_almost_yaml_string {
Subroutine nt_parse_almost_yaml_string: called CC=3 SCAR=11.0
Pod documented
97 100 my ($page) = @_;
98  
99 100 my @lines = split(/\n/, $page);
100 100 my $block = parse_almost_yaml_block(\@lines);
101
102 96 ▶ if (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 1 95 96
103 1 my $msg = trim_string(shift(@lines));
104 1 die("Bad indent at $msg\n");
105 }
106  
107 95 ▶ if (ref($block) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $block ne "HASH") 1 94 95
108 1 die("Configuration must be a hash\n");
109 }
110  
111 94 return %$block;
112 }
113  
114 #----------------------------------------------------------------------
115 # Read string in "almost xml" Format
116  
117 11 ▶sub nt_parse_almost_xml_string {
Subroutine nt_parse_almost_xml_string: called CC=2 SCAR=6.9
Pod documented
118 11 my ($page) = @_;
119  
120 11 my @tokens = split(/(<[^>]*>)/, $page);
121 11 my ($block, $blockname) = parse_almost_xml_block(\@tokens);
122 8 ▶ die "Unexpected closing tag at </$blockname>\n" if $blockname;
Branch: 1 branch
BranchTrueFalseTotal
if $blockname 1 7 8
123  
124 7 return %$block;
125 }
126  
127 #----------------------------------------------------------------------
128 # Write file in "almost yaml" Format
129  
130 3 ▶sub nt_write_almost_yaml_file {
Subroutine nt_write_almost_yaml_file: called CC=1 SCAR=0.0
Pod documented
131 3 my ($filename, %configuration) = @_;
132  
133 3 my ($type, $page) = format_almost_yaml_value(\%configuration);
134 3 $page .= "\n";
135  
136 3 fio_write_page($filename, $page);
137 3 return;
138 }
139  
140 #----------------------------------------------------------------------
141 # Write file in "almost xml" Format
142  
143 2 ▶sub nt_write_almost_xml_file {
Subroutine nt_write_almost_xml_file: called CC=1 SCAR=0.0
Pod documented
144 2 my ($filename, %rss) = @_;
145  
146 2 my $page = "<?xml version=\"1.0\"?>\n";
147 2 $page .= format_almost_xml_value(\%rss);
148 2 $page .= "\n";
149  
150 2 fio_write_page($filename, $page);
151 2 return;
152 }
153  
154 #----------------------------------------------------------------------
155 # Format a value as a yaml string for writing
156  
157 60 ▶sub format_almost_yaml_value {
Subroutine format_almost_yaml_value: called CC=9 SCAR=22.0
Pod undocumented
158 60 my ($value, $level) = @_;
159 60 ▶ $level = 0 unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 9 51 60
160  
161 60 my $text;
162 60 my $type = ref $value;
163 60 my $leading = ' ' x (4 * $level);
164 60 ▶ if ($type eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 5 55 60
elsif ($type eq 'HASH') { } 12 43 55
elsif (length $value > 60) { } 1 42 43
165 5 my @subtext;
166 5 foreach my $subvalue (@$value) {
167 13 my ($subtype, $subtext) = format_almost_yaml_value($subvalue, $level+1);
168 13 ▶ if ($subtype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtype) { } 0 13 13
169 0 $subtext = $leading . "-\n" . $subtext;
170 } else {
171 13 $subtext = $leading . "- " . $subtext;
172 }
173 13 push (@subtext, $subtext);
174 }
175 5 $text = join("\n", @subtext);
176  
177 } elsif ($type eq 'HASH') {
178 12 my @subtext;
179 12 foreach my $name (sort keys %$value) {
180 38 my $subvalue = $value->{$name};
181 38 my ($subtype, $subtext) = format_almost_yaml_value($subvalue, $level+1);
182 38 ▶ if ($subtype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtype) { } 9 29 38
183 9 $subtext = $leading . "$name:\n" . $subtext;
184 } else {
185 29 $subtext = $leading . "$name: " . $subtext;
186 }
187 38 push (@subtext, $subtext);
188 }
189 12 $text = join("\n", @subtext);
190  
191 } elsif (length($value) > 60) {
192 1 $type = 'SCALAR';
193 1 my @subtext = split(/(\S.{0,59}\S*)/, $value);
194 1 @subtext = grep( /\S/, @subtext);
195 1 @subtext = map("$leading> $_", @subtext);
196 1 $text = join("\n", @subtext);
197
198 } else {
199 42 $text = $value;
200 }
201  
202 60 return ($type, $text);
203 }
204  
205 #----------------------------------------------------------------------
206 # Format a value as an xml string for writing
207  
208 66 ▶sub format_almost_xml_value {
Subroutine format_almost_xml_value: called CC=11 SCAR=24.0
Pod undocumented
209 66 my ($value, $name, $level) = @_;
210 66 ▶ $name = '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 6 60 66
211 66 ▶ $level = 0 unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 6 60 66
212  
213 66 my $text;
214 66 my $type = ref $value;
215 66 my $leading = ' ' x (4 * $level);
216 66 my ($shortname) = split(/ /, $name);
217  
218 66 ▶ if ($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 4 62 66
elsif ($type eq 'HASH') { } 14 48 62
219 4 my @subtext;
220 4 foreach my $subvalue (@$value) {
221 12 my $subtext = format_almost_xml_value($subvalue, $name, $level);
222 12 push (@subtext, $subtext);
223 }
224 4 $text = join("\n", @subtext);
225  
226 } elsif ($type eq 'HASH') {
227 14 my @subtext;
228 14 ▶ $level += 1 if length $name;
Branch: 1 branch
BranchTrueFalseTotal
if length $name 8 6 14
229 14 ▶ push(@subtext, "$leading<$name>") if length $name;
Branch: 1 branch
BranchTrueFalseTotal
if length $name 8 6 14
230 14 foreach my $subname (sort_xml_hash($value)) {
231 48 my $subvalue = $value->{$subname};
232 48 my $subtext = format_almost_xml_value($subvalue, $subname, $level);
233 48 push (@subtext, $subtext);
234 }
235 14 ▶ push(@subtext, "$leading</$shortname>") if length $name;
Branch: 1 branch
BranchTrueFalseTotal
if length $name 8 6 14
236 14 $text = join("\n", @subtext);
237
238 } else {
239 48 ▶ $text = length $name ? "$leading<$name>$value</$shortname>"
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 48 0 48
240 : $leading . $value;
241 }
242  
243 66 return $text;
244 }
245  
246 #----------------------------------------------------------------------
247 # Parse a block of "almost yaml" lines at the same indentation level
248  
249 138 ▶sub parse_almost_yaml_block {
Subroutine parse_almost_yaml_block: called CC=16 SCAR=27.7
Pod undocumented
250 138 my ($lines) = @_;
251  
252 138 my @block;
253 138 my ($first_indent, $first_type);
254  
255 138 ▶ while (@$lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 290 106 396
256 290 my $line = shift(@$lines);
257 290 my ($indent, $value) = parse_almost_yaml_line($line);
258 289 ▶ next unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 12 277 289
259  
260 277 ▶ if (! defined $first_indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first_indent) 100 177 277
261 100 $first_indent = $indent;
262 100 $first_type = ref($value);
263 }
264
265 277 ▶ if ($indent == $first_indent) {
Branch: 3 branches
BranchTrueFalseTotal
if ($indent == $first_indent) { } 210 67 277
elsif ($indent > $first_indent) { } 40 27 67
elsif ($indent < $first_indent) { } 27 0 27
266 210 my $type = ref($value);
267  
268 210 ▶ if ($type ne $first_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne $first_type) 1 209 210
269 1 my $msg = trim_string($line);
270 1 die("Missing indent at $msg\n");
271 }
272  
273 209 ▶ if ($type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 51 158 209
elsif ($type eq 'HASH') { } 151 7 158
274 51 push(@block, @$value);
275 } elsif ($type eq 'HASH') {
276 151 push(@block, %$value);
277 } else {
278 7 push(@block, $value);
279 }
280  
281 } elsif ($indent > $first_indent) {
282 40 ▶ if ($first_type ne 'ARRAY' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($first_type ne "ARRAY" and $first_type ne "HASH") 1 39 40
Condition66%2/3
$first_type ne "ARRAY" and $first_type ne "HASH"
!ll&&!rl&&r
0391
MC/DC50%1/2
$first_type ne "ARRAY" and $first_type ne "HASH"
$first_type ne "ARRAY" $first_type ne "HASH"
Truth table66%2/3
A and B
A= $first_type ne "ARRAY"B= $first_type ne "HASH"
ABresult
0X0
100
111
283 $first_type ne 'HASH') {
284 1 my $msg = trim_string($line);
285 1 die("Indent under string at $msg\n");
286 }
287  
288 39 ▶ if (length($block[-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $block[-1]) 1 38 39
289 1 my $msg = trim_string($line);
290 1 die("Duplicate value at $msg\n");
291
292 }
293  
294 38 unshift(@$lines, $line);
295 38 $block[-1] = parse_almost_yaml_block($lines);
296  
297 } elsif ($indent < $first_indent) {
298 27 unshift(@$lines, $line);
299 27 last;
300 }
301 }
302  
303 133 my $block;
304 133 ▶ if (! defined $first_type) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $first_type) { } 38 95 133
elsif ($first_type eq 'ARRAY') { } 29 66 95
elsif ($first_type eq 'HASH') { } 64 2 66
305 38 $block = {};
306 } elsif ($first_type eq 'ARRAY') {
307 29 $block = \@block;
308 } elsif ($first_type eq 'HASH') {
309 64 my %block = @block;
310 64 $block = \%block;
311 } else {
312 2 $block = join(' ', @block);
313 }
314
315 133 return $block;
316 }
317  
318 #----------------------------------------------------------------------
319 # Parse a pair of xml tags and their contents
320  
321 81 ▶sub parse_almost_xml_block {
Subroutine parse_almost_xml_block: called CC=14 SCAR=26.4
Pod undocumented
322 81 my ($tokens) = @_;
323  
324 81 my $value;
325 81 ▶ while (@$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens) 292 7 299
326 292 my $token = shift(@$tokens);
327 292 ▶ next if $token !~ /\S/ || $token =~ /^<\?/;
Branch: 1 branch
BranchTrueFalseTotal
if not $token =~ /\S/ or $token =~ /^<\?/ 92 200 292
Condition100%3/3
not $token =~ /\S/ or $token =~ /^<\?/
l!l&&r!l&&!r
893200
MC/DC100%2/2
not $token =~ /\S/ or $token =~ /^<\?/
not $token =~ /\S/ $token =~ /^<\?/
Truth table100%3/3
not(A) or B
A= not $token =~ /\S/B= $token =~ /^<\?/
ABresult
1X1
011
000
328  
329 200 ▶ if ($token =~ /^<\s*\/\s*([^\s>]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ m[^<\s*/\s*([^\s>]+)]) { } 69 131 200
elsif ($token =~ /^<\s*([^\s>]+)/) { } 70 61 131
330 69 my $ending_tagname = $1;
331 69 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 69 69
332 69 return ($value, $ending_tagname);
333  
334 } elsif ($token =~ /^<\s*([^\s>]+)/) {
335 70 my $starting_tagname = $1;
336 70 my ($subvalue, $ending_tagname) = parse_almost_xml_block($tokens);
337 68 ▶ die "Mismatched tags at $token\n" if $starting_tagname ne $ending_tagname;
Branch: 1 branch
BranchTrueFalseTotal
if $starting_tagname ne $ending_tagname 1 67 68
338  
339 67 ▶ $value = {} unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 18 49 67
340 67 ▶ die "Unexpected text at $token\n" unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 1 66 67
341  
342 66 ▶ if (exists $value->{$starting_tagname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$starting_tagname}) { } 10 56 66
343 10 my $old_value = $value->{$starting_tagname};
344  
345 10 ▶ if (ref $old_value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old_value eq 'ARRAY') { } 5 5 10
346 5 push(@$old_value, $subvalue);
347 } else {
348 5 $value->{$starting_tagname} = [$old_value, $subvalue];
349 }
350
351 } else {
352 56 $value->{$starting_tagname} = $subvalue;
353 }
354  
355 } else {
356 61 ▶ die "Unexpected text at \"$token\"\n" if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 60 61
357 60 $value = trim_string($token);
358 }
359 }
360
361 7 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
362 7 return ($value, '');
363 }
364  
365 #----------------------------------------------------------------------
366 # Parse a single line of "almost yaml" to get its indentation and value
367  
368 290 ▶sub parse_almost_yaml_line {
Subroutine parse_almost_yaml_line: called CC=7 SCAR=19.5
Pod undocumented
369 290 my ($line) = @_;
370
371 290 $line =~ s/\t/ /g;
372 290 $line .= ' ';
373  
374 290 my ($indent, $value);
375 290 ▶ if ($line !~ /^\s*#/ && $line =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /^\s*#/ and $line =~ /\S/) 278 12 290
Condition100%3/3
not $line =~ /^\s*#/ and $line =~ /\S/
!ll&&!rl&&r
75278
MC/DC100%2/2
not $line =~ /^\s*#/ and $line =~ /\S/
not $line =~ /^\s*#/ $line =~ /\S/
Truth table100%3/3
not(A) and B
A= not $line =~ /^\s*#/B= $line =~ /\S/
ABresult
0X0
100
111
376 278 my $spaces;
377 278 ▶ if ($line =~ /^(\s*)> (.*)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^(\s*)> (.*)/) { } 11 267 278
elsif ($line =~ /^(\s*)- (.*)/) { } 80 187 267
elsif ($line =~ /^(\s*)(\S+): (.*)/) { } 186 1 187
378 11 $spaces = $1;
379 11 $value = trim_string($2);
380 } elsif ($line =~ /^(\s*)- (.*)/) {
381 80 $spaces = $1;
382 80 $value = [trim_string($2)];
383 } elsif ($line =~ /^(\s*)(\S+): (.*)/) {
384 186 $spaces = $1;
385 186 $value = {$2 => trim_string($3)};
386 } else {
387 1 my $msg = trim_string($line);
388 1 die "Bad tag at $msg\n";
389 }
390  
391 277 ▶ $indent = defined($spaces) ? length($spaces) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $spaces ? : 277 0 277
392 }
393
394 289 return ($indent, $value);
395 }
396  
397 #----------------------------------------------------------------------
398 # Sort the keys of an xml hash so that scalars are listed first
399  
400 14 ▶sub sort_xml_hash {
Subroutine sort_xml_hash: called CC=2 SCAR=6.9
Pod undocumented
401 14 my ($hash) = @_;
402  
403 14 my @augmented_keys = map {[ref $hash->{$_}, $_]} keys %$hash;
404 14 ▶ @augmented_keys = sort {$a->[0] cmp $b->[0] || $a->[1] cmp $b->[1]} @augmented_keys;
Condition66%2/3
$App::Followme::NestedText::a->[0] cmp $App::Followme::NestedText::b->[0] || $App::Followme::NestedText::a->[1] cmp $App::Followme::NestedText::b->[1]
l!l&&r!l&&!r
11460
MC/DC0%0/2
$App::Followme::NestedText::a->[0] cmp $App::Followme::NestedText::b->[0] || $App::Followme::NestedText::a->[1] cmp $App::Followme::NestedText::b->[1]
$App::Followme::NestedText::a->[0] cmp $App::Followme::NestedText::b->[0] $App::Followme::NestedText::a->[1] cmp $App::Followme::NestedText::b->[1]
Truth table66%2/3
A || B
A= $App::Followme::NestedText::a->[0] cmp $App::Followme::NestedText::b->[0]B= $App::Followme::NestedText::a->[1] cmp $App::Followme::NestedText::b->[1]
ABresult
1X1
011
000
405 14 my @keys = map {$_->[1]} @augmented_keys;
406  
407 14 return @keys;
408 }
409  
410 #----------------------------------------------------------------------
411 # Compress whitespace and remove leading and trailing space from string
412  
413 344 ▶sub trim_string {
Subroutine trim_string: called CC=2 SCAR=6.9
Pod undocumented
414 344 my ($str) = @_;
415 344 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 343 344
416  
417 343 $str =~ s/\s+/ /g;
418 343 $str =~ s/^\s//;
419 343 $str =~ s/\s$//;
420  
421 343 return $str;
422 }
423  
424 19 1;
425 __END__
« lib/App/Followme/UploadSite.pm ↑ Summary lib/App/Followme/Web.pm »