| 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
| |||||||||||||||||||||||||||||
| 30 | 54 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 211 | 66 | ▶ | $level = 0 unless defined $level; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 229 | 14 | ▶ | push(@subtext, "$leading<$name>") if length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 236 | 14 | $text = join("\n", @subtext); | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | } else { | ||||||||||||||||||||||||||||
| 239 | 48 | ▶ | $text = length $name ? "$leading<$name>$value</$shortname>" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 277 | ▶ | if (! defined $first_indent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 100 | $first_indent = $indent; | |||||||||||||||||||||||||||
| 262 | 100 | $first_type = ref($value); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 277 | ▶ | if ($indent == $first_indent) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 266 | 210 | my $type = ref($value); | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 210 | ▶ | if ($type ne $first_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$first_type ne "ARRAY" and $first_type ne "HASH"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 326 | 292 | my $token = shift(@$tokens); | |||||||||||||||||||||||||||
| 327 | 292 | ▶ | next if $token !~ /\S/ || $token =~ /^<\?/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $token =~ /\S/ or $token =~ /^<\?/
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 =~ /^<\?/
| |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 200 | ▶ | if ($token =~ /^<\s*\/\s*([^\s>]+)/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 330 | 69 | my $ending_tagname = $1; | |||||||||||||||||||||||||||
| 331 | 69 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 67 | ▶ | $value = {} unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 67 | ▶ | die "Unexpected text at $token\n" unless ref $value eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 66 | ▶ | if (exists $value->{$starting_tagname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 10 | my $old_value = $value->{$starting_tagname}; | |||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 10 | ▶ | if (ref $old_value eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 357 | 60 | $value = trim_string($token); | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 7 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $line =~ /^\s*#/ and $line =~ /\S/
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/
| |||||||||||||||||||||||||||||
| 376 | 278 | my $spaces; | |||||||||||||||||||||||||||
| 377 | 278 | ▶ | if ($line =~ /^(\s*)> (.*)/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||