| 1 | package Serge::Engine::Plugin::parse_yaml; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Encode qw(decode_utf8 encode_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Serge::Mail; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Serge::Util qw(xml_escape_strref); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub name { | ||||||||||||||||||||||||||||
| 13 | 0 | ▶ | return 'Generic YAML tree parser plugin'; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub init { | ||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | $self->SUPER::init(@_); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | $self->{errors} = {}; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | $self->merge_schema({ | |||||||||||||||||||||||||||
| 24 | expand_aliases => 'BOOLEAN', | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | email_from => 'STRING', | ||||||||||||||||||||||||||||
| 27 | email_to => 'ARRAY', | ||||||||||||||||||||||||||||
| 28 | email_subject => 'STRING', | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | yaml_kind => 'STRING', | ||||||||||||||||||||||||||||
| 31 | }); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | $self->add('after_job', \&report_errors); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub validate_data { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
validate_data: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | $self->SUPER::validate_data; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if (!defined $self->{data}->{email_from}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | print "WARNING: 'email_from' is not defined. Will skip sending any reports.\n"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (!defined $self->{data}->{email_to}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | print "WARNING: 'email_to' is not defined. Will skip sending any reports.\n"; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $yaml_kind = $self->{data}->{yaml_kind}; | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (defined($yaml_kind) && $yaml_kind !~ /^(rails|generic)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $yaml_kind and not $yaml_kind =~ /^(rails|generic)$/
MC/DC0%0/2
defined $yaml_kind and not $yaml_kind =~ /^(rails|generic)$/
defined $yaml_kind
not $yaml_kind =~ /^(rails|generic)$/
Truth table0%0/3
A and not(B)
A= defined $yaml_kindB= not $yaml_kind =~ /^(rails|generic)$/
| |||||||||||||||||||||||||||||
| 51 | 0 | print "WARNING: 'yaml_kind' is '$yaml_kind'. Supported values: rails, generic.\n"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub report_errors { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($self, $phase) = @_; | ||||||||||||||||||||||||||
|
Subroutine
report_errors: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $email_from = $self->{data}->{email_from}; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (!$email_from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | $self->{errors} = {}; | |||||||||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $email_to = $self->{data}->{email_to}; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (!$email_to) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $self->{errors} = {}; | |||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: YAML Parse Errors'); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: YAML Parse Errors'
MC/DC0%0/1
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: YAML Parse Errors'
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $text; | |||||||||||||||||||||||||||
| 73 | 0 | foreach my $key (sort keys %{$self->{errors}}) { | |||||||||||||||||||||||||||
| 74 | 0 | my $pre_contents = $self->{errors}->{$key}; | |||||||||||||||||||||||||||
| 75 | 0 | xml_escape_strref(\$pre_contents); | |||||||||||||||||||||||||||
| 76 | 0 | $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n"; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | $self->{errors} = {}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $text = qq| | |||||||||||||||||||||||||||
| 83 | <html> | ||||||||||||||||||||||||||||
| 84 | <head> | ||||||||||||||||||||||||||||
| 85 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||||||||||||||||||||||||||||
| 86 | </head> | ||||||||||||||||||||||||||||
| 87 | <body style="font-family: sans-serif; font-size: 120%"> | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | <p> | ||||||||||||||||||||||||||||
| 90 | # This is an automatically generated message. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | The following parsing errors were found when attempting to localize resource files. | ||||||||||||||||||||||||||||
| 93 | </p> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | $text | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | </body> | ||||||||||||||||||||||||||||
| 98 | </html> | ||||||||||||||||||||||||||||
| 99 | |; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | Serge::Mail::send_html_message( | |||||||||||||||||||||||||||
| 102 | $email_from, # from | ||||||||||||||||||||||||||||
| 103 | $email_to, # to (list) | ||||||||||||||||||||||||||||
| 104 | $email_subject, # subject | ||||||||||||||||||||||||||||
| 105 | $text # message body | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub parse { | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Make a copy of the string as we will change it | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my $text = $$textref; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (!$self->{data}->{expand_aliases}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | # if we don't expand aliases, then we must preserve | ||||||||||||||||||||||||||||
| 122 | # named anchors (foo: &fooname) and references (bar: *fooname); | ||||||||||||||||||||||||||||
| 123 | # since parsing will remove anchor names, encode them as a part of the key itself | ||||||||||||||||||||||||||||
| 124 | 0 | $text =~ s/^(\s*\S+)(:\s+)\&(\S+)/$1.'__PRESERVE_ANCHOR__'.$3.$2/mge; | |||||||||||||||||||||||||||
| 125 | 0 | $text =~ s/^(\s*\S+:\s+)\*/$1.'__PRESERVE_REFERENCE__'/mge; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Parse YAML | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $tree; | |||||||||||||||||||||||||||
| 131 | 0 | eval { | |||||||||||||||||||||||||||
| 132 | 0 | ($tree) = Load(encode_utf8($text)); | |||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | my $error_text = $@; | |||||||||||||||||||||||||||
| 136 | 0 | $error_text =~ s/\t/ /g; | |||||||||||||||||||||||||||
| 137 | 0 | $error_text =~ s/^\s+//s; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error_text; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | die $error_text; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $is_rails = $self->{data}->{yaml_kind} eq 'rails'; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($is_rails) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | my @tree_keys = keys %$tree; | |||||||||||||||||||||||||||
| 148 | 0 | my $tree_count = @tree_keys; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ($tree_count == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 150 | # Special case NOOP. Empty data | ||||||||||||||||||||||||||||
| 151 | } elsif ($tree_count == 1) { | ||||||||||||||||||||||||||||
| 152 | 0 | $tree = $tree->{$tree_keys[0]}; | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 0 | my $error_text = join(', ', sort @tree_keys); | |||||||||||||||||||||||||||
| 155 | 0 | $error_text = "YAML file is processed in `rails` mode but has multiple root keys: $error_text"; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error_text; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | die $error_text; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # Process tree recursively | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | $self->process_node('', $tree, $callbackref, $lang); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # Reconstruct YAML | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($lang && $is_rails) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$lang and $is_rails
MC/DC0%0/2
$lang and $is_rails
$lang
$is_rails
Truth table0%0/3
A and B
A= $langB= $is_rails
| |||||||||||||||||||||||||||||
| 169 | 0 | my $olr = $self->{parent}->{output_lang_rewrite}; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $root = defined $olr && exists($olr->{$lang}) ? $olr->{$lang} : $lang; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $olr and exists $olr->{$lang}
MC/DC0%0/2
defined $olr and exists $olr->{$lang}
defined $olr
exists $olr->{$lang}
Truth table0%0/3
A and B
A= defined $olrB= exists $olr->{$lang}
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | $tree = {$root => $tree}; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | my $out = decode_utf8(Dump($tree)); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if (!$self->{data}->{expand_aliases}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | $out =~ s/^(\s*\S+)__PRESERVE_ANCHOR__(\S+):/$1.': &'.$2/mge; | |||||||||||||||||||||||||||
| 179 | 0 | $out =~ s/__PRESERVE_REFERENCE__/\*/g; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | return $lang ? $out : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub process_node { | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my ($self, $path, $subtree, $callbackref, $lang, $parent, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_node: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if (ref($subtree) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | # hash | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | foreach my $key (sort keys %$subtree) { | |||||||||||||||||||||||||||
| 192 | 0 | $self->process_node($path.'/'.$key, $subtree->{$key}, $callbackref, $lang, $subtree, $key); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||
| 195 | # text node | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | my $string = $subtree; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # trim the string | ||||||||||||||||||||||||||||
| 200 | 0 | $string =~ s/^[\r\n\t ]+//sg; | |||||||||||||||||||||||||||
| 201 | 0 | $string =~ s/[\r\n\t ]+$//sg; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # translate only non-empty strings; | ||||||||||||||||||||||||||||
| 204 | # skip values starting with __PRESERVE_ANCHOR__ or __PRESERVE_REFERENCE__ | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if (($string ne '') && ($string !~ '^__PRESERVE_(ANCHOR|REFERENCE)__')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$string ne "" and not $string =~ /^__PRESERVE_(ANCHOR|REFERENCE)__/
MC/DC0%0/2
$string ne "" and not $string =~ /^__PRESERVE_(ANCHOR|REFERENCE)__/
$string ne ""
not $string =~ /^__PRESERVE_(ANCHOR|REFERENCE)__/
Truth table0%0/3
A and not(B)
A= $string ne ""B= not $string =~ /^__PRESERVE_(ANCHOR|REFERENCE)__/
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ($lang) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | my $translated_string = &$callbackref($string, undef, $path, undef, $lang, $path); | |||||||||||||||||||||||||||
| 208 | 0 | $parent->{$key} = $translated_string; | |||||||||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||||||||
| 210 | 0 | &$callbackref($string, undef, $path, undef, undef, $path); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | 1; | |||||||||||||||||||||||||||