| 1 | # | ||
| 2 | # | ||
| 3 | # Copyright (C) 2006-2010 Andrew Speer <andrew@webdyne.org>. All rights | ||
| 4 | # reserved. | ||
| 5 | # | ||
| 6 | # This file is part of WebDyne::Template. | ||
| 7 | # | ||
| 8 | # WebDyne::Template is free software; you can redistribute it and/or modify | ||
| 9 | # it under the terms of the GNU General Public License as published by | ||
| 10 | # the Free Software Foundation; either version 2 of the License, or | ||
| 11 | # (at your option) any later version. | ||
| 12 | # | ||
| 13 | # This program is distributed in the hope that it will be useful, | ||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | # GNU General Public License for more details. | ||
| 17 | # | ||
| 18 | # You should have received a copy of the GNU General Public License | ||
| 19 | # along with this program; if not, write to the Free Software | ||
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 21 | # | ||
| 22 | # | ||
| 23 | package WebDyne::Template; | ||
| 24 | |||
| 25 | |||
| 26 | # Compiler Pragma | ||
| 27 | # | ||
| 28 | 1 | ▶ | use strict qw(vars); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | |||
| 32 | # Webmod Modules. | ||
| 33 | # | ||
| 34 | 1 | ▶ | use WebDyne::Constant; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 0 | ▶ | use WebDyne::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 36 | |||
| 37 | |||
| 38 | # External modules | ||
| 39 | # | ||
| 40 | use File::Spec; | ||
| 41 | |||
| 42 | |||
| 43 | # Version information in a formate suitable for CPAN etc. Must be | ||
| 44 | # all on one line | ||
| 45 | # | ||
| 46 | $VERSION='1.002'; | ||
| 47 | |||
| 48 | |||
| 49 | # Debug | ||
| 50 | # | ||
| 51 | 0 && debug("%s loaded, version $VERSION", __PACKAGE__); | ||
| 52 | |||
| 53 | |||
| 54 | # And done | ||
| 55 | # | ||
| 56 | 1; | ||
| 57 | |||
| 58 | #------------------------------------------------------------------------------ | ||
| 59 | |||
| 60 | |||
| 61 | sub import { | ||
| 62 | |||
| 63 | |||
| 64 | # Will only work if called from within a __PERL__ block in WebDyne | ||
| 65 | # | ||
| 66 | my ($class, @param)=@_; | ||
| 67 | my $self_cr=UNIVERSAL::can(scalar caller, 'self') || return; | ||
| 68 | my $self=$self_cr->() || return; | ||
| 69 | $self->set_handler('WebDyne::Chain'); | ||
| 70 | #$self->set_handler('WebDyne::Template'); | ||
| 71 | my %param=(@param==1) ? (webdynetemplate => @param) : @param; | ||
| 72 | my $meta_hr=$self->meta(); | ||
| 73 | push @{$meta_hr->{'webdynechain'}}, __PACKAGE__; | ||
| 74 | push @{$meta_hr->{'webdynefilter'}}, __PACKAGE__; | ||
| 75 | map { $meta_hr->{lc($_)}=$param{$_} } keys %param; | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | sub handler : method { | ||
| 81 | |||
| 82 | |||
| 83 | # Add ourselves as a filter | ||
| 84 | # | ||
| 85 | my ($self, $r)=(shift, shift); | ||
| 86 | $self->set_filter(__PACKAGE__); | ||
| 87 | $self->SUPER::handler($r, @_); | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub template { | ||
| 93 | |||
| 94 | |||
| 95 | # Name of template in use. Cannot be set here, too late - read only | ||
| 96 | # | ||
| 97 | my $self=shift(); | ||
| 98 | my $r=$self->r() || return err(); | ||
| 99 | my $meta_hr=$self->meta() || return err(); | ||
| 100 | my $template_cn=$meta_hr->{'webdynetemplate'} || $r->dir_config('WebDyneTemplate'); | ||
| 101 | $template_cn || return err('no template file name specified %s !', Data::Dumper::Dumper($meta_hr)); | ||
| 102 | |||
| 103 | # Must be full path, if not use current dir | ||
| 104 | # | ||
| 105 | unless ((File::Spec->splitpath($template_cn))[1]) { | ||
| 106 | |||
| 107 | # No dir, must use cwd | ||
| 108 | # | ||
| 109 | my $dn=(File::Spec->splitpath($r->filename()))[1]; | ||
| 110 | $template_cn=File::Spec->catfile($dn, $template_cn); | ||
| 111 | |||
| 112 | } | ||
| 113 | \$template_cn | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | sub source_mtime { | ||
| 119 | |||
| 120 | |||
| 121 | # Get latest srce mtime for source file and template so engine can | ||
| 122 | # determine if cache is stale | ||
| 123 | # | ||
| 124 | my ($self, $srce_mtime)=@_; | ||
| 125 | 0 && debug('menu source mtime'); | ||
| 126 | |||
| 127 | |||
| 128 | # Get request object | ||
| 129 | # | ||
| 130 | my $r=$self->r() || return err(); | ||
| 131 | |||
| 132 | |||
| 133 | # Get full path, mtime of menu template | ||
| 134 | # | ||
| 135 | my $template_cn=${ $self->template() || return err() }; | ||
| 136 | my $template_mtime=(stat($template_cn))[9] || | ||
| 137 | return err("could not stat $template_cn, $!"); | ||
| 138 | |||
| 139 | |||
| 140 | # Get appropriate mtime | ||
| 141 | # | ||
| 142 | my $return_mtime=($srce_mtime > $template_mtime) ? $srce_mtime : $template_mtime; | ||
| 143 | 0 && debug("returning mtime $return_mtime"); | ||
| 144 | |||
| 145 | |||
| 146 | # Return whichever is greater | ||
| 147 | # | ||
| 148 | return \$return_mtime; | ||
| 149 | |||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub filter { | ||
| 155 | |||
| 156 | |||
| 157 | # The real guts. Wedge one HTML page into a wrapper page | ||
| 158 | # | ||
| 159 | my ($self, $data_main_ar, $meta_main_hr)=@_; | ||
| 160 | 0 && debug("in $self filter"); | ||
| 161 | |||
| 162 | |||
| 163 | # Get request object | ||
| 164 | # | ||
| 165 | my $r=$self->r() || | ||
| 166 | return err('unable to get request object'); | ||
| 167 | |||
| 168 | |||
| 169 | # Get the template path name | ||
| 170 | # | ||
| 171 | my $template_cn=${ $self->template() || return err() }; | ||
| 172 | 0 && debug("template_pn $template_cn, %s", ref($r)); | ||
| 173 | $template_cn || return err('no template file name specified'); | ||
| 174 | |||
| 175 | |||
| 176 | # If user is looking at template, don't try and | ||
| 177 | # recursively compile it for them, bad things happen, | ||
| 178 | # just show as is | ||
| 179 | # | ||
| 180 | ($template_cn eq $r->filename()) && | ||
| 181 | return $data_main_ar; | ||
| 182 | |||
| 183 | |||
| 184 | # Get the template structure data ref | ||
| 185 | # | ||
| 186 | my $container_ar=$self->compile({ | ||
| 187 | |||
| 188 | srce => $template_cn, | ||
| 189 | stage1 => 1, | ||
| 190 | |||
| 191 | }) || return err(); | ||
| 192 | my ($meta_template_hr, $data_template_ar)=@{$container_ar}; | ||
| 193 | |||
| 194 | |||
| 195 | # Concatenate meta perl sections | ||
| 196 | # | ||
| 197 | #my $perl_main_ar=$meta_main_hr->{'perl'}; | ||
| 198 | my $perl_template_ar=$meta_template_hr->{'perl'}; | ||
| 199 | push @{$meta_main_hr->{'perl'}}, @{$perl_template_ar}; | ||
| 200 | |||
| 201 | |||
| 202 | # Below fixes up HEAD section | ||
| 203 | # | ||
| 204 | |||
| 205 | |||
| 206 | # Find body block, ie <head> tag in data ref | ||
| 207 | # | ||
| 208 | my $data_template_head_ar=($self->find_node({ | ||
| 209 | |||
| 210 | data_ar => $data_template_ar, | ||
| 211 | tag => 'head' | ||
| 212 | |||
| 213 | }) || return err())->[0]; | ||
| 214 | 0 && debug("data_template_head_ar $data_template_head_ar %s", Dumper($data_template_head_ar)); | ||
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | # Find the *parent* of the <block name=head> tag in the menu code. Note that this | ||
| 219 | # block in not necessarily immediately under the <head> tag, may be buried | ||
| 220 | # further down under a table etc | ||
| 221 | # | ||
| 222 | my $data_template_head_block_prnt_ar=($self->find_node({ | ||
| 223 | |||
| 224 | data_ar => $data_template_head_ar, | ||
| 225 | tag => 'block', | ||
| 226 | attr_hr => { name=>'head' }, | ||
| 227 | prnt_fg => 1, | ||
| 228 | |||
| 229 | }) || return err())->[0]; | ||
| 230 | 0 && debug("data_template_head_block_prnt_ar $data_template_head_block_prnt_ar %s", | ||
| 231 | Dumper($data_template_head_block_prnt_ar)); | ||
| 232 | |||
| 233 | |||
| 234 | # Get the actual <block name=head> data ref | ||
| 235 | # | ||
| 236 | my $data_template_head_block_ar=($self->find_node({ | ||
| 237 | |||
| 238 | data_ar => $data_template_ar, | ||
| 239 | tag => 'block', | ||
| 240 | attr_hr => { name=>'head' } | ||
| 241 | |||
| 242 | }) || return err())->[0]; | ||
| 243 | 0 && debug("data_template_head_block_ar $data_template_head_block_ar %s", | ||
| 244 | Dumper($data_template_head_block_ar)); | ||
| 245 | |||
| 246 | |||
| 247 | # Get the <head> section from the main HTML page, ie the page to be | ||
| 248 | # embedded | ||
| 249 | # | ||
| 250 | my $data_main_head_ar=($self->find_node({ | ||
| 251 | |||
| 252 | data_ar => $data_main_ar, | ||
| 253 | tag => 'head' | ||
| 254 | |||
| 255 | }) || return err())->[0]; | ||
| 256 | 0 && debug("data_main_head_ar $data_main_head_ar %s", | ||
| 257 | Dumper($data_main_head_ar)); | ||
| 258 | |||
| 259 | |||
| 260 | # Concatenate titles | ||
| 261 | # | ||
| 262 | my $data_main_title_ar=($self->find_node({ | ||
| 263 | |||
| 264 | data_ar => $data_main_ar, | ||
| 265 | tag => 'title' | ||
| 266 | |||
| 267 | }) || return err())->[0]; | ||
| 268 | my $data_template_title_ar=($self->find_node({ | ||
| 269 | |||
| 270 | data_ar => $data_template_ar, | ||
| 271 | tag => 'title' | ||
| 272 | |||
| 273 | }) || return err())->[0]; | ||
| 274 | $data_main_title_ar->[$WEBDYNE_NODE_CHLD_IX][0]=join(' - ', grep {$_} | ||
| 275 | $data_template_title_ar->[$WEBDYNE_NODE_CHLD_IX][0],$data_main_title_ar->[$WEBDYNE_NODE_CHLD_IX][0]); | ||
| 276 | #0 && debug('titles, %s, %s', Dumper($data_main_title_ar, $data_template_title_ar)); | ||
| 277 | |||
| 278 | |||
| 279 | # Replace menu head attr with any head attr from main page | ||
| 280 | # | ||
| 281 | $data_template_head_ar->[$WEBDYNE_NODE_ATTR_IX]= | ||
| 282 | $data_main_head_ar->[$WEBDYNE_NODE_ATTR_IX]; | ||
| 283 | |||
| 284 | |||
| 285 | # Search for head block in head block parent | ||
| 286 | # | ||
| 287 | foreach my $data_chld_ix (0 .. $#{$data_template_head_block_prnt_ar->[$WEBDYNE_NODE_CHLD_IX]}) { | ||
| 288 | |||
| 289 | |||
| 290 | # Skip if not found | ||
| 291 | # | ||
| 292 | my $data_chld_ar=$data_template_head_block_prnt_ar->[$WEBDYNE_NODE_CHLD_IX][$data_chld_ix]; | ||
| 293 | next unless ($data_chld_ar eq $data_template_head_block_ar); | ||
| 294 | |||
| 295 | |||
| 296 | # Must have found node if get to here, splice in head | ||
| 297 | # | ||
| 298 | splice @{$data_template_head_block_prnt_ar->[$WEBDYNE_NODE_CHLD_IX]},$data_chld_ix,1, | ||
| 299 | @{$data_main_head_ar->[$WEBDYNE_NODE_CHLD_IX]}; | ||
| 300 | last; | ||
| 301 | |||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | # Below fixes up BODY section | ||
| 306 | # | ||
| 307 | |||
| 308 | |||
| 309 | # Find body block, ie <body> tag in data ref | ||
| 310 | # | ||
| 311 | my $data_template_body_ar=($self->find_node({ | ||
| 312 | |||
| 313 | data_ar => $data_template_ar, | ||
| 314 | tag => 'body' | ||
| 315 | |||
| 316 | }) || return err())->[0]; | ||
| 317 | 0 && debug("data_template_body_ar $data_template_body_ar %s", Dumper($data_template_body_ar)); | ||
| 318 | |||
| 319 | |||
| 320 | |||
| 321 | # Find the *parent* of the <block name=body> tag in the menu code. Note that this | ||
| 322 | # block in not neccessarily immediately under the <body> tag, may be buried | ||
| 323 | # further down under a table etc | ||
| 324 | # | ||
| 325 | my $data_template_body_block_prnt_ar=($self->find_node({ | ||
| 326 | |||
| 327 | data_ar => $data_template_body_ar, | ||
| 328 | tag => 'block', | ||
| 329 | attr_hr => { name=>'body' }, | ||
| 330 | prnt_fg => 1, | ||
| 331 | |||
| 332 | }) || return err())->[0]; | ||
| 333 | 0 && debug("data_template_body_block_prnt_ar $data_template_body_block_prnt_ar %s", | ||
| 334 | Dumper($data_template_body_block_prnt_ar)); | ||
| 335 | |||
| 336 | |||
| 337 | # Get the actual <block name=body> data ref | ||
| 338 | # | ||
| 339 | my $data_template_body_block_ar=($self->find_node({ | ||
| 340 | |||
| 341 | data_ar => $data_template_ar, | ||
| 342 | tag => 'block', | ||
| 343 | attr_hr => { name=>'body' } | ||
| 344 | |||
| 345 | }) || return err())->[0]; | ||
| 346 | 0 && debug("data_template_body_block_ar $data_template_body_block_ar %s", | ||
| 347 | Dumper($data_template_body_block_ar)); | ||
| 348 | |||
| 349 | |||
| 350 | # Get the <body> section from the main HTML page, ie the page to be | ||
| 351 | # embedded | ||
| 352 | # | ||
| 353 | my $data_main_body_ar=($self->find_node({ | ||
| 354 | |||
| 355 | data_ar => $data_main_ar, | ||
| 356 | tag => 'body' | ||
| 357 | |||
| 358 | }) || return err())->[0]; | ||
| 359 | 0 && debug("data_main_body_ar $data_main_body_ar %s", | ||
| 360 | Dumper($data_main_body_ar)); | ||
| 361 | |||
| 362 | |||
| 363 | # Replace menu body attr with any body attr from main page | ||
| 364 | # | ||
| 365 | $data_template_body_ar->[$WEBDYNE_NODE_ATTR_IX]= | ||
| 366 | $data_main_body_ar->[$WEBDYNE_NODE_ATTR_IX]; | ||
| 367 | |||
| 368 | |||
| 369 | # Search for body block in body block parent | ||
| 370 | # | ||
| 371 | foreach my $data_chld_ix (0 .. $#{$data_template_body_block_prnt_ar->[$WEBDYNE_NODE_CHLD_IX]}) { | ||
| 372 | |||
| 373 | |||
| 374 | # Skip if not found | ||
| 375 | # | ||
| 376 | my $data_chld_ar=$data_template_body_block_prnt_ar->[$WEBDYNE_NODE_CHLD_IX][$data_chld_ix]; | ||
| 377 | next unless ($data_chld_ar eq $data_template_body_block_ar); | ||
| 378 | |||
| 379 | |||
| 380 | # Must have found node if get to here, splice in body | ||
| 381 | # | ||
| 382 | splice @{$data_template_body_block_prnt_ar->[$WEBDYNE_NODE_CHLD_IX]},$data_chld_ix,1, | ||
| 383 | @{$data_main_body_ar->[$WEBDYNE_NODE_CHLD_IX]}; | ||
| 384 | last; | ||
| 385 | |||
| 386 | |||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | # All done, pass onto next filter | ||
| 391 | # | ||
| 392 | return $data_template_ar; | ||
| 393 | |||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | __END__ | ||