Reactor/Preprocessor/Native.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 201 CC 18 SCAR 35.8
CC
18
Cov
0%
__process_tag
72.0
load_file
56.0
new
12.0
CRAP 35.9
1 ##############################################################################
2 ##
3 ## Web::Reactor application machinery
4 ## Copyright (c) 2013-2022 Vladi Belperchinov-Shabanski "Cade"
5 ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
6 ## http://cade.noxrun.com
7 ##
8 ## LICENSE: GPLv2
9 ## https://github.com/cade-vs/perl-web-reactor
10 ##
11 ##############################################################################
12 package Web::Reactor::Preprocessor::Native;
13 use strict;
14 use Exception::Sink;
15 use Data::Dumper;
16 use Data::Tools;
17 use Web::Reactor::Preprocessor;
18  
19 use parent 'Web::Reactor::Preprocessor';
20  
21 sub new
22 {
23 my $class = shift;
24 $class = ref( $class ) || $class;
25  
26 my $self = $class->SUPER::new( @_ );
27  
28 $self->{ 'FILE_CACHE' } = {};
29  
30 my $cfg = $self->get_cfg();
31  
32 # FIXME: common directories setup code?
33 if( ! $cfg->{ 'HTML_DIRS' } or @{ $cfg->{ 'HTML_DIRS' } } < 1 )
34 {
35 my $root = $cfg->{ 'APP_ROOT' };
36 my $lang = $cfg->{ 'LANG' };
37 if( $lang )
38 {
39 $cfg->{ 'HTML_DIRS' } = [ "$root/html/$lang", "$root/html/default" ];
40 }
41 else
42 {
43 $cfg->{ 'HTML_DIRS' } = [ "$root/html/default" ];
44 }
45 }
46  
47 my $html_dirs = $cfg->{ 'HTML_DIRS' } || [];
48 @$html_dirs = grep { -d } @$html_dirs;
49  
50 return $self;
51 }
52  
53 ##############################################################################
54 ##
55 ##
56 ##
57  
58 sub load_page
59 {
60 my $self = shift;
61  
62 my $pn = lc shift; # page name
63  
64 return $self->load_file( "page_$pn" );
65 }
66  
67 sub load_file
68 {
69 my $self = shift;
70  
71 my $pn = lc shift; # page name
72  
73 die "invalid page name, expected ALPHANUMERIC, got [$pn]" unless $pn =~ /^[a-z_\-0-9]+$/;
74  
75 my $reo = $self->get_reo();
76 my $cfg = $self->get_cfg();
77  
78 my $lang = $cfg->{ 'LANG' };
79  
80 if( exists $self->{ 'FILE_CACHE' }{ $lang }{ $pn } )
81 {
82 # FIXME: log: debug: file cache hit
83 return $self->{ 'FILE_CACHE' }{ $lang }{ $pn };
84 }
85  
86 my $pn = "$pn.html";
87 my $dirs = $cfg->{ 'HTML_DIRS' };
88  
89 my $fn;
90 for my $dir ( @$dirs )
91 {
92 next unless -e "$dir/$pn";
93 $fn = "$dir/$pn";
94 last;
95 }
96  
97 if( ! $fn )
98 {
99 if( $pn =~ /^page_/ )
100 {
101 $reo->log( "error: cannot load file for page [$pn] from [@$dirs]" );
102 }
103 else
104 {
105 $reo->log( "warning: cannot load file for page [$pn] from [@$dirs]" ) if $reo->is_debug();
106 }
107 return undef;
108 }
109  
110 $reo->log_debug( "debug: preprocessor load page filename [$fn]" );
111 my $fdata = file_text_load( $fn );
112 $self->{ 'FILE_CACHE' }{ $lang }{ $pn } = $fdata;
113  
114 return $fdata;
115 }
116  
117 sub process
118 {
119 my $self = shift;
120  
121 my $pn = lc shift; # page name
122 my $text = shift;
123 my $opt = shift || {};
124 my $ctx = shift || {};
125  
126 boom "too many nesting levels at page [$pn], probable bug in actions or pages" if (caller(128))[0] ne ''; # FIXME: config option for max level
127  
128 $ctx = { %$ctx };
129 $ctx->{ 'LEVEL' }++;
130  
131 # FIXME: cache here? moje bi ne, zaradi modulite
132 $text =~ s/<([\$\&\#]|\$\$)([a-zA-Z_\-0-9]+)(\s*[^>]*)?>/$self->__process_tag( $pn, $1, $2, $3, $opt, $ctx )/ge;
133 $text =~ s/reactor_((new|back|here|none)_)?(href|src)=(["'])?([a-z_0-9]+\.([a-z]+)|\.\/?)?\?([^\n\r\s>"']*)(\4)?/$self->__process_href( $2, $3, $5, $7 )/gie;
134  
135 return $text;
136 }
137  
138 sub __process_tag
139 {
140 my $self = shift;
141  
142 my $pn = lc shift; # page name
143 my $type = shift; # types are: $ variable, & callback, # template file include
144 my $tag = shift;
145 my $args = shift; # the rest of the tag
146 my $opt = shift;
147 my $ctx = shift;
148  
149 $ctx = { %$opt };
150 $ctx->{ 'PATH' } .= ", $type$tag";
151 my $path = $ctx->{ 'PATH' };
152  
153 die "preprocess loop detected, tag [$type$tag] path [$path]" if $ctx->{ 'SEEN:' . $type . $tag }++;
154 die "empty or invalid tag" unless $tag =~ /^[a-zA-Z_\-0-9]+$/;
155  
156 my $reo = $self->get_reo();
157  
158 $tag = lc $tag;
159  
160 my $text;
161  
162 if( $type eq '$$' )
163 {
164 $opt->{ 'SECOND_PASS_REQUIRED' }++;
165 return "<\$$tag>"; # shortcut to deferred eval
166 }
167 elsif( $type eq '$' )
168 {
169 # FIXME: get content from reactor?
170 $text = undef unless exists $reo->{ 'HTML_CONTENT' }{ $tag };
171 $text = $reo->{ 'HTML_CONTENT' }{ $tag };
172 }
173 elsif( $type eq '#' )
174 {
175 $text = $self->load_file( $tag );
176 }
177 elsif( $type eq '&' )
178 {
179 # FIXME: make args to a function?
180 my %args;
181 while( $args =~ /\s*([a-zA-Z_0-9]+)(=('([^']*)'|"([^"]*)"|(\S*)))?/g ) # "' # fix string colorization
182 {
183 my $k = uc $1;
184 my $v = $4 || $5 || $6 || 1;
185 $args{ $k } = $v;
186 }
187 # FIXME: action calls may return non-text data, however the preprocessor expects text data for now...
188 $text = $reo->action_call( $tag, HTML_ARGS => \%args );
189 }
190 else
191 {
192 re_log( "debug: invalid tag: [$type$tag]" );
193 }
194  
195 $text = $self->process( $pn, $text, $opt, $ctx );
196  
197 return $text;
198 }
199  
200 sub __process_href
201 {
202 my $self = shift;
203  
204 my $type = lc shift || 'here';
205 my $attr = shift; # href or src
206 my $script = shift;
207 my $data = shift;
208  
209 my $data_hr = url2hash( $data );
210  
211 my $reo = $self->get_reo();
212  
213 $type = 'new' if $attr eq 'src'; # images
214  
215 my $href = $reo->args_type( $type, %$data_hr );
216  
217 return "$attr=$script?_=$href";
218 }
219  
220 ##############################################################################
221 1;
222 ###EOF########################################################################