| 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::Extended; | ||
| 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 | $self->{ 'DIR_CACHE' } = {}; | ||
| 30 | |||
| 31 | my $cfg = $self->get_cfg(); | ||
| 32 | |||
| 33 | # FIXME: common directories setup code? | ||
| 34 | if( ! $cfg->{ 'HTML_DIRS' } or @{ $cfg->{ 'HTML_DIRS' } } < 1 ) | ||
| 35 | { | ||
| 36 | my $root = $cfg->{ 'APP_ROOT' }; | ||
| 37 | my $lang = $cfg->{ 'LANG' }; | ||
| 38 | if( $lang ) | ||
| 39 | { | ||
| 40 | $cfg->{ 'HTML_DIRS' } = [ "$root/html/$lang", "$root/html/default" ]; | ||
| 41 | } | ||
| 42 | else | ||
| 43 | { | ||
| 44 | $cfg->{ 'HTML_DIRS' } = [ "$root/html/default" ]; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | my $html_dirs = $cfg->{ 'HTML_DIRS' } || []; | ||
| 49 | @$html_dirs = grep { -d } @$html_dirs; | ||
| 50 | |||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | ############################################################################## | ||
| 55 | ## | ||
| 56 | ## | ||
| 57 | ## | ||
| 58 | |||
| 59 | sub load_page | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $pn = lc shift || 'main'; # page name | ||
| 64 | |||
| 65 | return $self->load_file( $pn, 'index' ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub load_file | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | my $pn = lc shift || 'main'; # page name | ||
| 73 | my $fn = lc shift; # file name | ||
| 74 | |||
| 75 | # sanitize page name | ||
| 76 | $pn =~ s/^\/+//o; | ||
| 77 | $pn =~ s/\/+$//o; | ||
| 78 | $pn =~ s/\/+/\//go; | ||
| 79 | |||
| 80 | boom "invalid page name, expected ALPHANUMERIC, got [$pn]" unless $pn =~ /^[a-z_\-0-9\/]+$/; | ||
| 81 | boom "invalid file name, expected ALPHANUMERIC, got [$fn]" unless $fn =~ /^[a-z_\-0-9]+$/; | ||
| 82 | |||
| 83 | my $cfg = $self->get_cfg(); | ||
| 84 | |||
| 85 | my $lang = $cfg->{ 'LANG' }; | ||
| 86 | |||
| 87 | if( exists $self->{ 'FILE_CACHE' }{ $lang }{ $pn }{ $fn } ) | ||
| 88 | { | ||
| 89 | # FIXME: log: debug: file cache hit | ||
| 90 | return $self->{ 'FILE_CACHE' }{ $lang }{ $pn }{ $fn }; | ||
| 91 | } | ||
| 92 | |||
| 93 | my $dirs = []; | ||
| 94 | |||
| 95 | if( exists $self->{ 'DIRS_CACHE' }{ $lang }{ $pn } ) | ||
| 96 | { | ||
| 97 | # FIXME: log: debug: file cache hit | ||
| 98 | $dirs = $self->{ 'DIRS_CACHE' }{ $lang }{ $pn }; | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | my $orgs = $cfg->{ 'HTML_DIRS' }; | ||
| 103 | |||
| 104 | my @pn = grep { $_ } split /\/+/, $pn; | ||
| 105 | |||
| 106 | for( @$orgs ) | ||
| 107 | { | ||
| 108 | my $org = $_; | ||
| 109 | #push @$dirs, $org; | ||
| 110 | for my $pni ( @pn ) | ||
| 111 | { | ||
| 112 | $org .= "/$pni"; | ||
| 113 | push @$dirs, $org; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | push @$dirs, @$orgs; | ||
| 118 | @$dirs = grep { -d } @$dirs; | ||
| 119 | # print STDERR Dumper( 'PREPROCESSOR EXTENDED LOAD FILE DIRS:', $orgs, $pn, \@pn, $dirs ); | ||
| 120 | |||
| 121 | $self->{ 'DIRS_CACHE' }{ $lang }{ $pn } = $dirs; | ||
| 122 | } | ||
| 123 | |||
| 124 | my $reo = $self->get_reo(); | ||
| 125 | |||
| 126 | my $fname; | ||
| 127 | for my $dir ( @$dirs ) | ||
| 128 | { | ||
| 129 | next unless -e "$dir/$fn.html"; | ||
| 130 | $fname = "$dir/$fn.html"; | ||
| 131 | last; | ||
| 132 | } | ||
| 133 | |||
| 134 | if( ! $fname ) | ||
| 135 | { | ||
| 136 | if( $fn eq 'index' ) | ||
| 137 | { | ||
| 138 | $reo->log( "error: cannot load file [$fn] for page [$pn] from [@$dirs]" ); | ||
| 139 | } | ||
| 140 | else | ||
| 141 | { | ||
| 142 | $reo->log( "warning: cannot load file [$fn] for page [$pn] from [@$dirs]" ) if $reo->is_debug(); | ||
| 143 | } | ||
| 144 | return undef; | ||
| 145 | } | ||
| 146 | |||
| 147 | my $fdata = file_text_load( $fname ); | ||
| 148 | |||
| 149 | $reo->log_debug( "debug: preprocessor load page [$pn] file [$fn] OK [$fname]" ); | ||
| 150 | $self->{ 'FILE_CACHE' }{ $lang }{ $pn }{ $fn } = $fdata; | ||
| 151 | |||
| 152 | return $fdata; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub process | ||
| 156 | { | ||
| 157 | my $self = shift; | ||
| 158 | |||
| 159 | my $pn = lc shift; # page name | ||
| 160 | my $text = shift; | ||
| 161 | my $opt = shift || {}; | ||
| 162 | my $ctx = shift || {}; | ||
| 163 | |||
| 164 | #print STDERR "DEBUG: PROCESS PAGE----------------------- [$pn]\n"; | ||
| 165 | |||
| 166 | boom "too many nesting levels at page [$pn], probable bug in actions or page files" if (caller(128))[0] ne ''; # FIXME: config option for max level | ||
| 167 | |||
| 168 | $ctx = { %$ctx }; | ||
| 169 | $ctx->{ 'LEVEL' }++; | ||
| 170 | |||
| 171 | #print STDERR Dumper( 'PROCESS PRE --- ' x 7, $pn, $text ); | ||
| 172 | |||
| 173 | # FIXME: cache here? moje bi ne, zaradi modulite | ||
| 174 | $text =~ s/<([\$\&\#]|\$\$)([a-zA-Z_\-0-9]+)(\s*[^>]*)?>/$self->__process_tag( $pn, $1, $2, $3, $opt, $ctx )/ge; | ||
| 175 | $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; | ||
| 176 | |||
| 177 | #print STDERR Dumper( 'PROCESS POST --- ' x 7, $pn, $text ); | ||
| 178 | |||
| 179 | return $text; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub __process_tag | ||
| 183 | { | ||
| 184 | my $self = shift; | ||
| 185 | |||
| 186 | my $pn = lc shift; # page name | ||
| 187 | my $type = shift; # types are: $ variable, & callback, # template file include | ||
| 188 | my $tag = shift; | ||
| 189 | my $args = shift; # the rest of the tag | ||
| 190 | my $opt = shift; | ||
| 191 | my $ctx = shift; | ||
| 192 | |||
| 193 | #print STDERR "DEBUG: PROCESS PAGE TAG ----------------------- [$pn] [$type] [$tag]\n"; | ||
| 194 | |||
| 195 | #print STDERR Dumper( 'PROCESS ARGS --- ' x 7, ( $pn, $type, $tag, $args, $opt, $ctx ) ); | ||
| 196 | |||
| 197 | $ctx = { %$opt }; | ||
| 198 | $ctx->{ 'PATH' } .= ", $type$tag"; | ||
| 199 | my $path = $ctx->{ 'PATH' }; | ||
| 200 | |||
| 201 | die "preprocess loop detected, tag [$type$tag] path [$path]" if $ctx->{ 'SEEN:' . $type . $tag }++; | ||
| 202 | die "empty or invalid tag" unless $tag =~ /^[a-zA-Z_\-0-9]+$/; | ||
| 203 | |||
| 204 | my $reo = $self->get_reo(); | ||
| 205 | |||
| 206 | $tag = lc $tag; | ||
| 207 | |||
| 208 | my $text; | ||
| 209 | |||
| 210 | if( $type eq '$$' ) | ||
| 211 | { | ||
| 212 | $opt->{ 'SECOND_PASS_REQUIRED' }++; | ||
| 213 | return "<\$$tag>"; # shortcut to deferred eval | ||
| 214 | } | ||
| 215 | elsif( $type eq '$' ) | ||
| 216 | { | ||
| 217 | # FIXME: get content from reactor? | ||
| 218 | $text = undef unless exists $reo->{ 'HTML_CONTENT' }{ $tag }; | ||
| 219 | $text = $reo->{ 'HTML_CONTENT' }{ $tag }; | ||
| 220 | } | ||
| 221 | elsif( $type eq '#' ) | ||
| 222 | { | ||
| 223 | $text = $self->load_file( $pn, $tag ); | ||
| 224 | } | ||
| 225 | elsif( $type eq '&' ) | ||
| 226 | { | ||
| 227 | # FIXME: make args to a function? | ||
| 228 | my %args; | ||
| 229 | while( $args =~ /\s*([a-zA-Z_0-9]+)(=('([^']*)'|"([^"]*)"|(\S*)))?/g ) # "' # fix string colorization | ||
| 230 | { | ||
| 231 | my $k = uc $1; | ||
| 232 | my $v = $4 || $5 || $6 || 1; | ||
| 233 | $args{ $k } = $v; | ||
| 234 | } | ||
| 235 | # FIXME: action calls may return non-text data, however the preprocessor expects text data for now... | ||
| 236 | $text = $reo->act->call( $tag, HTML_ARGS => \%args ); | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { | ||
| 240 | re_log( "debug: invalid tag: [$type$tag]" ); | ||
| 241 | } | ||
| 242 | |||
| 243 | # print STDERR Dumper( 'PROCESS TEXT --- ' x 7, ( $pn, $text, $opt, $ctx ) ); | ||
| 244 | $text = $self->process( $pn, $text, $opt, $ctx ); | ||
| 245 | |||
| 246 | return $text; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub __process_href | ||
| 250 | { | ||
| 251 | my $self = shift; | ||
| 252 | |||
| 253 | my $type = lc shift || 'here'; | ||
| 254 | my $attr = shift; # href or src | ||
| 255 | my $script = shift; | ||
| 256 | my $data = shift; | ||
| 257 | |||
| 258 | my $data_hr = url2hash( $data ); | ||
| 259 | |||
| 260 | my $reo = $self->get_reo(); | ||
| 261 | |||
| 262 | $type = 'new' if $attr eq 'src'; # images | ||
| 263 | |||
| 264 | my $href = $reo->args_type( $type, %$data_hr ); | ||
| 265 | |||
| 266 | return "$attr=$script?_=$href"; | ||
| 267 | } | ||
| 268 | |||
| 269 | ############################################################################## | ||
| 270 | 1; | ||
| 271 | ###EOF######################################################################## |