| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## 2013 (c) Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@bis.bg> <cade@biscom.net> <cade@cpan.org> | ||
| 6 | ## | ||
| 7 | ## LICENSE: GPLv2 | ||
| 8 | ## | ||
| 9 | ############################################################################## | ||
| 10 | package Web::Reactor::Preprocessor; | ||
| 11 | use strict; | ||
| 12 | use Exception::Sink; | ||
| 13 | use Carp; | ||
| 14 | use Data::Tools; | ||
| 15 | |||
| 16 | sub new | ||
| 17 | { | ||
| 18 | my $class = shift; | ||
| 19 | my %env = @_; | ||
| 20 | |||
| 21 | $class = ref( $class ) || $class; | ||
| 22 | my $self = { | ||
| 23 | 'ENV' => \%env, | ||
| 24 | 'FILE_CACHE' => {}, | ||
| 25 | }; | ||
| 26 | bless $self, $class; | ||
| 27 | # rcd_log( "debug: rcd_rec:$self created" ); | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | ############################################################################## | ||
| 33 | ## | ||
| 34 | ## | ||
| 35 | ## | ||
| 36 | |||
| 37 | sub load_file | ||
| 38 | { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | my $pn = lc shift; # page name | ||
| 42 | |||
| 43 | die "invalid page name, expected ALPHANUMERIC, got [$pn]" unless $pn =~ /^[a-z_0-9]+$/; | ||
| 44 | |||
| 45 | my $reo = $self->{ 'REO_REACTOR' }; | ||
| 46 | |||
| 47 | if( exists $self->{ 'FILE_CACHE' }{ $pn } ) | ||
| 48 | { | ||
| 49 | # FIXME: log: debug: file cache hit | ||
| 50 | return $self->{ 'FILE_CACHE' }{ $pn }; | ||
| 51 | } | ||
| 52 | |||
| 53 | my $pn = "$pn.html"; | ||
| 54 | my $dirs = $self->{ 'ENV' }{ 'HTML_DIRS' } || []; | ||
| 55 | if( @$dirs == 0 ) | ||
| 56 | { | ||
| 57 | my $app_root = $self->{ 'ENV' }{ 'APP_ROOT' }; | ||
| 58 | boom "missing APP_ROOT" unless -d $app_root; # FIXME: function? get_app_root() | ||
| 59 | $dirs = [ "$app_root/html" ]; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $fn; | ||
| 63 | for my $dir ( @$dirs ) | ||
| 64 | { | ||
| 65 | # FIXME: move this check on create/new of reactor | ||
| 66 | confess "not accessible HTML include dir [$dir]" unless -d $dir; | ||
| 67 | next unless -e "$dir/$pn"; | ||
| 68 | $fn = "$dir/$pn"; | ||
| 69 | last; | ||
| 70 | } | ||
| 71 | |||
| 72 | if( ! $fn ) | ||
| 73 | { | ||
| 74 | # FIXME: not a error really, more like warning, should be able to disable :)) | ||
| 75 | $reo->log( "error: cannot find file [$fn] dirs list [@$dirs]" ); | ||
| 76 | return undef; | ||
| 77 | } | ||
| 78 | |||
| 79 | my $fdata = file_load( $fn ); | ||
| 80 | $self->{ 'FILE_CACHE' }{ $pn } = $fdata; | ||
| 81 | |||
| 82 | return $fdata; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub process | ||
| 86 | { | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | my $text = shift; | ||
| 90 | my $opt = shift || {}; | ||
| 91 | |||
| 92 | $opt = { %$opt }; | ||
| 93 | $opt->{ 'LEVEL' }++; | ||
| 94 | |||
| 95 | # FIXME: cache here? moje bi ne, zaradi modulite | ||
| 96 | $text =~ s/<([\$\&\#])([a-zA-Z_0-9]+)(\s*[^>]*)*>/$self->__process_tag( $1, $2, $3, $opt )/ge; | ||
| 97 | $text =~ s/reactor_((new|back|here)_)?href=([a-z_0-9]+\.([a-z]+)|\.\/?)?\?([^\n\r\s>"']*)/$self->__process_href( $2, $3, $5 )/gie; | ||
| 98 | |||
| 99 | return $text; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub __process_tag | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | my $type = shift; # types are: $ variable, & callback, # template file include | ||
| 107 | my $tag = shift; | ||
| 108 | my $args = shift; # the rest of the tag | ||
| 109 | my $opt = shift; | ||
| 110 | |||
| 111 | $opt = { %$opt }; | ||
| 112 | $opt->{ 'PATH' } .= ", $type$tag"; | ||
| 113 | my $path = $opt->{ 'PATH' }; | ||
| 114 | |||
| 115 | die "preprocess loop detected, tag [$type$tag] path [$path]" if $opt->{ 'SEEN:' . $type . $tag }++; | ||
| 116 | die "empty or invalid tag" unless $tag =~ /^[a-zA-Z_0-9]+$/; | ||
| 117 | |||
| 118 | my $reo = $self->{ 'REO_REACTOR' }; | ||
| 119 | |||
| 120 | $tag = lc $tag; | ||
| 121 | |||
| 122 | my $text; | ||
| 123 | |||
| 124 | if( $type eq '$' ) | ||
| 125 | { | ||
| 126 | # FIXME: get content from reactor? | ||
| 127 | $text = undef unless exists $reo->{ 'HTML_CONTENT' }{ $tag }; | ||
| 128 | $text = $reo->{ 'HTML_CONTENT' }{ $tag }; | ||
| 129 | } | ||
| 130 | elsif( $type eq '#' ) | ||
| 131 | { | ||
| 132 | $text = $self->load_file( $tag ); | ||
| 133 | } | ||
| 134 | elsif( $type eq '&' ) | ||
| 135 | { | ||
| 136 | # FIXME: make args to a function? | ||
| 137 | my %args; | ||
| 138 | while( $args =~ /\s*([^a-zA-Z_0-9]+)(=('([^']*)'|"([^"]*)"|(\S*)))?/g ) # "' # fix string colorization | ||
| 139 | { | ||
| 140 | my $k = uc $1; | ||
| 141 | my $v = $4 || $5 || $6 || 1; | ||
| 142 | $args{ $k } = $v; | ||
| 143 | } | ||
| 144 | $text = $reo->act_call( $tag, HTML_ARGS => \%args ); | ||
| 145 | } | ||
| 146 | else | ||
| 147 | { | ||
| 148 | re_log( "debug: invalid tag: [$type$tag]" ); | ||
| 149 | } | ||
| 150 | |||
| 151 | $text = $self->process( $text, $opt ); | ||
| 152 | |||
| 153 | return $text; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub __process_href | ||
| 157 | { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | my $type = lc shift || 'here'; | ||
| 161 | my $script = shift; | ||
| 162 | my $data = shift; | ||
| 163 | |||
| 164 | my $data_hr = url2hash( $data ); | ||
| 165 | |||
| 166 | my $reo = $self->{ 'REO_REACTOR' }; | ||
| 167 | |||
| 168 | my $href; | ||
| 169 | if( $type eq 'new' ) | ||
| 170 | { | ||
| 171 | $href = $reo->args_new( %$data_hr ); | ||
| 172 | } | ||
| 173 | elsif( $type eq 'back' ) | ||
| 174 | { | ||
| 175 | $href = $reo->args_back( %$data_hr ); | ||
| 176 | } | ||
| 177 | elsif( $type eq 'here' ) | ||
| 178 | { | ||
| 179 | $href = $reo->args_here( %$data_hr ); | ||
| 180 | } | ||
| 181 | else | ||
| 182 | { | ||
| 183 | boom "invalid first argument, expected one of (new|back|here)"; | ||
| 184 | } | ||
| 185 | |||
| 186 | return "href=?_=$href"; | ||
| 187 | } | ||
| 188 | |||
| 189 | ############################################################################## | ||
| 190 | 1; | ||
| 191 | ###EOF######################################################################## |