Spiderpup.pm

filter: Statementstmt 50.1% 456 / 910 Branchbran 31.1% 111 / 356 Conditioncond 37.9% 87 / 229 MC/DCmcdc 16.9% 22 / 130 Subroutinesub 65.9% 31 / 47 Podpod 0.0% 0 / 27 totaltotal 41.6% 707 / 1699 CC 312 SCAR 54.3
CC
312
Cov
44%
render_ssr_node
1074.3
run_server
462.0
compile_all
182.0
CRAP 227.3
↑ Summary Spiderpup/SFC.pm »
1 package Yote::Spiderpup;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.07';
7  
8 1 ▶use CSS::LESSp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Yote::Spiderpup::SFC qw(parse_sfc);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Yote::Spiderpup::Transform qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 transform_dollar_vars
21 transform_expression
22 extract_arrow_params
23 add_implicit_this
24 parse_html
25 1 );
26  
27 # HTML void tags (self-closing)
28 1 my %VOID_TAGS = map { $_ => 1 } qw(area base br col embed hr img input meta param source track wbr);
29  
30 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
31 11 my ($class, %args) = @_;
32 11 ▶ my $www_dir = $args{www_dir} // die "www_dir required";
Condition50%1/2
$args{"www_dir"} // die("www_dir required")
l!l
110
MC/DC0%0/1
$args{"www_dir"} // die("www_dir required")
$args{"www_dir"}
Truth table50%1/2
A
A= $args{"www_dir"}
Aresult
11
00
33 11 ▶ my $webroot_dir = $args{webroot_dir} // File::Spec->catdir($www_dir, 'webroot');
Condition66%2/3
$args{"webroot_dir"} // "File::Spec"->catdir($www_dir, "webroot")
l!l&&r!l&&!r
380
MC/DC0%0/2
$args{"webroot_dir"} // "File::Spec"->catdir($www_dir, "webroot")
$args{"webroot_dir"} "File::Spec"->catdir($www_dir, "webroot")
Truth table66%2/3
A // B
A= $args{"webroot_dir"}B= "File::Spec"->catdir($www_dir, "webroot")
ABresult
1X1
011
000
34 my $self = bless {
35 www_dir => $www_dir,
36 pages_dir => File::Spec->catdir($www_dir, 'pages'),
37 recipes_dir => File::Spec->catdir($www_dir, 'recipes'),
38 webroot_dir => $webroot_dir,
39 js_dir => File::Spec->catdir($webroot_dir, 'js'),
40 static_dir => $www_dir,
41 11 ▶ base_url_path => $args{base_url_path} // '',
Condition100%2/2
$args{"base_url_path"} // ""
l!l
47
MC/DC100%1/1
$args{"base_url_path"} // ""
$args{"base_url_path"}
Truth table100%2/2
A
A= $args{"base_url_path"}
Aresult
11
00
42 file_mtimes => {},
43 last_change_time => 0,
44 }, $class;
45 11 ▶ make_path($self->{js_dir}) unless -d $self->{js_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"js_dir"} 3 8 11
46 11 return $self;
47 }
48  
49 #----------------------------------------------------------------------
50 # File change tracking
51 #----------------------------------------------------------------------
52  
53 0 ▶sub _scan_yaml_dir {
Subroutine _scan_yaml_dir: not called CC=8 SCAR=42.8
54 0 my ($self, $dir, $changed_ref) = @_;
55 0 ▶ opendir(my $dh, $dir) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return 0 0 0
56 0 ▶ while (my $entry = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 0 0 0
57 0 ▶ next if $entry =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\./ 0 0 0
58 0 my $path = File::Spec->catfile($dir, $entry);
59 0 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
elsif ($entry =~ /\.(yaml|pup)$/) { } 0 0 0
60 0 $self->_scan_yaml_dir($path, $changed_ref);
61 } elsif ($entry =~ /\.(yaml|pup)$/) {
62 0 my $mtime = (stat($path))[9];
63 0 ▶ if (!exists $self->{file_mtimes}{$path} || $self->{file_mtimes}{$path} != $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"file_mtimes"}{$path} or $self->{"file_mtimes"}{$path} != $mtime) 0 0 0
Condition0%0/3
not exists $self->{"file_mtimes"}{$path} or $self->{"file_mtimes"}{$path} != $mtime
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{"file_mtimes"}{$path} or $self->{"file_mtimes"}{$path} != $mtime
not exists $self->{"file_mtimes"}{$path} $self->{"file_mtimes"}{$path} != $mtime
Truth table0%0/3
not(A) or B
A= not exists $self->{"file_mtimes"}{$path}B= $self->{"file_mtimes"}{$path} != $mtime
ABresult
1X1
011
000
64 0 $self->{file_mtimes}{$path} = $mtime;
65 0 $$changed_ref = 1;
66 }
67 }
68 }
69 0 closedir($dh);
70 }
71  
72 0 ▶sub update_file_mtimes {
Subroutine update_file_mtimes: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my ($self) = @_;
74 0 my $changed = 0;
75  
76 0 $self->_scan_yaml_dir($self->{recipes_dir}, \$changed);
77 0 $self->_scan_yaml_dir($self->{pages_dir}, \$changed);
78  
79 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 0 0
80 0 $self->{last_change_time} = time();
81 }
82 0 return $changed;
83 }
84  
85 0 ▶sub is_dev_mode {
Subroutine is_dev_mode: not called CC=2 SCAR=17.9
Pod undocumented
86 0 ▶ return $ENV{SPIDERPUP_DEV} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SPIDERPUP_DEV'} ? : 0 0 0
87 }
88  
89 #----------------------------------------------------------------------
90 # Component file helpers (.yaml / .pup)
91 #----------------------------------------------------------------------
92  
93 23 ▶sub _find_component_file {
Subroutine _find_component_file: called CC=3 SCAR=16.0
94 23 my ($self, $dir, $name) = @_;
95 23 my $yaml = File::Spec->catfile($dir, "$name.yaml");
96 23 ▶ return $yaml if -f $yaml;
Branch: 1 branch
BranchTrueFalseTotal
if -f $yaml 23 0 23
97 0 my $pup = File::Spec->catfile($dir, "$name.pup");
98 0 ▶ return $pup if -f $pup;
Branch: 1 branch
BranchTrueFalseTotal
if -f $pup 0 0 0
99 0 return undef;
100 }
101  
102 12 ▶sub _load_component_data {
Subroutine _load_component_data: called CC=3 SCAR=17.9
103 12 my ($self, $file_path) = @_;
104 12 ▶ if ($file_path =~ /\.pup$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path =~ /\.pup$/) 0 12 12
105 0 ▶ open my $fh, '<', $file_path or die "Cannot open $file_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file_path or die "Cannot open ${file_path}: $!" 0 0 0
106 0 my $content = do { local $/; <$fh> };
107 0 close $fh;
108 0 return parse_sfc($content);
109 }
110 12 return YAML::LoadFile($file_path);
111 }
112  
113 #----------------------------------------------------------------------
114 # Path resolution
115 #----------------------------------------------------------------------
116  
117 0 ▶sub parse_page_path {
Subroutine parse_page_path: not called CC=5 SCAR=34.0
Pod undocumented
118 0 my ($self, $path) = @_;
119  
120 0 $path =~ s|^/+||;
121 0 $path =~ s|\.html$||;
122 0 $path =~ s|\.yaml$||;
123 0 $path =~ s|\.pup$||;
124  
125 0 ▶ return 'index' if $path eq '' || $path eq 'index';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" or $path eq "index" 0 0 0
Condition0%0/3
$path eq "" or $path eq "index"
l!l&&r!l&&!r
000
MC/DC0%0/2
$path eq "" or $path eq "index"
$path eq "" $path eq "index"
Truth table0%0/3
A or B
A= $path eq ""B= $path eq "index"
ABresult
1X1
011
000
126  
127 0 my @segments = split(/\//, $path);
128 0 ▶ if (@segments == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@segments == 1) 0 0 0
129 0 my $subdir = File::Spec->catdir($self->{pages_dir}, $segments[0]);
130 0 ▶ if (-d $subdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $subdir) 0 0 0
131 0 return "$segments[0]/index";
132 }
133 }
134  
135 0 return $path;
136 }
137  
138 7 ▶sub load_page {
Subroutine load_page: called CC=5 SCAR=16.3
Pod undocumented
139 7 my ($self, $path) = @_;
140  
141 7 $path =~ s|^/+||;
142 7 $path =~ s|\.html$||;
143 7 $path =~ s|\.yaml$||;
144 7 $path =~ s|\.pup$||;
145 7 ▶ $path = 'index' if $path eq '' || $path eq 'index.html';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" or $path eq "index.html" 0 7 7
Condition33%1/3
$path eq "" or $path eq "index.html"
l!l&&r!l&&!r
007
MC/DC0%0/2
$path eq "" or $path eq "index.html"
$path eq "" $path eq "index.html"
Truth table33%1/3
A or B
A= $path eq ""B= $path eq "index.html"
ABresult
1X1
011
000
146  
147 7 my $component_file;
148  
149 7 ▶ if ($path =~ s|^recipes/||) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[^recipes/][]) { } 5 2 7
150 5 $component_file = $self->_find_component_file($self->{recipes_dir}, $path);
151 } else {
152 2 $component_file = $self->_find_component_file($self->{pages_dir}, $path);
153 }
154  
155 7 ▶ return undef unless $component_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $component_file 0 7 7
156  
157 7 my $page_data = $self->_load_component_data($component_file);
158 7 my $relative_path = File::Spec->abs2rel($component_file, $self->{www_dir});
159 7 $page_data->{yaml_path} = $relative_path;
160  
161 7 return $page_data;
162 }
163  
164 14 ▶sub resolve_recipe_name {
Subroutine resolve_recipe_name: called CC=4 SCAR=13.9
Pod undocumented
165 14 my ($self, $import_path) = @_;
166  
167 14 $import_path =~ s|^/+||;
168 14 $import_path =~ s|\.yaml$||;
169 14 $import_path =~ s|\.pup$||;
170  
171 14 my ($module_name, $is_page);
172 14 ▶ if ($import_path =~ m|^recipes/(.+)$|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($import_path =~ m[^recipes/(.+)$]) { } 10 4 14
173 10 $module_name = $1;
174 10 $is_page = 0;
175 } else {
176 4 $module_name = $import_path;
177 4 my $page_file = $self->_find_component_file($self->{pages_dir}, $module_name);
178 4 ▶ $is_page = $page_file ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$page_file ? : 4 0 4
179 }
180  
181 14 my $class_name = $module_name;
182 14 $class_name =~ s|/|_|g;
183 14 $class_name =~ s/-(.)/\U$1/g;
184 14 $class_name = ucfirst($class_name);
185  
186 14 ▶ my $js_path = $is_page ? "/js/pages/$module_name.js" : "/js/$module_name.js";
Branch: 1 branch
BranchTrueFalseTotal
$is_page ? : 4 10 14
187  
188 14 return ($module_name, $class_name, $js_path, $is_page);
189 }
190  
191 #----------------------------------------------------------------------
192 # Caching
193 #----------------------------------------------------------------------
194  
195 0 ▶sub get_cache_paths {
Subroutine get_cache_paths: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my ($self, $page_name) = @_;
197 return (
198 html => File::Spec->catfile($self->{webroot_dir}, "$page_name.html"),
199 0 meta => File::Spec->catfile($self->{webroot_dir}, "$page_name.meta"),
200 );
201 }
202  
203 3 ▶sub get_recipe_cache_paths {
Subroutine get_recipe_cache_paths: called CC=1 SCAR=0.0
Pod undocumented
204 3 my ($self, $module_name) = @_;
205 return (
206 js => File::Spec->catfile($self->{js_dir}, "$module_name.js"),
207 3 meta => File::Spec->catfile($self->{js_dir}, "$module_name.js.meta"),
208 );
209 }
210  
211 0 ▶sub collect_yaml_files {
Subroutine collect_yaml_files: not called CC=10 SCAR=47.0
Pod undocumented
212 0 my ($self, $page_data, $page_name, $collected) = @_;
213 0 ▶ $collected //= {};
Condition0%0/2
$collected //= {}
l!l
00
MC/DC0%0/1
$collected //= {}
$collected
Truth table0%0/2
A
A= $collected
Aresult
11
00
214  
215 0 my $component_file;
216 0 ▶ if ($page_name =~ /^recipes\/(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_name =~ m[^recipes/(.+)$]) { } 0 0 0
217 0 $component_file = $self->_find_component_file($self->{recipes_dir}, $1);
218 } else {
219 0 $component_file = $self->_find_component_file($self->{pages_dir}, $page_name);
220 }
221 0 ▶ if ($component_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component_file) 0 0 0
222 0 $collected->{$component_file} = (stat($component_file))[9];
223 }
224  
225 0 ▶ my $imports = $page_data->{import} // {};
Condition0%0/2
$page_data->{"import"} // {}
l!l
00
MC/DC0%0/1
$page_data->{"import"} // {}
$page_data->{"import"}
Truth table0%0/2
A
A= $page_data->{"import"}
Aresult
11
00
226 0 for my $namespace (keys %$imports) {
227 0 my $import_path = $imports->{$namespace};
228 0 $import_path =~ s|^/+||;
229 0 $import_path =~ s|\.yaml$||;
230 0 $import_path =~ s|\.pup$||;
231  
232 0 my $import_file;
233 0 ▶ if ($import_path =~ /^recipes\/(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($import_path =~ m[^recipes/(.+)$]) { } 0 0 0
234 0 $import_file = $self->_find_component_file($self->{recipes_dir}, $1);
235 } else {
236 0 $import_file = $self->_find_component_file($self->{pages_dir}, $import_path);
237 }
238 0 ▶ next unless $import_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $import_file 0 0 0
239 0 ▶ next if exists $collected->{$import_file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $collected->{$import_file} 0 0 0
240  
241 0 my $imported_page = $self->load_page($import_path);
242 0 ▶ if ($imported_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imported_page) 0 0 0
243 0 $self->collect_yaml_files($imported_page, $import_path, $collected);
244 }
245 }
246  
247 0 return $collected;
248 }
249  
250 0 ▶sub is_cache_valid {
Subroutine is_cache_valid: not called CC=10 SCAR=47.0
Pod undocumented
251 0 my ($self, $page_name, $page_data) = @_;
252  
253 0 my %paths = $self->get_cache_paths($page_name);
254  
255 0 ▶ return 0 unless -f $paths{html} && -f $paths{meta};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $paths{"html"} and -f $paths{"meta"} 0 0 0
Condition0%0/3
-f $paths{"html"} and -f $paths{"meta"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $paths{"html"} and -f $paths{"meta"}
-f $paths{"html"} -f $paths{"meta"}
Truth table0%0/3
A and B
A= -f $paths{"html"}B= -f $paths{"meta"}
ABresult
0X0
100
111
256  
257 0 ▶ open my $fh, '<', $paths{meta} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $paths{"meta"} or return 0 0 0 0
258 0 my $meta_content = do { local $/; <$fh> };
259 0 close $fh;
260  
261 0 my $cached_mtimes;
262 0 eval { $cached_mtimes = decode_json($meta_content); };
263 0 ▶ return 0 if $@ || !$cached_mtimes;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $cached_mtimes 0 0 0
Condition0%0/3
$@ or not $cached_mtimes
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $cached_mtimes
$@ not $cached_mtimes
Truth table0%0/3
A or not(B)
A= $@B= not $cached_mtimes
ABresult
1X1
011
000
264  
265 0 my $current_mtimes = $self->collect_yaml_files($page_data, $page_name);
266  
267 0 ▶ return 0 if keys %$cached_mtimes != keys %$current_mtimes;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$cached_mtimes != keys %$current_mtimes 0 0 0
268  
269 0 for my $file (keys %$cached_mtimes) {
270 0 ▶ return 0 unless exists $current_mtimes->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current_mtimes->{$file} 0 0 0
271 0 ▶ return 0 if $current_mtimes->{$file} != $cached_mtimes->{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $current_mtimes->{$file} != $cached_mtimes->{$file} 0 0 0
272 }
273  
274 0 return 1;
275 }
276  
277 0 ▶sub get_cached_html {
Subroutine get_cached_html: not called CC=8 SCAR=42.8
Pod undocumented
278 0 my ($self, $page_data, $page_name) = @_;
279  
280 0 ▶ if ($ENV{SPIDERPUP_NO_CACHE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SPIDERPUP_NO_CACHE"}) 0 0 0
281 0 return $self->build_html($page_data, $page_name);
282 }
283 0 my %paths = $self->get_cache_paths($page_name);
284  
285 0 ▶ if ($self->is_cache_valid($page_name, $page_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_cache_valid($page_name, $page_data)) 0 0 0
286 0 print "CACHED $page_name\n";
287 0 ▶ open my $fh, '<', $paths{html} or goto BUILD;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $paths{"html"} or goto BUILD 0 0 0
288 0 my $html = do { local $/; <$fh> };
289 0 close $fh;
290 0 return $html;
291 }
292 0 print "not cached $page_name\n";
293  
294 0 BUILD:
295 my $html = $self->build_html($page_data, $page_name);
296  
297 0 my $mtimes = $self->collect_yaml_files($page_data, $page_name);
298  
299 0 my $dir = dirname($paths{html});
300 0 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
301  
302 0 eval {
303 0 ▶ open my $html_fh, '>', $paths{html} or die "Cannot write $paths{html}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $html_fh, ">", $paths{"html"} or die qq[Cannot write $paths{"html"}: $!] 0 0 0
304 0 print $html_fh $html;
305 0 close $html_fh;
306  
307 0 ▶ open my $meta_fh, '>', $paths{meta} or die "Cannot write $paths{meta}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $meta_fh, ">", $paths{"meta"} or die qq[Cannot write $paths{"meta"}: $!] 0 0 0
308 0 print $meta_fh encode_json($mtimes);
309 0 close $meta_fh;
310 };
311 0 ▶ warn "Cache write failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
312  
313 0 return $html;
314 }
315  
316 #----------------------------------------------------------------------
317 # Class generation
318 #----------------------------------------------------------------------
319  
320 0 ▶sub _save_block {
Subroutine _save_block: not called CC=5 SCAR=34.0
321 0 my ($self, $result, $key, $mode, $multiline, $map, $array) = @_;
322 0 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
323  
324 0 ▶ if ($mode eq 'multiline') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'multiline') { } 0 0 0
elsif ($mode eq 'array') { } 0 0 0
elsif ($mode eq 'map') { } 0 0 0
325 0 $result->{$key} = join("\n", @$multiline);
326 } elsif ($mode eq 'array') {
327 0 $result->{$key} = [ @$array ];
328 } elsif ($mode eq 'map') {
329 0 $result->{$key} = { %$map };
330 }
331 }
332  
333 6 ▶sub generate_single_class {
Subroutine generate_single_class: called CC=43 SCAR=39.8
Pod undocumented
334 6 my ($self, $class_name, $page, $page_imports_obj) = @_;
335  
336 6 ▶ my @method_names = $page->{methods} ? keys %{$page->{methods}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$page->{'methods'} ? : 3 3 6
337 6 my $known_methods = {map { $_ => 1 } @method_names};
338  
339 6 ▶ my $title = $page->{title} // '';
Condition100%2/2
$page->{"title"} // ""
l!l
42
MC/DC100%1/1
$page->{"title"} // ""
$page->{"title"}
Truth table100%2/2
A
A= $page->{"title"}
Aresult
11
00
340 6 ▶ my $yaml_path = $page->{yaml_path} // '';
Condition100%2/2
$page->{"yaml_path"} // ""
l!l
51
MC/DC100%1/1
$page->{"yaml_path"} // ""
$page->{"yaml_path"}
Truth table100%2/2
A
A= $page->{"yaml_path"}
Aresult
11
00
341 6 ▶ my $html_raw = $page->{html} // '';
Condition50%1/2
$page->{"html"} // ""
l!l
60
MC/DC0%0/1
$page->{"html"} // ""
$page->{"html"}
Truth table50%1/2
A
A= $page->{"html"}
Aresult
11
00
342 6 my $html = $html_raw;
343 6 $title =~ s/\\/\\\\/g; $title =~ s/'/\\'/g;
344 6 $yaml_path =~ s/\\/\\\\/g; $yaml_path =~ s/'/\\'/g;
345 6 $html =~ s/\\/\\\\/g; $html =~ s/'/\\'/g;
346 6 $html =~ s/\n/\\n/g;
347  
348 6 my $structure = parse_html($html_raw, $known_methods);
349  
350 6 my $structure_json = encode_json($structure);
351  
352 # remove quotes around functions so they are interpreted as javascript functions
353 # handles both function() and arrow function () => styles
354 6 $structure_json =~ s/"\*([a-z_]+":)"(function\([^"]+)"/"$1$2/g;
355 6 $structure_json =~ s/"\*([a-z_]+":)"(\([^"]*\)\s*=>[^"]*)"/"$1$2/g;
356  
357 # Build alternate structures for html_* variant keys
358 6 my @variant_entries;
359 6 for my $key (sort keys %$page) {
360 38 ▶ if ($key =~ /^html_(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^html_(\w+)$/) 2 36 38
361 2 my $variant_name = $1;
362 2 my $variant_html_raw = $page->{$key};
363 2 my $variant_structure = parse_html($variant_html_raw, $known_methods);
364 2 my $variant_json = encode_json($variant_structure);
365 2 $variant_json =~ s/"\*([a-z_]+":)"(function\([^"]+)"/"$1$2/g;
366 2 $variant_json =~ s/"\*([a-z_]+":)"(\([^"]*\)\s*=>[^"]*)"/"$1$2/g;
367 2 push @variant_entries, "$variant_name: $variant_json";
368 }
369 }
370 6 ▶ my $structures_js = @variant_entries ? '{ ' . join(', ', @variant_entries) . ' }' : '{}';
Branch: 1 branch
BranchTrueFalseTotal
@variant_entries ? : 1 5 6
371  
372 6 ▶ my $imports_obj = $page_imports_obj // '{}';
Condition50%1/2
$page_imports_obj // "{}"
l!l
60
MC/DC0%0/1
$page_imports_obj // "{}"
$page_imports_obj
Truth table50%1/2
A
A= $page_imports_obj
Aresult
11
00
373  
374 6 my $vars_json = '{}';
375 6 my @var_methods;
376 6 ▶ if ($page->{vars} && keys %{$page->{vars}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"vars"} and keys %{$$page{"vars"};}) 3 3 6
Condition66%2/3
$page->{"vars"} and keys %{$$page{"vars"};}
!ll&&!rl&&r
303
MC/DC50%1/2
$page->{"vars"} and keys %{$$page{"vars"};}
$page->{"vars"} keys %{$$page{"vars"};}
Truth table66%2/3
A and B
A= $page->{"vars"}B= keys %{$$page{"vars"};}
ABresult
0X0
100
111
377 3 my %vars_copy = %{$page->{vars}};
378 3 for my $var_name (keys %vars_copy) {
379 8 my $value = $vars_copy{$var_name};
380 8 ▶ if (defined $value && !ref($value) && $value =~ /^-?\d+\.?\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $value =~ /^-?\d+\.?\d*$/) 4 4 8
Condition83%5/6
defined $value and not ref $value and $value =~ /^-?\d+\.?\d*$/
!ll&&!rl&&r
134
defined $value and not ref $value
!ll&&!rl&&r
017
MC/DC66%2/3
defined $value and not ref $value and $value =~ /^-?\d+\.?\d*$/
defined $value not ref $value $value =~ /^-?\d+\.?\d*$/
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /^-?\d+\.?\d*$/
ABCresult
0XX0
10X0
1100
1111
381 4 $vars_copy{$var_name} = $value + 0;
382 }
383 }
384 3 $vars_json = encode_json(\%vars_copy);
385 3 for my $var_name (sort keys %{$page->{vars}}) {
386 8 push @var_methods, " get_$var_name(defaultValue) { return this.get('$var_name', defaultValue); }";
387 8 push @var_methods, " set_$var_name(value) { return this.set('$var_name', value); }";
388 }
389 }
390  
391 6 my @custom_methods;
392 6 ▶ if ($page->{methods} && keys %{$page->{methods}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"methods"} and keys %{$$page{"methods"};}) 3 3 6
Condition66%2/3
$page->{"methods"} and keys %{$$page{"methods"};}
!ll&&!rl&&r
303
MC/DC50%1/2
$page->{"methods"} and keys %{$$page{"methods"};}
$page->{"methods"} keys %{$$page{"methods"};}
Truth table66%2/3
A and B
A= $page->{"methods"}B= keys %{$$page{"methods"};}
ABresult
0X0
100
111
393 3 for my $method_name (sort keys %{$page->{methods}}) {
394 4 my $method_code = transform_expression($page->{methods}{$method_name}, $known_methods);
395 4 push @custom_methods, " $method_name = $method_code;";
396 }
397 }
398  
399 6 my @computed_methods;
400 6 ▶ if ($page->{computed} && keys %{$page->{computed}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"computed"} and keys %{$$page{"computed"};}) 1 5 6
Condition66%2/3
$page->{"computed"} and keys %{$$page{"computed"};}
!ll&&!rl&&r
501
MC/DC50%1/2
$page->{"computed"} and keys %{$$page{"computed"};}
$page->{"computed"} keys %{$$page{"computed"};}
Truth table66%2/3
A and B
A= $page->{"computed"}B= keys %{$$page{"computed"};}
ABresult
0X0
100
111
401 1 for my $computed_name (sort keys %{$page->{computed}}) {
402 1 my $computed_code = transform_expression($page->{computed}{$computed_name}, $known_methods);
403 1 push @computed_methods, " get_$computed_name() { return ($computed_code).call(this); }";
404 }
405 }
406  
407 6 my @lifecycle_methods;
408 6 ▶ if ($page->{lifecycle} && keys %{$page->{lifecycle}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"lifecycle"} and keys %{$$page{"lifecycle"};}) 3 3 6
Condition66%2/3
$page->{"lifecycle"} and keys %{$$page{"lifecycle"};}
!ll&&!rl&&r
303
MC/DC50%1/2
$page->{"lifecycle"} and keys %{$$page{"lifecycle"};}
$page->{"lifecycle"} keys %{$$page{"lifecycle"};}
Truth table66%2/3
A and B
A= $page->{"lifecycle"}B= keys %{$$page{"lifecycle"};}
ABresult
0X0
100
111
409 3 for my $hook_name (sort keys %{$page->{lifecycle}}) {
410 4 my $hook_code = transform_expression($page->{lifecycle}{$hook_name}, $known_methods);
411 4 push @lifecycle_methods, " $hook_name = $hook_code;";
412 }
413 }
414  
415 6 my $initial_store_js = '';
416 0 ▶ if ($page->{'initial_store'} && keys %{$page->{'initial_store'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"initial_store"} and keys %{$$page{"initial_store"};}) 0 6 6
Condition33%1/3
$page->{"initial_store"} and keys %{$$page{"initial_store"};}
!ll&&!rl&&r
600
MC/DC0%0/2
$page->{"initial_store"} and keys %{$$page{"initial_store"};}
$page->{"initial_store"} keys %{$$page{"initial_store"};}
Truth table33%1/3
A and B
A= $page->{"initial_store"}B= keys %{$$page{"initial_store"};}
ABresult
0X0
100
111
417 0 my $store_json = encode_json($page->{'initial_store'});
418 0 $initial_store_js = " _initialStore = $store_json;";
419  
420 0 my $has_onmount = 0;
421 0 my $existing_onmount_code = '';
422 0 for my $i (0 .. $#lifecycle_methods) {
423 0 ▶ if ($lifecycle_methods[$i] =~ /^\s*onMount\s*=\s*(.+);$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lifecycle_methods[$i] =~ /^\s*onMount\s*=\s*(.+);$/s) 0 0 0
424 0 $has_onmount = 1;
425 0 $existing_onmount_code = $1;
426 0 splice(@lifecycle_methods, $i, 1);
427 0 last;
428 }
429 }
430  
431 0 ▶ if ($has_onmount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_onmount) { } 0 0 0
432 0 push @lifecycle_methods, " onMount = () => { store.init(this._initialStore); ($existing_onmount_code).call(this); };";
433 } else {
434 0 push @lifecycle_methods, " onMount = () => { store.init(this._initialStore); };";
435 }
436 }
437  
438 6 my $methods_str = '';
439 6 ▶ if (@var_methods || @custom_methods || @computed_methods || @lifecycle_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (@var_methods or @custom_methods or @computed_methods or @lifecycle_methods) 4 2 6
Condition77%7/9
@var_methods or @custom_methods or @computed_methods or @lifecycle_methods
l!l&&r!l&&!r
312
@var_methods or @custom_methods or @computed_methods
l!l&&r!l&&!r
303
@var_methods or @custom_methods
l!l&&r!l&&!r
303
MC/DC50%2/4
@var_methods or @custom_methods or @computed_methods or @lifecycle_methods
@var_methods @custom_methods @computed_methods @lifecycle_methods
Truth table60%3/5
A or B or C or D
A= @var_methodsB= @custom_methodsC= @computed_methodsD= @lifecycle_methods
ABCDresult
1XXX1
01XX1
001X1
00011
00000
440 4 $methods_str = "\n" . join("\n", @var_methods, @custom_methods, @computed_methods, @lifecycle_methods) . "\n";
441 }
442  
443 6 my $routes_js = 'null';
444 6 ▶ if ($page->{routes} && keys %{$page->{routes}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"routes"} and keys %{$$page{"routes"};}) 1 5 6
Condition66%2/3
$page->{"routes"} and keys %{$$page{"routes"};}
!ll&&!rl&&r
501
MC/DC50%1/2
$page->{"routes"} and keys %{$$page{"routes"};}
$page->{"routes"} keys %{$$page{"routes"};}
Truth table66%2/3
A and B
A= $page->{"routes"}B= keys %{$$page{"routes"};}
ABresult
0X0
100
111
445 1 my @route_entries;
446 1 for my $route_path (sort keys %{$page->{routes}}) {
447 2 my $component_name = $page->{routes}{$route_path};
448 2 my $component_class = $component_name;
449 2 $component_class =~ s/-(.)/\U$1/g;
450 2 $component_class = ucfirst($component_class);
451 2 my $pattern = $route_path;
452 2 my @param_names;
453 2 ▶ while ($pattern =~ /:(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /:(\w+)/g) 0 2 2
454 0 push @param_names, $1;
455 }
456 2 $pattern =~ s#:(\w+)#([^/]+)#g;
457 2 $pattern =~ s#/#\\/#g;
458 2 $pattern = "^$pattern\$";
459 0 my $params_js = '[' . join(', ', map { "'$_'" } @param_names) . ']';
460 2 push @route_entries, "{ path: '$route_path', pattern: /$pattern/, component: $component_class, params: $params_js }";
461 }
462 1 $routes_js = '[' . join(', ', @route_entries) . ']';
463 }
464  
465 6 my $css_str = '';
466 6 ▶ my $css = $page->{css} // '';
Condition100%2/2
$page->{"css"} // ""
l!l
42
MC/DC100%1/1
$page->{"css"} // ""
$page->{"css"}
Truth table100%2/2
A
A= $page->{"css"}
Aresult
11
00
467 6 my $less = $page->{less};
468 6 ▶ if ($less) {
Branch: 1 branch
BranchTrueFalseTotal
if ($less) 1 5 6
469 1 eval {
470 1 $css .= join("", CSS::LESSp->parse($less));
471 };
472 1 ▶ warn "LESS compilation failed for $class_name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
473 }
474 6 ▶ if ($css) {
Branch: 1 branch
BranchTrueFalseTotal
if ($css) 5 1 6
475 5 $css =~ s/^\s+//;
476 5 $css =~ s/\s+$//;
477 5 $css =~ s/\\/\\\\/g;
478 5 $css =~ s/'/\\'/g;
479 5 $css =~ s/\n/\\n/g;
480 5 $css_str = "\n _css = '$css';";
481 }
482  
483 6 ▶ my $initial_store_line = $initial_store_js ? "\n$initial_store_js" : '';
Branch: 1 branch
BranchTrueFalseTotal
$initial_store_js ? : 0 6 6
484  
485 6 return <<"CLASS";
486 class $class_name extends Recipe {
487 title = '$title';
488 yamlPath = '$yaml_path';
489 structure = $structure_json;
490 structures = $structures_js;
491 vars = $vars_json;
492 imports = $imports_obj;
493 routes = $routes_js;$css_str$initial_store_line
494 $methods_str
495 }
496 CLASS
497 }
498  
499 #----------------------------------------------------------------------
500 # Recipe compilation
501 #----------------------------------------------------------------------
502  
503 2 ▶sub compile_recipe {
Subroutine compile_recipe: called CC=23 SCAR=34.2
Pod undocumented
504 2 my ($self, $module_name) = @_;
505  
506 2 my $component_file = $self->_find_component_file($self->{recipes_dir}, $module_name);
507 2 ▶ return unless $component_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $component_file 0 2 2
508  
509 2 my $page_data = $self->_load_component_data($component_file);
510 2 $page_data->{yaml_path} = File::Spec->abs2rel($component_file, $self->{www_dir});
511  
512 2 my $class_name = $module_name;
513 2 $class_name =~ s/-(.)/\U$1/g;
514 2 $class_name = ucfirst($class_name);
515  
516 2 my @import_lines;
517 my @import_pairs;
518 2 ▶ my $imports = $page_data->{import} // {};
Condition50%1/2
$page_data->{"import"} // {}
l!l
20
MC/DC0%0/1
$page_data->{"import"} // {}
$page_data->{"import"}
Truth table50%1/2
A
A= $page_data->{"import"}
Aresult
11
00
519  
520 2 for my $namespace (sort keys %$imports) {
521 2 my $import_path = $imports->{$namespace};
522 2 my ($dep_mod_name, $dep_class_name, $dep_js_path) = $self->resolve_recipe_name($import_path);
523  
524 2 my $dep_data = $self->load_page($import_path);
525 2 my @exported_classes = ($dep_class_name);
526  
527 2 ▶ if ($dep_data && $dep_data->{recipes} && ref($dep_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_data and $dep_data->{"recipes"} and ref $dep_data->{"recipes"} eq "HASH") 0 2 2
Condition33%2/6
$dep_data and $dep_data->{"recipes"} and ref $dep_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
200
$dep_data and $dep_data->{"recipes"}
!ll&&!rl&&r
020
MC/DC0%0/3
$dep_data and $dep_data->{"recipes"} and ref $dep_data->{"recipes"} eq "HASH"
$dep_data $dep_data->{"recipes"} ref $dep_data->{"recipes"} eq "HASH"
Truth table25%1/4
A and B and C
A= $dep_dataB= $dep_data->{"recipes"}C= ref $dep_data->{"recipes"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
528 0 for my $recipe_name (sort keys %{$dep_data->{recipes}}) {
529 0 push @exported_classes, "${dep_class_name}_${recipe_name}";
530 }
531 }
532  
533 2 my $imports_list = join(', ', @exported_classes);
534 2 push @import_lines, "import { $imports_list } from './$dep_mod_name.js';";
535  
536 2 my $ns_lower = lc($namespace);
537 2 push @import_pairs, "$ns_lower: $dep_class_name";
538  
539 2 ▶ if ($dep_data && $dep_data->{recipes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_data and $dep_data->{"recipes"}) 0 2 2
Condition33%1/3
$dep_data and $dep_data->{"recipes"}
!ll&&!rl&&r
020
MC/DC0%0/2
$dep_data and $dep_data->{"recipes"}
$dep_data $dep_data->{"recipes"}
Truth table33%1/3
A and B
A= $dep_dataB= $dep_data->{"recipes"}
ABresult
0X0
100
111
540 0 for my $recipe_name (sort keys %{$dep_data->{recipes}}) {
541 0 my $dot_name = "$ns_lower.$recipe_name";
542 0 my $sub_class = "${dep_class_name}_${recipe_name}";
543 0 push @import_pairs, "\"$dot_name\": $sub_class";
544 }
545 }
546 }
547  
548 2 ▶ if ($page_data->{recipes} && ref($page_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH") 1 1 2
Condition66%2/3
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
101
MC/DC50%1/2
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
$page_data->{"recipes"} ref $page_data->{"recipes"} eq "HASH"
Truth table66%2/3
A and B
A= $page_data->{"recipes"}B= ref $page_data->{"recipes"} eq "HASH"
ABresult
0X0
100
111
549 1 for my $recipe_name (sort keys %{$page_data->{recipes}}) {
550 1 my $sub_class = "${class_name}_${recipe_name}";
551 1 $sub_class = ucfirst($sub_class);
552 1 push @import_pairs, "$recipe_name: $sub_class";
553 }
554 }
555  
556 2 ▶ my $imports_obj = @import_pairs ? '{ ' . join(', ', @import_pairs) . ' }' : '{}';
Branch: 1 branch
BranchTrueFalseTotal
@import_pairs ? : 2 0 2
557  
558 2 my @classes;
559  
560 2 ▶ if ($page_data->{recipes} && ref($page_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH") 1 1 2
Condition66%2/3
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
101
MC/DC50%1/2
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
$page_data->{"recipes"} ref $page_data->{"recipes"} eq "HASH"
Truth table66%2/3
A and B
A= $page_data->{"recipes"}B= ref $page_data->{"recipes"} eq "HASH"
ABresult
0X0
100
111
561 1 for my $recipe_name (sort keys %{$page_data->{recipes}}) {
562 1 my $sub_recipe = $page_data->{recipes}{$recipe_name};
563 1 my $sub_class_name = "${class_name}_${recipe_name}";
564 1 $sub_class_name = ucfirst($sub_class_name);
565 1 push @classes, "export " . $self->generate_single_class($sub_class_name, $sub_recipe, '{}');
566 }
567 }
568  
569 2 push @classes, "export " . $self->generate_single_class($class_name, $page_data, $imports_obj);
570  
571 2 my $js_content = '';
572 2 ▶ if (@import_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@import_lines) 2 0 2
573 2 $js_content .= join("\n", @import_lines) . "\n\n";
574 }
575 2 $js_content .= join("\n", @classes);
576  
577 2 my $js_file = File::Spec->catfile($self->{js_dir}, "$module_name.js");
578 2 ▶ open my $fh, '>', $js_file or die "Cannot write $js_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $js_file or die "Cannot write ${js_file}: $!" 2 0 2
579 2 print $fh $js_content;
580 2 close $fh;
581  
582 2 my $meta_file = File::Spec->catfile($self->{js_dir}, "$module_name.js.meta");
583 2 my %mtimes;
584 2 $mtimes{$component_file} = (stat($component_file))[9];
585 2 for my $namespace (keys %$imports) {
586 2 my $import_path = $imports->{$namespace};
587 2 my ($dep_mod_name) = $self->resolve_recipe_name($import_path);
588 2 my $dep_file = $self->_find_component_file($self->{recipes_dir}, $dep_mod_name);
589 2 ▶ $mtimes{$dep_file} = (stat($dep_file))[9] if $dep_file;
Branch: 1 branch
BranchTrueFalseTotal
if $dep_file 2 0 2
590 }
591 2 ▶ open my $meta_fh, '>', $meta_file or die "Cannot write $meta_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $meta_fh, ">", $meta_file or die "Cannot write ${meta_file}: $!" 2 0 2
592 2 print $meta_fh encode_json(\%mtimes);
593 2 close $meta_fh;
594  
595 2 print " Compiled recipe: $module_name -> webroot/js/$module_name.js\n";
596 2 return $js_content;
597 }
598  
599 3 ▶sub compile_recipe_if_stale {
Subroutine compile_recipe_if_stale: called CC=10 SCAR=24.8
Pod undocumented
600 3 my ($self, $module_name) = @_;
601 3 my %paths = $self->get_recipe_cache_paths($module_name);
602  
603 3 ▶ if (-f $paths{js} && -f $paths{meta} && !$ENV{SPIDERPUP_NO_CACHE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $paths{"js"} and -f $paths{"meta"} and not $ENV{"SPIDERPUP_NO_CACHE"}) 1 2 3
Condition66%4/6
-f $paths{"js"} and -f $paths{"meta"} and not $ENV{"SPIDERPUP_NO_CACHE"}
!ll&&!rl&&r
201
-f $paths{"js"} and -f $paths{"meta"}
!ll&&!rl&&r
201
MC/DC33%1/3
-f $paths{"js"} and -f $paths{"meta"} and not $ENV{"SPIDERPUP_NO_CACHE"}
-f $paths{"js"} -f $paths{"meta"} not $ENV{"SPIDERPUP_NO_CACHE"}
Truth table50%2/4
A and B and not(C)
A= -f $paths{"js"}B= -f $paths{"meta"}C= not $ENV{"SPIDERPUP_NO_CACHE"}
ABCresult
0XX0
10X0
1100
1111
604 1 ▶ open my $fh, '<', $paths{meta} or goto COMPILE;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $paths{"meta"} or goto COMPILE 1 0 1
605 1 my $meta = do { local $/; <$fh> };
606 1 close $fh;
607 1 my $cached = eval { decode_json($meta) };
608 1 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) 1 0 1
609 1 my $valid = 1;
610 1 for my $file (keys %$cached) {
611 2 ▶ if (!-f $file || (stat($file))[9] != $cached->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $file or (stat $file)[9] != $cached->{$file}) 0 2 2
Condition33%1/3
not -f $file or (stat $file)[9] != $cached->{$file}
l!l&&r!l&&!r
002
MC/DC0%0/2
not -f $file or (stat $file)[9] != $cached->{$file}
not -f $file (stat $file)[9] != $cached->{$file}
Truth table33%1/3
not(A) or B
A= not -f $fileB= (stat $file)[9] != $cached->{$file}
ABresult
1X1
011
000
612 0 $valid = 0;
613 0 last;
614 }
615 }
616 1 ▶ return if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 1 0 1
617 }
618 }
619  
620 COMPILE:
621 2 $self->compile_recipe($module_name);
622 }
623  
624 #----------------------------------------------------------------------
625 # Page JS compilation
626 #----------------------------------------------------------------------
627  
628 3 ▶sub compile_page_js {
Subroutine compile_page_js: called CC=27 SCAR=34.7
Pod undocumented
629 3 my ($self, $page_name) = @_;
630  
631 3 my $component_file = $self->_find_component_file($self->{pages_dir}, $page_name);
632 3 ▶ return unless $component_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $component_file 0 3 3
633  
634 3 my $page_data = $self->_load_component_data($component_file);
635 3 $page_data->{yaml_path} = File::Spec->abs2rel($component_file, $self->{www_dir});
636  
637 3 my $class_name = $page_name;
638 3 $class_name =~ s|/|_|g;
639 3 $class_name =~ s/-(.)/\U$1/g;
640 3 $class_name = ucfirst($class_name);
641  
642 3 my @segments = split(m|/|, $page_name);
643 3 my $depth = scalar(@segments);
644 3 my $prefix = '../' x $depth;
645  
646 3 my @import_lines;
647 my @import_pairs;
648 3 ▶ my $imports = $page_data->{import} // {};
Condition100%2/2
$page_data->{"import"} // {}
l!l
21
MC/DC100%1/1
$page_data->{"import"} // {}
$page_data->{"import"}
Truth table100%2/2
A
A= $page_data->{"import"}
Aresult
11
00
649  
650 3 for my $namespace (sort keys %$imports) {
651 5 my $import_path = $imports->{$namespace};
652 5 my ($dep_name, $dep_class_name, $dep_js_path, $dep_is_page) = $self->resolve_recipe_name($import_path);
653  
654 5 ▶ if ($dep_is_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_is_page) { } 2 3 5
655 2 $self->compile_page_js_if_stale($dep_name);
656 } else {
657 3 $self->compile_recipe_if_stale($dep_name);
658 }
659  
660 5 my $dep_data = $self->load_page($import_path);
661 5 my @exported_classes = ($dep_class_name);
662  
663 5 ▶ if ($dep_data && $dep_data->{recipes} && ref($dep_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_data and $dep_data->{"recipes"} and ref $dep_data->{"recipes"} eq "HASH") 2 3 5
Condition66%4/6
$dep_data and $dep_data->{"recipes"} and ref $dep_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
302
$dep_data and $dep_data->{"recipes"}
!ll&&!rl&&r
032
MC/DC33%1/3
$dep_data and $dep_data->{"recipes"} and ref $dep_data->{"recipes"} eq "HASH"
$dep_data $dep_data->{"recipes"} ref $dep_data->{"recipes"} eq "HASH"
Truth table50%2/4
A and B and C
A= $dep_dataB= $dep_data->{"recipes"}C= ref $dep_data->{"recipes"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
664 2 for my $recipe_name (sort keys %{$dep_data->{recipes}}) {
665 2 push @exported_classes, "${dep_class_name}_${recipe_name}";
666 }
667 }
668  
669 5 my $imports_list = join(', ', @exported_classes);
670  
671 5 my $rel_path;
672 5 ▶ if ($dep_is_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_is_page) { } 2 3 5
673 2 $rel_path = "${prefix}pages/$dep_name.js";
674 } else {
675 3 $rel_path = "${prefix}$dep_name.js";
676 }
677 5 push @import_lines, "import { $imports_list } from './$rel_path';";
678  
679 5 my $ns_lower = lc($namespace);
680 5 push @import_pairs, "$ns_lower: $dep_class_name";
681  
682 5 ▶ if ($dep_data && $dep_data->{recipes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_data and $dep_data->{"recipes"}) 2 3 5
Condition66%2/3
$dep_data and $dep_data->{"recipes"}
!ll&&!rl&&r
032
MC/DC50%1/2
$dep_data and $dep_data->{"recipes"}
$dep_data $dep_data->{"recipes"}
Truth table66%2/3
A and B
A= $dep_dataB= $dep_data->{"recipes"}
ABresult
0X0
100
111
683 2 for my $recipe_name (sort keys %{$dep_data->{recipes}}) {
684 2 my $dot_name = "$ns_lower.$recipe_name";
685 2 my $sub_class = "${dep_class_name}_${recipe_name}";
686 2 push @import_pairs, "\"$dot_name\": $sub_class";
687 }
688 }
689 }
690  
691 3 ▶ if ($page_data->{recipes} && ref($page_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH") 0 3 3
Condition33%1/3
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
300
MC/DC0%0/2
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
$page_data->{"recipes"} ref $page_data->{"recipes"} eq "HASH"
Truth table33%1/3
A and B
A= $page_data->{"recipes"}B= ref $page_data->{"recipes"} eq "HASH"
ABresult
0X0
100
111
692 0 for my $recipe_name (sort keys %{$page_data->{recipes}}) {
693 0 my $sub_class = "${class_name}_${recipe_name}";
694 0 $sub_class = ucfirst($sub_class);
695 0 push @import_pairs, "$recipe_name: $sub_class";
696 }
697 }
698  
699 3 ▶ my $imports_obj = @import_pairs ? '{ ' . join(', ', @import_pairs) . ' }' : '{}';
Branch: 1 branch
BranchTrueFalseTotal
@import_pairs ? : 2 1 3
700  
701 3 my @classes;
702  
703 3 ▶ if ($page_data->{recipes} && ref($page_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH") 0 3 3
Condition33%1/3
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
300
MC/DC0%0/2
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
$page_data->{"recipes"} ref $page_data->{"recipes"} eq "HASH"
Truth table33%1/3
A and B
A= $page_data->{"recipes"}B= ref $page_data->{"recipes"} eq "HASH"
ABresult
0X0
100
111
704 0 for my $recipe_name (sort keys %{$page_data->{recipes}}) {
705 0 my $sub_recipe = $page_data->{recipes}{$recipe_name};
706 0 my $sub_class_name = "${class_name}_${recipe_name}";
707 0 $sub_class_name = ucfirst($sub_class_name);
708 0 push @classes, "export " . $self->generate_single_class($sub_class_name, $sub_recipe, '{}');
709 }
710 }
711  
712 3 push @classes, "export " . $self->generate_single_class($class_name, $page_data, $imports_obj);
713  
714 3 my $js_content = '';
715 3 ▶ if (@import_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@import_lines) 2 1 3
716 2 $js_content .= join("\n", @import_lines) . "\n\n";
717 }
718 3 $js_content .= join("\n", @classes);
719  
720 3 my $js_file = File::Spec->catfile($self->{js_dir}, 'pages', "$page_name.js");
721 3 my $js_dir = dirname($js_file);
722 3 ▶ make_path($js_dir) unless -d $js_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $js_dir 1 2 3
723  
724 3 ▶ open my $fh, '>', $js_file or die "Cannot write $js_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $js_file or die "Cannot write ${js_file}: $!" 3 0 3
725 3 print $fh $js_content;
726 3 close $fh;
727  
728 3 my $meta_file = File::Spec->catfile($self->{js_dir}, 'pages', "$page_name.js.meta");
729 3 my %mtimes;
730 3 $mtimes{$component_file} = (stat($component_file))[9];
731 3 for my $namespace (keys %$imports) {
732 5 my $import_path = $imports->{$namespace};
733 5 my ($dep_name, undef, undef, $dep_is_page) = $self->resolve_recipe_name($import_path);
734 5 ▶ my $dep_dir = $dep_is_page ? $self->{pages_dir} : $self->{recipes_dir};
Branch: 1 branch
BranchTrueFalseTotal
$dep_is_page ? : 2 3 5
735 5 my $dep_file = $self->_find_component_file($dep_dir, $dep_name);
736 5 ▶ $mtimes{$dep_file} = (stat($dep_file))[9] if $dep_file;
Branch: 1 branch
BranchTrueFalseTotal
if $dep_file 5 0 5
737 }
738 3 ▶ open my $meta_fh, '>', $meta_file or die "Cannot write $meta_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $meta_fh, ">", $meta_file or die "Cannot write ${meta_file}: $!" 3 0 3
739 3 print $meta_fh encode_json(\%mtimes);
740 3 close $meta_fh;
741  
742 3 print " Compiled page JS: $page_name -> webroot/js/pages/$page_name.js\n";
743 3 return $js_content;
744 }
745  
746 4 ▶sub compile_page_js_if_stale {
Subroutine compile_page_js_if_stale: called CC=10 SCAR=24.7
Pod undocumented
747 4 my ($self, $page_name) = @_;
748  
749 4 my $js_file = File::Spec->catfile($self->{js_dir}, 'pages', "$page_name.js");
750 4 my $meta_file = File::Spec->catfile($self->{js_dir}, 'pages', "$page_name.js.meta");
751  
752 4 ▶ if (-f $js_file && -f $meta_file && !$ENV{SPIDERPUP_NO_CACHE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $js_file and -f $meta_file and not $ENV{"SPIDERPUP_NO_CACHE"}) 1 3 4
Condition66%4/6
-f $js_file and -f $meta_file and not $ENV{"SPIDERPUP_NO_CACHE"}
!ll&&!rl&&r
301
-f $js_file and -f $meta_file
!ll&&!rl&&r
301
MC/DC33%1/3
-f $js_file and -f $meta_file and not $ENV{"SPIDERPUP_NO_CACHE"}
-f $js_file -f $meta_file not $ENV{"SPIDERPUP_NO_CACHE"}
Truth table50%2/4
A and B and not(C)
A= -f $js_fileB= -f $meta_fileC= not $ENV{"SPIDERPUP_NO_CACHE"}
ABCresult
0XX0
10X0
1100
1111
753 1 ▶ open my $fh, '<', $meta_file or goto COMPILE;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $meta_file or goto COMPILE 1 0 1
754 1 my $meta = do { local $/; <$fh> };
755 1 close $fh;
756 1 my $cached = eval { decode_json($meta) };
757 1 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) 1 0 1
758 1 my $valid = 1;
759 1 for my $file (keys %$cached) {
760 4 ▶ if (!-f $file || (stat($file))[9] != $cached->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $file or (stat $file)[9] != $cached->{$file}) 0 4 4
Condition33%1/3
not -f $file or (stat $file)[9] != $cached->{$file}
l!l&&r!l&&!r
004
MC/DC0%0/2
not -f $file or (stat $file)[9] != $cached->{$file}
not -f $file (stat $file)[9] != $cached->{$file}
Truth table33%1/3
not(A) or B
A= not -f $fileB= (stat $file)[9] != $cached->{$file}
ABresult
1X1
011
000
761 0 $valid = 0;
762 0 last;
763 }
764 }
765 1 ▶ return if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 1 0 1
766 }
767 }
768  
769 COMPILE:
770 3 $self->compile_page_js($page_name);
771 }
772  
773 #----------------------------------------------------------------------
774 # SSR rendering
775 #----------------------------------------------------------------------
776  
777 0 ▶sub ssr_substitute_vars {
Subroutine ssr_substitute_vars: not called CC=2 SCAR=17.9
Pod undocumented
778 0 my ($self, $func_str, $vars) = @_;
779 0 my $text = $func_str;
780 0 $text =~ s/^function\(\)\{return\s*`(.*)`\}$/$1/s;
781 0 ▶ $text =~ s/\$\{this\.get_(\w+)\(\)\}/defined $vars->{$1} ? $self->_html_escape($vars->{$1}) : ''/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $vars->{$1} ? : 0 0 0
782 0 return $text;
783 }
784  
785 8 ▶sub _html_escape {
Subroutine _html_escape: called CC=2 SCAR=7.0
786 8 my ($self, $text) = @_;
787 8 ▶ return '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 8 8
788 8 $text =~ s/&/&amp;/g;
789 8 $text =~ s/</&lt;/g;
790 8 $text =~ s/>/&gt;/g;
791 8 $text =~ s/"/&quot;/g;
792 8 return $text;
793 }
794  
795 5 ▶sub render_ssr_body {
Subroutine render_ssr_body: called CC=3 SCAR=11.0
Pod undocumented
796 5 my ($self, $structure, $vars, $recipes_map, $slot_children, $slot_vars, $slot_recipes_map) = @_;
797 5 my $html = '';
798 5 ▶ my $children = $structure->{children} // [];
Condition50%1/2
$structure->{"children"} // []
l!l
50
MC/DC0%0/1
$structure->{"children"} // []
$structure->{"children"}
Truth table50%1/2
A
A= $structure->{"children"}
Aresult
11
00
799 5 for my $i (0 .. $#$children) {
800 5 $html .= $self->render_ssr_node($children, $i, $vars, $recipes_map, $slot_children, $slot_vars, $slot_recipes_map, {}, undef);
801 }
802 5 return $html;
803 }
804  
805 10 ▶sub render_ssr_node {
Subroutine render_ssr_node: called CC=55 SCAR=69.8
Pod undocumented
806 # $parent_component_vars: vars of the component that is doing the current rendering
807 # Used so nested components in slot content know their parent component's vars
808 10 my ($self, $siblings, $idx, $vars, $recipes_map, $slot_children, $slot_vars, $slot_recipes_map, $seen, $parent_component_vars) = @_;
809 10 ▶ $seen //= {};
Condition50%1/2
$seen //= {}
l!l
100
MC/DC0%0/1
$seen //= {}
$seen
Truth table50%1/2
A
A= $seen
Aresult
11
00
810 10 ▶ return '' if $seen->{$idx};
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$idx} 0 10 10
811  
812 10 my $node = $siblings->[$idx];
813 10 ▶ return '' unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 10 10
814  
815 10 ▶ if (exists $node->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"content"}) 5 5 10
816 5 return $self->_html_escape($node->{content});
817 }
818  
819 5 ▶ if (exists $node->{'*content'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"*content"}) 0 5 5
820 0 return $self->ssr_substitute_vars($node->{'*content'}, $vars);
821 }
822  
823 5 ▶ my $tag = $node->{tag} // '';
Condition50%1/2
$node->{"tag"} // ""
l!l
50
MC/DC0%0/1
$node->{"tag"} // ""
$node->{"tag"}
Truth table50%1/2
A
A= $node->{"tag"}
Aresult
11
00
824 5 ▶ my $attrs = $node->{attributes} // {};
Condition50%1/2
$node->{"attributes"} // {}
l!l
50
MC/DC0%0/1
$node->{"attributes"} // {}
$node->{"attributes"}
Truth table50%1/2
A
A= $node->{"attributes"}
Aresult
11
00
825 5 ▶ my $children = $node->{children} // [];
Condition50%1/2
$node->{"children"} // []
l!l
50
MC/DC0%0/1
$node->{"children"} // []
$node->{"children"}
Truth table50%1/2
A
A= $node->{"children"}
Aresult
11
00
826  
827 5 ▶ if ($tag eq 'if') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "if") 0 5 5
828 0 for my $j (($idx + 1) .. $#$siblings) {
829 0 my $sib = $siblings->[$j];
830 0 ▶ last unless $sib && $sib->{tag} && ($sib->{tag} eq 'elseif' || $sib->{tag} eq 'else');
Branch: 1 branch
BranchTrueFalseTotal
unless $sib and $sib->{"tag"} and $sib->{"tag"} eq "elseif" || $sib->{"tag"} eq "else" 0 0 0
Condition0%0/9
$sib and $sib->{"tag"} and $sib->{"tag"} eq "elseif" || $sib->{"tag"} eq "else"
!ll&&!rl&&r
000
$sib and $sib->{"tag"}
!ll&&!rl&&r
000
$sib->{"tag"} eq "elseif" or $sib->{"tag"} eq "else"
l!l&&r!l&&!r
000
MC/DC0%0/4
$sib and $sib->{"tag"} and $sib->{"tag"} eq "elseif" || $sib->{"tag"} eq "else"
$sib $sib->{"tag"} $sib->{"tag"} eq "elseif" $sib->{"tag"} eq "else"
Truth table0%0/5
A and B and C or D
A= $sibB= $sib->{"tag"}C= $sib->{"tag"} eq "elseif"D= $sib->{"tag"} eq "else"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
831 0 $seen->{$j} = 1;
832 0 ▶ last if $sib->{tag} eq 'else';
Branch: 1 branch
BranchTrueFalseTotal
if $sib->{"tag"} eq "else" 0 0 0
833 }
834 0 return '<div data-sp-if></div>';
835 }
836  
837 5 ▶ return '' if $tag eq 'elseif' || $tag eq 'else';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "elseif" or $tag eq "else" 0 5 5
Condition33%1/3
$tag eq "elseif" or $tag eq "else"
l!l&&r!l&&!r
005
MC/DC0%0/2
$tag eq "elseif" or $tag eq "else"
$tag eq "elseif" $tag eq "else"
Truth table33%1/3
A or B
A= $tag eq "elseif"B= $tag eq "else"
ABresult
1X1
011
000
838  
839 # <slot/> tag: render the slot children with the parent's vars
840 # Pass $vars (component's own vars) as parent_component_vars so nested
841 # components in slot content know their "parent component" for scoping
842 # Named slots only render children with matching slot="name" attribute;
843 # default (unnamed) slots only render children without a slot attribute.
844 5 ▶ if ($tag eq 'slot') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "slot") 0 5 5
845 0 ▶ if ($slot_children && @$slot_children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot_children and @$slot_children) 0 0 0
Condition0%0/3
$slot_children and @$slot_children
!ll&&!rl&&r
000
MC/DC0%0/2
$slot_children and @$slot_children
$slot_children @$slot_children
Truth table0%0/3
A and B
A= $slot_childrenB= @$slot_children
ABresult
0X0
100
111
846 0 my $slot_name = $attrs->{name};
847 0 my $slot_html = '';
848 0 my $child_seen = {};
849 0 for my $i (0 .. $#$slot_children) {
850 0 my $child = $slot_children->[$i];
851 0 ▶ next unless $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 0 0
852 0 ▶ my $child_slot = ($child->{attributes} // {})->{slot};
Condition0%0/2
$child->{"attributes"} // {}
l!l
00
MC/DC0%0/1
$child->{"attributes"} // {}
$child->{"attributes"}
Truth table0%0/2
A
A= $child->{"attributes"}
Aresult
11
00
853 0 ▶ if ($slot_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot_name) { } 0 0 0
854 0 ▶ next unless defined $child_slot && $child_slot eq $slot_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_slot and $child_slot eq $slot_name 0 0 0
Condition0%0/3
defined $child_slot and $child_slot eq $slot_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $child_slot and $child_slot eq $slot_name
defined $child_slot $child_slot eq $slot_name
Truth table0%0/3
A and B
A= defined $child_slotB= $child_slot eq $slot_name
ABresult
0X0
100
111
855 } else {
856 0 ▶ next if defined $child_slot;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child_slot 0 0 0
857 }
858 0 ▶ $slot_html .= $self->render_ssr_node(
Condition0%0/6
$slot_vars // $vars
l!l&&r!l&&!r
000
$slot_recipes_map // $recipes_map
l!l&&r!l&&!r
000
MC/DC0%0/2
$slot_vars // $vars
$slot_vars $vars
MC/DC0%0/2
$slot_recipes_map // $recipes_map
$slot_recipes_map $recipes_map
Truth table0%0/3
A // B
A= $slot_varsB= $vars
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $slot_recipes_mapB= $recipes_map
ABresult
1X1
011
000
859 $slot_children, $i,
860 $slot_vars // $vars,
861 $slot_recipes_map // $recipes_map,
862 undef, undef, undef, $child_seen,
863 $vars # parent_component_vars = slot-owning component's vars
864 );
865 }
866 0 return "<slot>$slot_html</slot>";
867 }
868 0 return '<slot></slot>';
869 }
870  
871 5 ▶ if ($recipes_map && $recipes_map->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recipes_map and $recipes_map->{$tag}) 0 5 5
Condition33%1/3
$recipes_map and $recipes_map->{$tag}
!ll&&!rl&&r
050
MC/DC0%0/2
$recipes_map and $recipes_map->{$tag}
$recipes_map $recipes_map->{$tag}
Truth table33%1/3
A and B
A= $recipes_mapB= $recipes_map->{$tag}
ABresult
0X0
100
111
872 0 my $mod_info = $recipes_map->{$tag};
873 0 my $mod_data = $mod_info->{data};
874 0 my $mod_vars = {};
875  
876 0 ▶ if ($mod_data->{vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_data->{"vars"}) 0 0 0
877 0 %$mod_vars = %{$mod_data->{vars}};
878 }
879  
880 0 for my $attr (keys %$attrs) {
881 0 ▶ next if $attr =~ /^\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^\*/ 0 0 0
882 0 ▶ next if $attr eq 'for' || $attr eq 'slot';
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "for" or $attr eq "slot" 0 0 0
Condition0%0/3
$attr eq "for" or $attr eq "slot"
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr eq "for" or $attr eq "slot"
$attr eq "for" $attr eq "slot"
Truth table0%0/3
A or B
A= $attr eq "for"B= $attr eq "slot"
ABresult
1X1
011
000
883 0 ▶ next if $attr =~ /^!/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^!/ 0 0 0
884 0 $mod_vars->{$attr} = $attrs->{$attr};
885 }
886  
887 0 my $variant = $node->{variant};
888 0 my $mod_html_raw;
889 0 ▶ if ($variant && $mod_data->{"html_$variant"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant and $$mod_data{"html_$variant"}) { } 0 0 0
Condition0%0/3
$variant and $$mod_data{"html_$variant"}
!ll&&!rl&&r
000
MC/DC0%0/2
$variant and $$mod_data{"html_$variant"}
$variant $$mod_data{"html_$variant"}
Truth table0%0/3
A and B
A= $variantB= $$mod_data{"html_$variant"}
ABresult
0X0
100
111
890 0 $mod_html_raw = $mod_data->{"html_$variant"};
891 } else {
892 0 ▶ $mod_html_raw = $mod_data->{html} // '';
Condition0%0/2
$mod_data->{"html"} // ""
l!l
00
MC/DC0%0/1
$mod_data->{"html"} // ""
$mod_data->{"html"}
Truth table0%0/2
A
A= $mod_data->{"html"}
Aresult
11
00
893 }
894 0 my $mod_structure = parse_html($mod_html_raw, {});
895  
896 0 my $sub_modules_map = $self->_build_recipes_map($mod_data);
897  
898 # Slot content scopes to the parent of the slot-owning component.
899 # $parent_component_vars reflects the component that created this component
900 # (like parentModule in the JS runtime). Falls back to $vars (current context).
901 0 ▶ my $parent_vars = $parent_component_vars // $vars;
Condition0%0/3
$parent_component_vars // $vars
l!l&&r!l&&!r
000
MC/DC0%0/2
$parent_component_vars // $vars
$parent_component_vars $vars
Truth table0%0/3
A // B
A= $parent_component_varsB= $vars
ABresult
1X1
011
000
902  
903 # Pass slot children and parent vars so <slot/> tags can render them with correct scoping
904 0 my $component_html = $self->render_ssr_body(
905 $mod_structure, $mod_vars, $sub_modules_map,
906 $children, $parent_vars, $recipes_map
907 );
908  
909 # Render slot children after template if component has no explicit <slot/> tag
910 # parent_component_vars = mod_vars (this component's vars) so nested components
911 # know their parent component for slot scoping
912 0 ▶ if ($children && @$children && !$self->_has_slot_tag($mod_structure)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children and @$children and not $self->_has_slot_tag($mod_structure)) 0 0 0
Condition0%0/6
$children and @$children and not $self->_has_slot_tag($mod_structure)
!ll&&!rl&&r
000
$children and @$children
!ll&&!rl&&r
000
MC/DC0%0/3
$children and @$children and not $self->_has_slot_tag($mod_structure)
$children @$children not $self->_has_slot_tag($mod_structure)
Truth table0%0/4
A and B and not(C)
A= $childrenB= @$childrenC= not $self->_has_slot_tag($mod_structure)
ABCresult
0XX0
10X0
1100
1111
913 0 my $child_seen = {};
914 0 for my $i (0 .. $#$children) {
915 0 $component_html .= $self->render_ssr_node(
916 $children, $i, $parent_vars, $recipes_map, undef, undef, undef, $child_seen, $mod_vars
917 );
918 }
919 }
920  
921 0 return $component_html;
922 }
923  
924 5 ▶ if ($tag eq 'link') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "link") 3 2 5
925 3 ▶ my $to = $attrs->{to} // '/';
Condition50%1/2
$attrs->{"to"} // "/"
l!l
30
MC/DC0%0/1
$attrs->{"to"} // "/"
$attrs->{"to"}
Truth table50%1/2
A
A= $attrs->{"to"}
Aresult
11
00
926 3 ▶ my $base = $self->{base_url_path} || '';
Condition100%2/2
$self->{'base_url_path'} || ''
l!l
21
MC/DC100%1/1
$self->{'base_url_path'} || ''
$self->{'base_url_path'}
Truth table100%2/2
A
A= $self->{'base_url_path'}
Aresult
11
00
927 3 ▶ my $html_to = ($to eq '/') ? '/' : "$to.html";
Branch: 1 branch
BranchTrueFalseTotal
$to eq '/' ? : 1 2 3
928 3 my $href = $self->_html_escape($base . $html_to);
929 3 my $inner = '';
930 3 my $child_seen = {};
931 3 for my $i (0 .. $#$children) {
932 3 $inner .= $self->render_ssr_node($children, $i, $vars, $recipes_map, $slot_children, $slot_vars, $slot_recipes_map, $child_seen, $parent_component_vars);
933 }
934 3 return "<a href=\"$href\">$inner</a>";
935 }
936  
937 2 ▶ if ($tag eq 'router') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "router") 0 2 2
938 0 return '<div data-router-view></div>';
939 }
940  
941 2 ▶ if ($attrs->{'*for'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"*for"}) 0 2 2
942 0 my $static_attrs = $self->_render_static_attrs($attrs);
943 0 return "<$tag$static_attrs data-sp-for></$tag>";
944 }
945  
946 2 my $static_attrs = $self->_render_static_attrs($attrs);
947  
948 2 ▶ if ($VOID_TAGS{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VOID_TAGS{$tag}) 0 2 2
949 0 return "<$tag$static_attrs />";
950 }
951  
952 2 my $inner = '';
953 2 my $child_seen = {};
954 2 for my $i (0 .. $#$children) {
955 2 $inner .= $self->render_ssr_node($children, $i, $vars, $recipes_map, $slot_children, $slot_vars, $slot_recipes_map, $child_seen, $parent_component_vars);
956 }
957 2 return "<$tag$static_attrs>$inner</$tag>";
958 }
959  
960 2 ▶sub _render_static_attrs {
Subroutine _render_static_attrs: called CC=8 SCAR=38.1
961 2 my ($self, $attrs) = @_;
962 2 my $result = '';
963 2 for my $attr (sort keys %$attrs) {
964 0 ▶ next if $attr =~ /^\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^\*/ 0 0 0
965 0 ▶ next if $attr eq 'for' || $attr eq 'slot' || $attr eq 'condition';
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "for" or $attr eq "slot" or $attr eq "condition" 0 0 0
Condition0%0/6
$attr eq "for" or $attr eq "slot" or $attr eq "condition"
l!l&&r!l&&!r
000
$attr eq "for" or $attr eq "slot"
l!l&&r!l&&!r
000
MC/DC0%0/3
$attr eq "for" or $attr eq "slot" or $attr eq "condition"
$attr eq "for" $attr eq "slot" $attr eq "condition"
Truth table0%0/4
A or B or C
A= $attr eq "for"B= $attr eq "slot"C= $attr eq "condition"
ABCresult
1XX1
01X1
0011
0000
966 0 ▶ next if $attr =~ /^!/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^!/ 0 0 0
967 0 my $val = $attrs->{$attr};
968 0 ▶ next if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 0 0
969 0 $result .= ' ' . $self->_html_escape($attr) . '="' . $self->_html_escape($val) . '"';
970 }
971 2 return $result;
972 }
973  
974 0 ▶sub _has_slot_tag {
Subroutine _has_slot_tag: not called CC=6 SCAR=37.4
975 0 my ($self, $structure) = @_;
976 0 ▶ my $children = $structure->{children} // [];
Condition0%0/2
$structure->{"children"} // []
l!l
00
MC/DC0%0/1
$structure->{"children"} // []
$structure->{"children"}
Truth table0%0/2
A
A= $structure->{"children"}
Aresult
11
00
977 0 for my $child (@$children) {
978 0 ▶ return 1 if ($child->{tag} // '') eq 'slot';
Branch: 1 branch
BranchTrueFalseTotal
if ($child->{"tag"} // "") eq "slot" 0 0 0
Condition0%0/2
$child->{"tag"} // ""
l!l
00
MC/DC0%0/1
$child->{"tag"} // ""
$child->{"tag"}
Truth table0%0/2
A
A= $child->{"tag"}
Aresult
11
00
979 0 ▶ return 1 if $self->_has_slot_tag($child);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_slot_tag($child) 0 0 0
980 }
981 0 return 0;
982 }
983  
984 2 ▶sub _build_recipes_map {
Subroutine _build_recipes_map: called CC=10 SCAR=38.8
985 2 my ($self, $page_data) = @_;
986 2 ▶ my $imports = $page_data->{import} // {};
Condition50%1/2
$page_data->{"import"} // {}
l!l
02
MC/DC0%0/1
$page_data->{"import"} // {}
$page_data->{"import"}
Truth table50%1/2
A
A= $page_data->{"import"}
Aresult
11
00
987 2 my $recipes_map = {};
988  
989 2 for my $namespace (keys %$imports) {
990 0 my $import_path = $imports->{$namespace};
991 0 my $mod_data = $self->load_page($import_path);
992 0 ▶ next unless $mod_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_data 0 0 0
993  
994 0 my ($mod_name, $mod_class) = $self->resolve_recipe_name($import_path);
995 0 my $ns_lower = lc($namespace);
996 0 $recipes_map->{$ns_lower} = { data => $mod_data, class_name => $mod_class };
997  
998 0 ▶ if ($mod_data->{recipes} && ref($mod_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_data->{"recipes"} and ref $mod_data->{"recipes"} eq "HASH") 0 0 0
Condition0%0/3
$mod_data->{"recipes"} and ref $mod_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$mod_data->{"recipes"} and ref $mod_data->{"recipes"} eq "HASH"
$mod_data->{"recipes"} ref $mod_data->{"recipes"} eq "HASH"
Truth table0%0/3
A and B
A= $mod_data->{"recipes"}B= ref $mod_data->{"recipes"} eq "HASH"
ABresult
0X0
100
111
999 0 for my $recipe_name (keys %{$mod_data->{recipes}}) {
1000 $recipes_map->{"$ns_lower.$recipe_name"} = {
1001 0 data => $mod_data->{recipes}{$recipe_name},
1002 class_name => "${mod_class}_${recipe_name}",
1003 };
1004 }
1005 }
1006 }
1007  
1008 2 ▶ if ($page_data->{recipes} && ref($page_data->{recipes}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH") 0 2 2
Condition33%1/3
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
$page_data->{"recipes"} and ref $page_data->{"recipes"} eq "HASH"
$page_data->{"recipes"} ref $page_data->{"recipes"} eq "HASH"
Truth table33%1/3
A and B
A= $page_data->{"recipes"}B= ref $page_data->{"recipes"} eq "HASH"
ABresult
0X0
100
111
1009 0 for my $recipe_name (keys %{$page_data->{recipes}}) {
1010 $recipes_map->{$recipe_name} = {
1011 0 data => $page_data->{recipes}{$recipe_name},
1012 class_name => $recipe_name,
1013 };
1014 }
1015 }
1016  
1017 2 return $recipes_map;
1018 }
1019  
1020 #----------------------------------------------------------------------
1021 # Page compilation (HTML with ES module imports)
1022 #----------------------------------------------------------------------
1023  
1024 2 ▶sub collect_external_assets {
Subroutine collect_external_assets: called CC=5 SCAR=17.5
Pod undocumented
1025 2 my ($self, $page_data) = @_;
1026  
1027 2 my @css_files;
1028 my @js_files;
1029  
1030 2 ▶ my $import_css = $page_data->{'import-css'} // [];
Condition50%1/2
$page_data->{"import-css"} // []
l!l
02
MC/DC0%0/1
$page_data->{"import-css"} // []
$page_data->{"import-css"}
Truth table50%1/2
A
A= $page_data->{"import-css"}
Aresult
11
00
1031 2 for my $css_file (@$import_css) {
1032 0 push @css_files, $css_file;
1033 }
1034  
1035 2 ▶ my $import_js = $page_data->{'import-js'} // [];
Condition50%1/2
$page_data->{"import-js"} // []
l!l
02
MC/DC0%0/1
$page_data->{"import-js"} // []
$page_data->{"import-js"}
Truth table50%1/2
A
A= $page_data->{"import-js"}
Aresult
11
00
1036 2 for my $js_file (@$import_js) {
1037 0 push @js_files, $js_file;
1038 }
1039  
1040 2 return (\@css_files, \@js_files);
1041 }
1042  
1043 2 ▶sub collect_inline_js {
Subroutine collect_inline_js: called CC=5 SCAR=26.8
Pod undocumented
1044 2 my ($self, $page_data) = @_;
1045  
1046 2 my @js_contents;
1047 2 ▶ my $include_js = $page_data->{'include-js'} // [];
Condition50%1/2
$page_data->{"include-js"} // []
l!l
02
MC/DC0%0/1
$page_data->{"include-js"} // []
$page_data->{"include-js"}
Truth table50%1/2
A
A= $page_data->{"include-js"}
Aresult
11
00
1048 2 for my $js_file (@$include_js) {
1049 0 my $file_path = File::Spec->catfile($self->{static_dir}, $js_file);
1050 0 ▶ if (-f $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file_path) { } 0 0 0
1051 0 ▶ open my $fh, '<', $file_path or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file_path or do { warn "Cannot open include-js file ${file_path}: $!"; next } 0 0 0
1052 0 warn "Cannot open include-js file $file_path: $!";
1053 0 next;
1054 };
1055 0 my $content = do { local $/; <$fh> };
1056 0 close $fh;
1057 0 push @js_contents, "/* include-js: $js_file */\n$content";
1058 } else {
1059 0 warn "include-js file not found: $file_path";
1060 }
1061 }
1062  
1063 2 return \@js_contents;
1064 }
1065  
1066 2 ▶sub build_html {
Subroutine build_html: called CC=15 SCAR=30.9
Pod undocumented
1067 2 my ($self, $page_data, $page_name) = @_;
1068 2 ▶ $page_name //= 'index';
Condition50%1/2
$page_name //= "index"
l!l
20
MC/DC0%0/1
$page_name //= "index"
$page_name
Truth table50%1/2
A
A= $page_name
Aresult
11
00
1069  
1070 2 ▶ my $title = $page_data->{title} // 'Untitled';
Condition50%1/2
$page_data->{"title"} // "Untitled"
l!l
20
MC/DC0%0/1
$page_data->{"title"} // "Untitled"
$page_data->{"title"}
Truth table50%1/2
A
A= $page_data->{"title"}
Aresult
11
00
1071  
1072 2 my $class_name = $page_name;
1073 2 $class_name =~ s|/|_|g;
1074 2 $class_name =~ s/-(.)/\U$1/g;
1075 2 $class_name = ucfirst($class_name);
1076  
1077 2 $self->compile_page_js_if_stale($page_name);
1078  
1079 2 ▶ my $css = $page_data->{css} // '';
Condition50%1/2
$page_data->{"css"} // ""
l!l
02
MC/DC0%0/1
$page_data->{"css"} // ""
$page_data->{"css"}
Truth table50%1/2
A
A= $page_data->{"css"}
Aresult
11
00
1080 2 my $less = $page_data->{less};
1081 2 ▶ if ($less) {
Branch: 1 branch
BranchTrueFalseTotal
if ($less) 0 2 2
1082 0 eval { $css .= join("", CSS::LESSp->parse($less)); };
1083 0 ▶ warn "LESS compilation failed for page $page_name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1084 }
1085 2 my $style = '';
1086 2 ▶ if ($css) {
Branch: 1 branch
BranchTrueFalseTotal
if ($css) 0 2 2
1087 0 $css =~ s/^\s+//;
1088 0 $css =~ s/\s+$//;
1089 0 $style = "<style>\n.$page_name { $css }\n</style>";
1090 }
1091  
1092 2 my ($external_css, $external_js) = $self->collect_external_assets($page_data);
1093 2 my $inline_js = $self->collect_inline_js($page_data);
1094  
1095 2 my $css_links = '';
1096 2 for my $css_file (@$external_css) {
1097 0 $css_links .= qq{ <link rel="stylesheet" href="$css_file">\n};
1098 }
1099  
1100 2 my $js_scripts = '';
1101 2 for my $js_file (@$external_js) {
1102 0 $js_scripts .= qq{ <script src="$js_file"></script>\n};
1103 }
1104  
1105 2 my $inline_js_script = '';
1106 2 ▶ if (@$inline_js) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$inline_js) 0 2 2
1107 0 my $inline_content = join("\n\n", @$inline_js);
1108 0 $inline_js_script = " <script>\n$inline_content\n </script>\n";
1109 }
1110  
1111 2 ▶ my $base_url_path = $self->{base_url_path} || '';
Condition100%2/2
$self->{'base_url_path'} || ''
l!l
11
MC/DC100%1/1
$self->{'base_url_path'} || ''
$self->{'base_url_path'}
Truth table100%2/2
A
A= $self->{'base_url_path'}
Aresult
11
00
1112 2 ▶ my $spiderpup_src = $base_url_path ? "$base_url_path/js/spiderpup.js" : "/js/spiderpup.js";
Branch: 1 branch
BranchTrueFalseTotal
$base_url_path ? : 1 1 2
1113  
1114 2 my $recipes_map = $self->_build_recipes_map($page_data);
1115 2 ▶ my $page_html_raw = $page_data->{html} // '';
Condition50%1/2
$page_data->{"html"} // ""
l!l
20
MC/DC0%0/1
$page_data->{"html"} // ""
$page_data->{"html"}
Truth table50%1/2
A
A= $page_data->{"html"}
Aresult
11
00
1116 2 my $page_structure = parse_html($page_html_raw, {});
1117 2 ▶ my $page_vars = $page_data->{vars} // {};
Condition50%1/2
$page_data->{"vars"} // {}
l!l
02
MC/DC0%0/1
$page_data->{"vars"} // {}
$page_data->{"vars"}
Truth table50%1/2
A
A= $page_data->{"vars"}
Aresult
11
00
1118 2 my $ssr_body = $self->render_ssr_body($page_structure, $page_vars, $recipes_map);
1119  
1120 # Skip SSR hydration if body has no meaningful content (only empty placeholders)
1121 2 my $ssr_test = $ssr_body;
1122 2 $ssr_test =~ s/<div data-sp-if><\/div>//g;
1123 2 $ssr_test =~ s/\s//g;
1124 2 ▶ my $ssr_attr = $ssr_test ne '' ? ' data-sp-ssr' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ssr_test ne '' ? : 2 0 2
1125  
1126 2 return <<"HTML";
1127 <!DOCTYPE html>
1128 <html lang="en">
1129 <head>
1130 <meta charset="UTF-8">
1131 <title>$title</title>
1132 $css_links
1133 $style
1134 $js_scripts
1135 $inline_js_script
1136 <script src="$spiderpup_src"></script>
1137 <script type="module">
1138 import { $class_name } from '$base_url_path/js/pages/$page_name.js';
1139  
1140 window.SPIDERPUP_BASE_PATH = '$base_url_path';
1141 document.addEventListener('DOMContentLoaded', function() {
1142 const page = new $class_name();
1143 page.pageName = '$page_name';
1144 document.body.classList.add('$page_name');
1145 if (document.body.hasAttribute('data-sp-ssr')) {
1146 page.hydrateUI();
1147 } else {
1148 page.initUI();
1149 }
1150 });
1151 </script>
1152 </head>
1153 <body$ssr_attr>$ssr_body</body>
1154 </html>
1155 HTML
1156 }
1157  
1158 #----------------------------------------------------------------------
1159 # Request handling
1160 #----------------------------------------------------------------------
1161  
1162 0 ▶sub load_page_from_path {
Subroutine load_page_from_path: not called CC=5 SCAR=34.0
Pod undocumented
1163 0 my ($self, $path) = @_;
1164  
1165 0 my $response = '';
1166  
1167 0 my $page_data;
1168 my $load_error;
1169 0 my $page_name = $self->parse_page_path($path);
1170  
1171 0 eval {
1172 0 $page_data = $self->load_page($page_name);
1173 0 print "Loaded page: $page_name\n";
1174 };
1175 0 ▶ $load_error = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1176  
1177 0 ▶ if ($load_error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($load_error) { } 0 0 0
elsif (defined $page_data) { } 0 0 0
1178 0 my $escaped_error = $load_error;
1179 0 $escaped_error =~ s/&/&amp;/g;
1180 0 $escaped_error =~ s/</&lt;/g;
1181 0 $escaped_error =~ s/>/&gt;/g;
1182 0 $escaped_error =~ s/\n/<br>/g;
1183  
1184 0 my $error_html = <<"ERRORHTML";
1185 <!DOCTYPE html>
1186 <html>
1187 <head>
1188 <title>Spiderpup Error</title>
1189 <style>
1190 .sp-error-overlay {
1191 position: fixed;
1192 top: 0;
1193 left: 0;
1194 right: 0;
1195 bottom: 0;
1196 background: rgba(0, 0, 0, 0.9);
1197 color: #fff;
1198 font-family: monospace;
1199 padding: 40px;
1200 overflow: auto;
1201 z-index: 99999;
1202 }
1203 .sp-error-title {
1204 color: #ff6b6b;
1205 font-size: 24px;
1206 margin-bottom: 20px;
1207 }
1208 .sp-error-message {
1209 background: #1a1a2e;
1210 padding: 20px;
1211 border-radius: 8px;
1212 border-left: 4px solid #ff6b6b;
1213 white-space: pre-wrap;
1214 line-height: 1.6;
1215 }
1216 </style>
1217 </head>
1218 <body>
1219 <div class="sp-error-overlay">
1220 <div class="sp-error-title">Spiderpup Compilation Error</div>
1221 <div class="sp-error-message">$escaped_error</div>
1222 </div>
1223 </body>
1224 </html>
1225 ERRORHTML
1226  
1227 0 my $content_length = length($error_html);
1228 0 $response = "HTTP/1.1 500 Internal Server Error\r\n";
1229 0 $response .= "Content-Type: text/html; charset=utf-8\r\n";
1230 0 $response .= "Content-Length: $content_length\r\n";
1231 0 $response .= "Connection: close\r\n";
1232 0 $response .= "\r\n";
1233 0 $response .= $error_html;
1234 } elsif (defined $page_data) {
1235 0 my $body;
1236 0 eval {
1237 0 print "GET ($path) -> page=$page_name\n";
1238 0 $body = $self->get_cached_html($page_data, $page_name);
1239 };
1240 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1241 0 my $escaped_error = $@;
1242 0 $escaped_error =~ s/&/&amp;/g;
1243 0 $escaped_error =~ s/</&lt;/g;
1244 0 $escaped_error =~ s/>/&gt;/g;
1245 0 $escaped_error =~ s/\n/<br>/g;
1246  
1247 0 $body = <<"ERRORHTML";
1248 <!DOCTYPE html>
1249 <html>
1250 <head>
1251 <title>Spiderpup Error</title>
1252 <style>
1253 .sp-error-overlay {
1254 position: fixed;
1255 top: 0;
1256 left: 0;
1257 right: 0;
1258 bottom: 0;
1259 background: rgba(0, 0, 0, 0.9);
1260 color: #fff;
1261 font-family: monospace;
1262 padding: 40px;
1263 overflow: auto;
1264 z-index: 99999;
1265 }
1266 .sp-error-title {
1267 color: #ff6b6b;
1268 font-size: 24px;
1269 margin-bottom: 20px;
1270 }
1271 .sp-error-message {
1272 background: #1a1a2e;
1273 padding: 20px;
1274 border-radius: 8px;
1275 border-left: 4px solid #ff6b6b;
1276 white-space: pre-wrap;
1277 line-height: 1.6;
1278 }
1279 </style>
1280 </head>
1281 <body>
1282 <div class="sp-error-overlay">
1283 <div class="sp-error-title">Spiderpup Build Error</div>
1284 <div class="sp-error-message">$escaped_error</div>
1285 </div>
1286 </body>
1287 </html>
1288 ERRORHTML
1289 }
1290  
1291 0 my $content_length = length($body);
1292 0 $response = "HTTP/1.1 200 OK\r\n";
1293 0 $response .= "Content-Type: text/html; charset=utf-8\r\n";
1294 0 $response .= "Content-Length: $content_length\r\n";
1295 0 $response .= "Connection: close\r\n";
1296 0 $response .= "\r\n";
1297 0 $response .= $body;
1298 } else {
1299 0 my $not_found = "<!DOCTYPE html><html><body><h1>404 Not Found</h1></body></html>";
1300 0 my $content_length = length($not_found);
1301 0 $response = "HTTP/1.1 404 Not Found\r\n";
1302 0 $response .= "Content-Type: text/html; charset=utf-8\r\n";
1303 0 $response .= "Content-Length: $content_length\r\n";
1304 0 $response .= "Connection: close\r\n";
1305 0 $response .= "\r\n";
1306 0 $response .= $not_found;
1307 }
1308 0 return $response;
1309 }
1310  
1311 #----------------------------------------------------------------------
1312 # HTTP server
1313 #----------------------------------------------------------------------
1314  
1315 0 ▶sub run_server {
Subroutine run_server: not called CC=21 SCAR=61.4
Pod undocumented
1316 0 my ($self, $port, %opts) = @_;
1317 0 ▶ $port //= 5000;
Condition0%0/2
$port //= 5000
l!l
00
MC/DC0%0/1
$port //= 5000
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
1318  
1319 0 $SIG{CHLD} = 'IGNORE';
1320  
1321 0 my $listener;
1322 0 ▶ my $is_ssl = $opts{ssl_cert} && $opts{ssl_key};
Condition0%0/3
$opts{'ssl_cert'} && $opts{'ssl_key'}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{'ssl_cert'} && $opts{'ssl_key'}
$opts{'ssl_cert'} $opts{'ssl_key'}
Truth table0%0/3
A && B
A= $opts{'ssl_cert'}B= $opts{'ssl_key'}
ABresult
0X0
100
111
1323  
1324 0 ▶ if ($is_ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ssl) { } 0 0 0
1325 0 require IO::Socket::SSL;
1326 $listener = IO::Socket::SSL->new(
1327 LocalAddr => '0.0.0.0',
1328 LocalPort => $port,
1329 Proto => 'tcp',
1330 Listen => SOMAXCONN,
1331 Reuse => 1,
1332 SSL_cert_file => $opts{ssl_cert},
1333 SSL_key_file => $opts{ssl_key},
1334 0 ▶ ) or die "Cannot create SSL socket: $! ($IO::Socket::SSL::SSL_ERROR)";
Branch: 1 branch
BranchTrueFalseTotal
$listener = "IO::Socket::SSL"->new("LocalAddr", "0.0.0.0", "LocalPort", $port, "Proto", "tcp", "Listen", 4096, "Reuse", 1, "SSL_cert_file", $opts{"ssl_cert"}, "SSL_key_file", $opts{"ssl_key"}) or die "Cannot create SSL socket: $! ($IO::Socket::SSL::SSL_ERROR)" 0 0 0
1335 } else {
1336 0 ▶ $listener = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
$listener = "IO::Socket::INET"->new("LocalAddr", "0.0.0.0", "LocalPort", $port, "Proto", "tcp", "Listen", 4096, "Reuse", 1) or die "Cannot create socket: $!" 0 0 0
1337 LocalAddr => '0.0.0.0',
1338 LocalPort => $port,
1339 Proto => 'tcp',
1340 Listen => SOMAXCONN,
1341 Reuse => 1,
1342 ) or die "Cannot create socket: $!";
1343 }
1344  
1345 0 ▶ my $scheme = $is_ssl ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$is_ssl ? : 0 0 0
1346 0 print "Server running on $scheme://localhost:$port\n";
1347 0 print "Press Ctrl+C to stop.\n";
1348  
1349 0 my $base_url_path = $self->{base_url_path};
1350  
1351 0 ▶ while (my $conn = $listener->accept) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = $listener->accept) 0 0 0
1352 0 my $pid = fork();
1353  
1354 0 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
1355 0 warn "Fork failed: $!";
1356 0 $conn->close;
1357 0 next;
1358 }
1359  
1360 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
1361 0 $listener->close;
1362  
1363 0 eval {
1364 0 my $request = '';
1365 0 $conn->recv($request, 8192);
1366 0 print "RAW Request: $request\n";
1367 0 my ($request_line) = split(/\r?\n/, $request, 2);
1368 0 ▶ $request_line //= '';
Condition0%0/2
$request_line //= ""
l!l
00
MC/DC0%0/1
$request_line //= ""
$request_line
Truth table0%0/2
A
A= $request_line
Aresult
11
00
1369 0 my ($method, $path, $version) = split(/\s+/, $request_line);
1370  
1371 0 print "Request: $method $path\n";
1372  
1373 0 $path =~ s~^/*$base_url_path~~;
1374  
1375 0 print "Path adjusted to $path\n";
1376  
1377 0 my $response;
1378  
1379 # Serve static files from webroot (JS, etc.)
1380 0 ▶ if ($path =~ /^\/?(.+\.js)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^/?(.+\.js)$]) 0 0 0
1381 0 my $js_file = File::Spec->catfile($self->{webroot_dir}, $1);
1382 0 ▶ if (-f $js_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $js_file) { } 0 0 0
1383 0 print "FOUND $js_file\n";
1384 0 ▶ open my $fh, '<', $js_file or die "Cannot open $js_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $js_file or die "Cannot open ${js_file}: $!" 0 0 0
1385 0 my $body = do { local $/; <$fh> };
1386 0 close $fh;
1387 0 my $content_length = length($body);
1388 0 $response = "HTTP/1.1 200 OK\r\n";
1389 0 $response .= "Content-Type: application/javascript; charset=utf-8\r\n";
1390 0 $response .= "Content-Length: $content_length\r\n";
1391 0 $response .= "Connection: close\r\n";
1392 0 $response .= "\r\n";
1393 0 $response .= $body;
1394 } else {
1395 0 print "missing $js_file\n";
1396 }
1397 }
1398  
1399 # Serve raw YAML/PUP files
1400 0 ▶ if (!$response && $path =~ /\.(yaml|pup)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response and $path =~ /\.(yaml|pup)$/) 0 0 0
Condition0%0/3
not $response and $path =~ /\.(yaml|pup)$/
!ll&&!rl&&r
000
MC/DC0%0/2
not $response and $path =~ /\.(yaml|pup)$/
not $response $path =~ /\.(yaml|pup)$/
Truth table0%0/3
not(A) and B
A= not $responseB= $path =~ /\.(yaml|pup)$/
ABresult
0X0
100
111
1401 0 my $raw_path = $path;
1402 0 $raw_path =~ s~^/~~;
1403 0 my $raw_file = File::Spec->catfile($self->{www_dir}, $raw_path);
1404 0 ▶ if (-f $raw_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $raw_file) 0 0 0
1405 0 ▶ open my $fh, '<', $raw_file or die "Cannot open $raw_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $raw_file or die "Cannot open ${raw_file}: $!" 0 0 0
1406 0 my $body = do { local $/; <$fh> };
1407 0 close $fh;
1408 0 ▶ my $content_type = $raw_file =~ /\.pup$/ ? 'text/plain' : 'text/yaml';
Branch: 1 branch
BranchTrueFalseTotal
$raw_file =~ /\.pup$/ ? : 0 0 0
1409 0 my $content_length = length($body);
1410 0 $response = "HTTP/1.1 200 OK\r\n";
1411 0 $response .= "Content-Type: $content_type; charset=utf-8\r\n";
1412 0 $response .= "Content-Length: $content_length\r\n";
1413 0 $response .= "Connection: close\r\n";
1414 0 $response .= "\r\n";
1415 0 $response .= $body;
1416 }
1417 }
1418  
1419 # Load page from YAML file
1420 0 ▶ if (!$response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
1421 0 $response = $self->load_page_from_path($path);
1422 }
1423  
1424 0 $conn->send($response);
1425 };
1426 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1427 0 warn "Error handling request: $@";
1428 }
1429  
1430 0 $conn->close;
1431 0 exit(0);
1432 }
1433  
1434 0 $conn->close;
1435 }
1436 }
1437  
1438 #----------------------------------------------------------------------
1439 # Batch compilation
1440 #----------------------------------------------------------------------
1441  
1442 0 ▶sub find_yaml_files {
Subroutine find_yaml_files: not called CC=8 SCAR=42.8
Pod undocumented
1443 0 my ($self, $dir, $prefix, $results) = @_;
1444  
1445 0 ▶ opendir(my $dh, $dir) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or return 0 0 0
1446 0 my @entries = readdir($dh);
1447 0 closedir($dh);
1448  
1449 0 for my $entry (@entries) {
1450 0 ▶ next if $entry =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\./ 0 0 0
1451 0 my $path = File::Spec->catfile($dir, $entry);
1452  
1453 0 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
elsif ($entry =~ /\.(yaml|pup)$/) { } 0 0 0
1454 0 ▶ my $new_prefix = $prefix ? "$prefix/$entry" : $entry;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
1455 0 $self->find_yaml_files($path, $new_prefix, $results);
1456 } elsif ($entry =~ /\.(yaml|pup)$/) {
1457 0 my $name = $entry;
1458 0 $name =~ s/\.(yaml|pup)$//;
1459 0 ▶ $name = $prefix ? "$prefix/$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
1460 0 push @$results, $name;
1461 }
1462 }
1463 }
1464  
1465 0 ▶sub compile_all {
Subroutine compile_all: not called CC=13 SCAR=52.0
Pod undocumented
1466 0 my ($self) = @_;
1467 0 print "Compiling all recipes and pages\n";
1468  
1469 0 ▶ make_path($self->{js_dir}) unless -d $self->{js_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"js_dir"} 0 0 0
1470  
1471 0 my $count = 0;
1472 0 my @errors;
1473  
1474 # Phase 1: Compile all recipes to JS files
1475 0 print "--- Compiling recipes ---\n";
1476 0 ▶ opendir(my $mod_dh, $self->{recipes_dir}) or die "Cannot open $self->{recipes_dir}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $mod_dh, $self->{"recipes_dir"} or die qq[Cannot open $self->{"recipes_dir"}: $!] 0 0 0
1477 0 my @module_files = sort grep { /\.(yaml|pup)$/ } readdir($mod_dh);
1478 0 closedir($mod_dh);
1479  
1480 0 for my $mod_file (@module_files) {
1481 0 my $module_name = $mod_file;
1482 0 $module_name =~ s/\.(yaml|pup)$//;
1483  
1484 0 eval { $self->compile_recipe($module_name); };
1485 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
1486 0 push @errors, "recipe $module_name: $@";
1487 0 warn " ERROR: $@";
1488 } else {
1489 0 $count++;
1490 }
1491 }
1492  
1493 # Phase 2: Compile all pages to HTML files
1494 0 print "--- Compiling pages ---\n";
1495 0 my @page_files;
1496 0 $self->find_yaml_files($self->{pages_dir}, '', \@page_files);
1497  
1498 0 for my $page_name (sort @page_files) {
1499 0 print " Compiling page: $page_name\n";
1500  
1501 0 eval {
1502 0 my $page_data = $self->load_page($page_name);
1503 0 ▶ if ($page_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_data) 0 0 0
1504 0 my $html = $self->build_html($page_data, $page_name);
1505 0 my %paths = $self->get_cache_paths($page_name);
1506  
1507 0 my $dir = dirname($paths{html});
1508 0 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
1509  
1510 0 ▶ open my $html_fh, '>', $paths{html} or die "Cannot write $paths{html}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $html_fh, ">", $paths{"html"} or die qq[Cannot write $paths{"html"}: $!] 0 0 0
1511 0 print $html_fh $html;
1512 0 close $html_fh;
1513  
1514 0 my $mtimes = $self->collect_yaml_files($page_data, $page_name);
1515 0 ▶ open my $meta_fh, '>', $paths{meta} or die "Cannot write $paths{meta}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $meta_fh, ">", $paths{"meta"} or die qq[Cannot write $paths{"meta"}: $!] 0 0 0
1516 0 print $meta_fh encode_json($mtimes);
1517 0 close $meta_fh;
1518  
1519 0 $count++;
1520 }
1521 };
1522 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1523 0 push @errors, "page $page_name: $@";
1524 0 warn " ERROR: $@";
1525 }
1526 }
1527  
1528 0 print "\nCompiled $count items\n";
1529 0 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
1530 0 print "Errors:\n";
1531 0 print " $_\n" for @errors;
1532 }
1533 }
1534  
1535 0 ▶sub watch_and_compile {
Subroutine watch_and_compile: not called CC=3 SCAR=24.8
Pod undocumented
1536 0 my ($self, $interval) = @_;
1537 0 ▶ $interval //= 5;
Condition0%0/2
$interval //= 5
l!l
00
MC/DC0%0/1
$interval //= 5
$interval
Truth table0%0/2
A
A= $interval
Aresult
11
00
1538  
1539 0 print "Watching for changes every ${interval}s (Ctrl+C to stop)...\n";
1540  
1541 0 $self->compile_all();
1542 0 $self->update_file_mtimes();
1543  
1544 0 while (1) {
1545 0 sleep $interval;
1546  
1547 0 ▶ if ($self->update_file_mtimes()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->update_file_mtimes) 0 0 0
1548 0 print "\n--- Changes detected at " . localtime() . " ---\n";
1549 0 $self->compile_all();
1550 }
1551 }
1552 }
1553  
1554 1 1;
↑ Summary Spiderpup/SFC.pm »