Fix bugs in perl VMXTemplate
parent
c5de6fbb89
commit
cf6ad41c12
|
@ -32,8 +32,9 @@ use strict;
|
||||||
use Digest::MD5 qw(md5_hex);
|
use Digest::MD5 qw(md5_hex);
|
||||||
use POSIX;
|
use POSIX;
|
||||||
|
|
||||||
|
use VMXTemplate::Utils;
|
||||||
use VMXTemplate::Options;
|
use VMXTemplate::Options;
|
||||||
use VMXTemplate::Compiler;
|
use VMXTemplate::Parser;
|
||||||
|
|
||||||
# Version of code classes, saved into compiled files
|
# Version of code classes, saved into compiled files
|
||||||
use constant CODE_VERSION => 4;
|
use constant CODE_VERSION => 4;
|
||||||
|
@ -97,7 +98,7 @@ sub vars
|
||||||
sub parse
|
sub parse
|
||||||
{
|
{
|
||||||
my ($self, $fn, $vars) = @_;
|
my ($self, $fn, $vars) = @_;
|
||||||
return $self->parse_real($fn, undef, '_main', $vars);
|
return $self->parse_real($fn, undef, undef, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Call named block/function from a template
|
# Call named block/function from a template
|
||||||
|
@ -112,7 +113,7 @@ sub exec_from
|
||||||
sub parse_inline
|
sub parse_inline
|
||||||
{
|
{
|
||||||
my ($self, $code, $vars) = @_;
|
my ($self, $code, $vars) = @_;
|
||||||
return $self->parse_real(undef, $_[1], '_main', $vars);
|
return $self->parse_real(undef, $_[1], undef, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Call function from a string parsed as a template
|
# Call function from a string parsed as a template
|
||||||
|
@ -141,7 +142,7 @@ sub parse_real
|
||||||
# Load code
|
# Load code
|
||||||
if ($filename)
|
if ($filename)
|
||||||
{
|
{
|
||||||
$filename = $self->{root}.$filename if $filename !~ m!^/!so;
|
$filename = $self->{options}->{root}.$filename if $filename !~ m!^/!so;
|
||||||
unless ($text = $self->loadfile($filename))
|
unless ($text = $self->loadfile($filename))
|
||||||
{
|
{
|
||||||
$self->{options}->error("couldn't load template file '$filename'");
|
$self->{options}->error("couldn't load template file '$filename'");
|
||||||
|
@ -178,14 +179,19 @@ sub _run
|
||||||
my ($code, $is_outer, $function, $filename, $vars) = @_;
|
my ($code, $is_outer, $function, $filename, $vars) = @_;
|
||||||
$function ||= ':main';
|
$function ||= ':main';
|
||||||
my $str = $code->{$function};
|
my $str = $code->{$function};
|
||||||
|
if (!defined $str)
|
||||||
|
{
|
||||||
|
$self->{options}->error("template function '$function' not found in '".($filename || 'inline template')."'");
|
||||||
|
return $is_outer ? $self->{options}->get_errors : '';
|
||||||
|
}
|
||||||
# a template function is just a constant if not a coderef
|
# a template function is just a constant if not a coderef
|
||||||
if (ref $str eq 'CODE')
|
elsif (ref $str eq 'CODE')
|
||||||
{
|
{
|
||||||
local $self->{tpldata} = $vars if $vars;
|
local $self->{tpldata} = $vars if $vars;
|
||||||
$str = eval { &$str($self) };
|
$str = eval { &$str($self) };
|
||||||
if ($@)
|
if ($@)
|
||||||
{
|
{
|
||||||
$self->{options}->error("error running '".($filename || 'inline template').'::'."$function': $@");
|
$self->{options}->error("error running function '$function' from '".($filename || 'inline template')."': $@");
|
||||||
return $is_outer ? $self->{options}->get_errors : '';
|
return $is_outer ? $self->{options}->get_errors : '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -296,7 +302,7 @@ sub compile
|
||||||
|
|
||||||
# call Compiler
|
# call Compiler
|
||||||
$self->{options}->{input_filename} = $fn;
|
$self->{options}->{input_filename} = $fn;
|
||||||
$self->{compiler} ||= VMXTemplate::Compiler->new($self->{options});
|
$self->{compiler} ||= VMXTemplate::Parser->new($self->{options});
|
||||||
$code = $self->{compiler}->compile($code);
|
$code = $self->{compiler}->compile($code);
|
||||||
|
|
||||||
# write compiled code to file
|
# write compiled code to file
|
||||||
|
@ -319,7 +325,7 @@ sub compile
|
||||||
$self->{compiled_code}->{$key} = eval $code;
|
$self->{compiled_code}->{$key} = eval $code;
|
||||||
if ($@)
|
if ($@)
|
||||||
{
|
{
|
||||||
$self->error("error compiling '$fn': [$@] in CODE:\n$code");
|
$self->{options}->error("error compiling '$fn': [$@] in CODE:\n$code");
|
||||||
return ();
|
return ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -233,6 +233,7 @@ sub compile_function
|
||||||
sub fmop
|
sub fmop
|
||||||
{
|
{
|
||||||
my $op = shift;
|
my $op = shift;
|
||||||
|
my $self = shift;
|
||||||
return "((" . join(") $op (", @_) . "))";
|
return "((" . join(") $op (", @_) . "))";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ sub read_token
|
||||||
{
|
{
|
||||||
my $r;
|
my $r;
|
||||||
my $code_pos = index($self->{code}, $self->{options}->{begin_code});
|
my $code_pos = index($self->{code}, $self->{options}->{begin_code});
|
||||||
my $subst_pos = index($self->{code}, $self->{options}->{begin_subst});
|
my $subst_pos = $self->{options}->{begin_subst} ne '' ? index($self->{code}, $self->{options}->{begin_subst}) : -1;
|
||||||
if ($code_pos == -1 && $subst_pos == -1)
|
if ($code_pos == -1 && $subst_pos == -1)
|
||||||
{
|
{
|
||||||
# No more directives
|
# No more directives
|
||||||
|
|
|
@ -77,7 +77,7 @@ sub get_errors
|
||||||
{
|
{
|
||||||
return '<div id="template-errors" style="display: block; border: 1px solid black; padding: 8px; background: #fcc">'.
|
return '<div id="template-errors" style="display: block; border: 1px solid black; padding: 8px; background: #fcc">'.
|
||||||
'VMXTemplate errors:<ul><li>'.
|
'VMXTemplate errors:<ul><li>'.
|
||||||
join('</li><li>', map \&html_pbr, @{$self->{errors}}).
|
join('</li><li>', map { html_pbr($_) } @{$self->{errors}}).
|
||||||
'</li></ul>';
|
'</li></ul>';
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -72,7 +72,7 @@
|
||||||
%%
|
%%
|
||||||
|
|
||||||
template: chunks {
|
template: chunks {
|
||||||
$_[0]->{functions}->{main}->{body} = "sub {\nmy \$self = shift;\nmy \$stack = [];\nmy \$t = '';\n".$_[1]."\nreturn \$t;\n}\n";
|
$_[0]->{functions}->{':main'}->{body} = "sub {\nmy \$self = shift;\nmy \$stack = [];\nmy \$t = '';\n".$_[1]."\nreturn \$t;\n}\n";
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
@ -153,7 +153,7 @@ c_fn: fn_def '=' exp {
|
||||||
;
|
;
|
||||||
c_for: for varref '=' exp '-->' chunks '<!--' 'END' {
|
c_for: for varref '=' exp '-->' chunks '<!--' 'END' {
|
||||||
my @varref = @{$_[2]};
|
my @varref = @{$_[2]};
|
||||||
my @exp = @_{$_[4]};
|
my @exp = @{$_[4]};
|
||||||
my $cs = $_[6];
|
my $cs = $_[6];
|
||||||
#{
|
#{
|
||||||
my $varref_index = substr($varref[0], 0, -1) . ".'_index'}";
|
my $varref_index = substr($varref[0], 0, -1) . ".'_index'}";
|
||||||
|
|
Loading…
Reference in New Issue