| 1 | ##============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Document | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Module defining a class of objects which encapsulate compiled | ||
| 7 | # templates, storing additional block definitions and metadata | ||
| 8 | # as well as the compiled Perl sub-routine representing the main | ||
| 9 | # template content. | ||
| 10 | # | ||
| 11 | # AUTHOR | ||
| 12 | # Andy Wardley <abw@wardley.org> | ||
| 13 | # | ||
| 14 | # COPYRIGHT | ||
| 15 | # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. | ||
| 16 | # | ||
| 17 | # This module is free software; you can redistribute it and/or | ||
| 18 | # modify it under the same terms as Perl itself. | ||
| 19 | # | ||
| 20 | #============================================================================ | ||
| 21 | |||
| 22 | package Template::Document; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use base 'Template::Base'; | ||
| 27 | use Template::Constants; | ||
| 28 | |||
| 29 | our $VERSION = '3.105'; | ||
| 30 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 31 | our $ERROR = ''; | ||
| 32 | our ($COMPERR, $AUTOLOAD, $UNICODE); | ||
| 33 | |||
| 34 | BEGIN { | ||
| 35 | # UNICODE is supported in versions of Perl from 5.008 onwards | ||
| 36 | if ($UNICODE = $] > 5.007 ? 1 : 0) { | ||
| 37 | if ($] > 5.008) { | ||
| 38 | # utf8::is_utf8() available from Perl 5.8.1 onwards | ||
| 39 | *is_utf8 = \&utf8::is_utf8; | ||
| 40 | } | ||
| 41 | elsif ($] == 5.008) { | ||
| 42 | # use Encode::is_utf8() for Perl 5.8.0 | ||
| 43 | require Encode; | ||
| 44 | *is_utf8 = \&Encode::is_utf8; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | #======================================================================== | ||
| 51 | # ----- PUBLIC METHODS ----- | ||
| 52 | #======================================================================== | ||
| 53 | |||
| 54 | #------------------------------------------------------------------------ | ||
| 55 | # new(\%document) | ||
| 56 | # | ||
| 57 | # Creates a new self-contained Template::Document object which | ||
| 58 | # encapsulates a compiled Perl sub-routine, $block, any additional | ||
| 59 | # BLOCKs defined within the document ($defblocks, also Perl sub-routines) | ||
| 60 | # and additional $metadata about the document. | ||
| 61 | #------------------------------------------------------------------------ | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my ($class, $doc) = @_; | ||
| 65 | my ($block, $defblocks, $variables, $metadata) = @$doc{ qw( BLOCK DEFBLOCKS VARIABLES METADATA ) }; | ||
| 66 | $defblocks ||= { }; | ||
| 67 | $metadata ||= { }; | ||
| 68 | |||
| 69 | # evaluate Perl code in $block to create sub-routine reference if necessary | ||
| 70 | unless (ref $block) { | ||
| 71 | local $SIG{__WARN__} = \&catch_warnings; | ||
| 72 | $COMPERR = ''; | ||
| 73 | |||
| 74 | # DON'T LOOK NOW! - blindly untainting can make you go blind! | ||
| 75 | { | ||
| 76 | no warnings 'syntax'; | ||
| 77 | $block = each %{ { $block => undef } } if ${^TAINT}; #untaint | ||
| 78 | } | ||
| 79 | |||
| 80 | $block = eval $block; | ||
| 81 | return $class->error($@) | ||
| 82 | unless defined $block; | ||
| 83 | } | ||
| 84 | |||
| 85 | # same for any additional BLOCK definitions | ||
| 86 | @$defblocks{ keys %$defblocks } = | ||
| 87 | # MORE BLIND UNTAINTING - turn away if you're squeamish | ||
| 88 | map { | ||
| 89 | ref($_) | ||
| 90 | ? $_ | ||
| 91 | : ( /(.*)/s && eval($1) or return $class->error($@) ) | ||
| 92 | } values %$defblocks; | ||
| 93 | |||
| 94 | bless { | ||
| 95 | %$metadata, | ||
| 96 | _BLOCK => $block, | ||
| 97 | _DEFBLOCKS => $defblocks, | ||
| 98 | _VARIABLES => $variables, | ||
| 99 | _HOT => 0, | ||
| 100 | }, $class; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | #------------------------------------------------------------------------ | ||
| 105 | # block() | ||
| 106 | # | ||
| 107 | # Returns a reference to the internal sub-routine reference, _BLOCK, | ||
| 108 | # that constitutes the main document template. | ||
| 109 | #------------------------------------------------------------------------ | ||
| 110 | |||
| 111 | sub block { | ||
| 112 | return $_[0]->{ _BLOCK }; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | #------------------------------------------------------------------------ | ||
| 117 | # blocks() | ||
| 118 | # | ||
| 119 | # Returns a reference to a hash array containing any BLOCK definitions | ||
| 120 | # from the template. The hash keys are the BLOCK name and the values | ||
| 121 | # are references to Template::Document objects. Returns 0 (# an empty hash) | ||
| 122 | # if no blocks are defined. | ||
| 123 | #------------------------------------------------------------------------ | ||
| 124 | |||
| 125 | sub blocks { | ||
| 126 | return $_[0]->{ _DEFBLOCKS }; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | #----------------------------------------------------------------------- | ||
| 131 | # variables() | ||
| 132 | # | ||
| 133 | # Returns a reference to a hash of variables used in the template. | ||
| 134 | # This requires the TRACE_VARS option to be enabled. | ||
| 135 | #----------------------------------------------------------------------- | ||
| 136 | |||
| 137 | sub variables { | ||
| 138 | return $_[0]->{ _VARIABLES }; | ||
| 139 | } | ||
| 140 | |||
| 141 | #------------------------------------------------------------------------ | ||
| 142 | # process($context) | ||
| 143 | # | ||
| 144 | # Process the document in a particular context. Checks for recursion, | ||
| 145 | # registers the document with the context via visit(), processes itself, | ||
| 146 | # and then unwinds with a large gin and tonic. | ||
| 147 | #------------------------------------------------------------------------ | ||
| 148 | |||
| 149 | sub process { | ||
| 150 | my ($self, $context) = @_; | ||
| 151 | my $defblocks = $self->{ _DEFBLOCKS }; | ||
| 152 | my $output; | ||
| 153 | |||
| 154 | |||
| 155 | # check we're not already visiting this template | ||
| 156 | return $context->throw( | ||
| 157 | Template::Constants::ERROR_FILE, | ||
| 158 | "recursion into '$self->{ name }'" | ||
| 159 | ) if $self->{ _HOT } && ! $context->{ RECURSION }; ## RETURN ## | ||
| 160 | |||
| 161 | $context->visit($self, $defblocks); | ||
| 162 | |||
| 163 | $self->{ _HOT } = 1; | ||
| 164 | eval { | ||
| 165 | my $block = $self->{ _BLOCK }; | ||
| 166 | $output = &$block($context); | ||
| 167 | }; | ||
| 168 | my $e = $@; # Fragile, capture now | ||
| 169 | $self->{ _HOT } = 0; | ||
| 170 | |||
| 171 | $context->leave(); | ||
| 172 | |||
| 173 | die $context->catch($e) | ||
| 174 | if $e; | ||
| 175 | |||
| 176 | return $output; | ||
| 177 | } | ||
| 178 | |||
| 179 | #------------------------------------------------------------------------ | ||
| 180 | # meta() | ||
| 181 | # | ||
| 182 | # Return the META items, i.e. anything that isn't prefixed with a _, e.g. | ||
| 183 | # _BLOCKS, or the name or modtime items. | ||
| 184 | #------------------------------------------------------------------------ | ||
| 185 | |||
| 186 | sub meta { | ||
| 187 | my $self = shift; | ||
| 188 | return { | ||
| 189 | map { $_ => $self->{ $_ } } | ||
| 190 | grep { ! /^(_|modtime$|name$)/ } | ||
| 191 | keys %$self | ||
| 192 | }; | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | #------------------------------------------------------------------------ | ||
| 197 | # AUTOLOAD | ||
| 198 | # | ||
| 199 | # Provides pseudo-methods for read-only access to various internal | ||
| 200 | # members. | ||
| 201 | #------------------------------------------------------------------------ | ||
| 202 | |||
| 203 | sub AUTOLOAD { | ||
| 204 | my $self = shift; | ||
| 205 | my $method = $AUTOLOAD; | ||
| 206 | |||
| 207 | $method =~ s/.*:://; | ||
| 208 | return if $method eq 'DESTROY'; | ||
| 209 | # my ($pkg, $file, $line) = caller(); | ||
| 210 | # print STDERR "called $self->AUTOLOAD($method) from $file line $line\n"; | ||
| 211 | return $self->{ $method }; | ||
| 212 | } | ||
| 213 | |||
| 214 | #======================================================================== | ||
| 215 | # ----- CLASS METHODS ----- | ||
| 216 | #======================================================================== | ||
| 217 | |||
| 218 | #------------------------------------------------------------------------ | ||
| 219 | # as_perl($content) | ||
| 220 | # | ||
| 221 | # This method expects a reference to a hash passed as the first argument | ||
| 222 | # containing 3 items: | ||
| 223 | # METADATA # a hash of template metadata | ||
| 224 | # BLOCK # string containing Perl sub definition for main block | ||
| 225 | # DEFBLOCKS # hash containing further subs for addional BLOCK defs | ||
| 226 | # It returns a string containing Perl code which, when evaluated and | ||
| 227 | # executed, will instantiate a new Template::Document object with the | ||
| 228 | # above data. On error, it returns undef with an appropriate error | ||
| 229 | # message set in $ERROR. | ||
| 230 | #------------------------------------------------------------------------ | ||
| 231 | |||
| 232 | sub as_perl { | ||
| 233 | my ($class, $content) = @_; | ||
| 234 | my ($block, $defblocks, $metadata) = @$content{ qw( BLOCK DEFBLOCKS METADATA ) }; | ||
| 235 | |||
| 236 | $block =~ s/\s+$//; | ||
| 237 | |||
| 238 | $defblocks = join('', map { | ||
| 239 | my $code = $defblocks->{ $_ }; | ||
| 240 | $code =~ s/\s*$//; | ||
| 241 | " '$_' => $code,\n"; | ||
| 242 | } keys %$defblocks); | ||
| 243 | $defblocks =~ s/\s+$//; | ||
| 244 | |||
| 245 | $metadata = join( | ||
| 246 | '', | ||
| 247 | map { | ||
| 248 | my $x = $metadata->{ $_ }; | ||
| 249 | $x =~ s/(['\\])/\\$1/g; | ||
| 250 | " '$_' => '$x',\n"; | ||
| 251 | } | ||
| 252 | keys %$metadata | ||
| 253 | ); | ||
| 254 | $metadata =~ s/\s+$//; | ||
| 255 | |||
| 256 | return <<EOF | ||
| 257 | #------------------------------------------------------------------------ | ||
| 258 | # Compiled template generated by the Template Toolkit version $Template::VERSION | ||
| 259 | #------------------------------------------------------------------------ | ||
| 260 | |||
| 261 | $class->new({ | ||
| 262 | METADATA => { | ||
| 263 | $metadata | ||
| 264 | }, | ||
| 265 | BLOCK => $block, | ||
| 266 | DEFBLOCKS => { | ||
| 267 | $defblocks | ||
| 268 | }, | ||
| 269 | }); | ||
| 270 | EOF | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | #------------------------------------------------------------------------ | ||
| 275 | # write_perl_file($filename, \%content) | ||
| 276 | # | ||
| 277 | # This method calls as_perl() to generate the Perl code to represent a | ||
| 278 | # compiled template with the content passed as the second argument. | ||
| 279 | # It then writes this to the file denoted by the first argument. | ||
| 280 | # | ||
| 281 | # Returns 1 on success. On error, sets the $ERROR package variable | ||
| 282 | # to contain an error message and returns undef. | ||
| 283 | #------------------------------------------------------------------------ | ||
| 284 | |||
| 285 | sub write_perl_file { | ||
| 286 | my ($class, $file, $content) = @_; | ||
| 287 | my ($fh, $tmpfile); | ||
| 288 | |||
| 289 | return $class->error("invalid filename: " . (defined $file ? $file : '')) | ||
| 290 | unless defined $file && length $file; | ||
| 291 | |||
| 292 | eval { | ||
| 293 | require File::Temp; | ||
| 294 | require File::Basename; | ||
| 295 | ($fh, $tmpfile) = File::Temp::tempfile( | ||
| 296 | DIR => File::Basename::dirname($file) | ||
| 297 | ); | ||
| 298 | my $perlcode = $class->as_perl($content) || die $!; | ||
| 299 | |||
| 300 | if ($UNICODE && is_utf8($perlcode)) { | ||
| 301 | $perlcode = "use utf8;\n\n$perlcode"; | ||
| 302 | binmode $fh, ":utf8"; | ||
| 303 | } | ||
| 304 | print $fh $perlcode; | ||
| 305 | close($fh); | ||
| 306 | }; | ||
| 307 | return $class->error($@) if $@; | ||
| 308 | return rename($tmpfile, $file) | ||
| 309 | || $class->error($!); | ||
| 310 | } | ||
| 311 | |||
| 312 | |||
| 313 | #------------------------------------------------------------------------ | ||
| 314 | # catch_warnings($msg) | ||
| 315 | # | ||
| 316 | # Installed as | ||
| 317 | #------------------------------------------------------------------------ | ||
| 318 | |||
| 319 | sub catch_warnings { | ||
| 320 | $COMPERR .= join('', @_); | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | 1; | ||
| 325 | |||
| 326 | __END__ |