Fix bugs in perl VMXTemplate

databind
vitalif 2014-11-27 22:51:14 +00:00 committed by Vitaliy Filippov
parent c5de6fbb89
commit cf6ad41c12
6 changed files with 1616 additions and 1609 deletions

View File

@ -32,8 +32,9 @@ use strict;
use Digest::MD5 qw(md5_hex);
use POSIX;
use VMXTemplate::Utils;
use VMXTemplate::Options;
use VMXTemplate::Compiler;
use VMXTemplate::Parser;
# Version of code classes, saved into compiled files
use constant CODE_VERSION => 4;
@ -97,7 +98,7 @@ sub vars
sub parse
{
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
@ -112,7 +113,7 @@ sub exec_from
sub parse_inline
{
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
@ -141,7 +142,7 @@ sub parse_real
# Load code
if ($filename)
{
$filename = $self->{root}.$filename if $filename !~ m!^/!so;
$filename = $self->{options}->{root}.$filename if $filename !~ m!^/!so;
unless ($text = $self->loadfile($filename))
{
$self->{options}->error("couldn't load template file '$filename'");
@ -178,14 +179,19 @@ sub _run
my ($code, $is_outer, $function, $filename, $vars) = @_;
$function ||= ':main';
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
if (ref $str eq 'CODE')
elsif (ref $str eq 'CODE')
{
local $self->{tpldata} = $vars if $vars;
$str = eval { &$str($self) };
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 : '';
}
}
@ -296,7 +302,7 @@ sub compile
# call Compiler
$self->{options}->{input_filename} = $fn;
$self->{compiler} ||= VMXTemplate::Compiler->new($self->{options});
$self->{compiler} ||= VMXTemplate::Parser->new($self->{options});
$code = $self->{compiler}->compile($code);
# write compiled code to file
@ -319,7 +325,7 @@ sub compile
$self->{compiled_code}->{$key} = eval $code;
if ($@)
{
$self->error("error compiling '$fn': [$@] in CODE:\n$code");
$self->{options}->error("error compiling '$fn': [$@] in CODE:\n$code");
return ();
}

View File

@ -233,6 +233,7 @@ sub compile_function
sub fmop
{
my $op = shift;
my $self = shift;
return "((" . join(") $op (", @_) . "))";
}

View File

@ -106,7 +106,7 @@ sub read_token
{
my $r;
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)
{
# No more directives

View File

@ -77,7 +77,7 @@ sub get_errors
{
return '<div id="template-errors" style="display: block; border: 1px solid black; padding: 8px; background: #fcc">'.
'VMXTemplate errors:<ul><li>'.
join('</li><li>', map \&html_pbr, @{$self->{errors}}).
join('</li><li>', map { html_pbr($_) } @{$self->{errors}}).
'</li></ul>';
}
return '';

File diff suppressed because it is too large Load Diff

View File

@ -72,7 +72,7 @@
%%
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' {
my @varref = @{$_[2]};
my @exp = @_{$_[4]};
my @exp = @{$_[4]};
my $cs = $_[6];
#{
my $varref_index = substr($varref[0], 0, -1) . ".'_index'}";