Add Options class
parent
0e43a60218
commit
5e90352980
|
@ -776,7 +776,7 @@ class VMXTemplateOptions
|
|||
$this->$k = $v;
|
||||
}
|
||||
}
|
||||
if ($this->strip_space)
|
||||
if ($this->strip_space && array_search('strip_space', $this->filters) === false)
|
||||
{
|
||||
$this->filters[] = 'strip_space';
|
||||
}
|
||||
|
|
157
template.skel.pm
157
template.skel.pm
|
@ -328,7 +328,7 @@ my @Wday = qw(Sun Mon Tue Wed Thu Fri Sat);
|
|||
|
||||
our $safe_tags = 'div|blockquote|span|a|b|i|u|p|h1|h2|h3|h4|h5|h6|strike|strong|small|big|blink|center|ol|pre|sub|sup|font|br|table|tr|td|th|tbody|tfoot|thead|tt|ul|li|em|img|marquee|cite';
|
||||
|
||||
# ограниченная распознавалка дат
|
||||
# Date parser for some common formats
|
||||
sub timestamp
|
||||
{
|
||||
my ($ts, $format) = @_;
|
||||
|
@ -552,6 +552,161 @@ sub encode_json
|
|||
goto &JSON::encode_json;
|
||||
}
|
||||
|
||||
# Remove whitespace from the beginning and the end of the line
|
||||
sub trim
|
||||
{
|
||||
local $_ = $_[0];
|
||||
if ($_[1])
|
||||
{
|
||||
s/^$_[1]//s;
|
||||
s/$_[1]$//s;
|
||||
}
|
||||
else
|
||||
{
|
||||
s/^\s+//so;
|
||||
s/\s+$//so;
|
||||
}
|
||||
$_;
|
||||
}
|
||||
|
||||
# htmlspecialchars + turn \n into <br />
|
||||
sub html_pbr
|
||||
{
|
||||
my ($s) = @_;
|
||||
$s = htmlspecialchars($s);
|
||||
$s =~ s/\n/<br \/>/gso;
|
||||
return $s;
|
||||
}
|
||||
|
||||
package VMXTemplate::Exception;
|
||||
|
||||
VMXTemplate::Utils::import();
|
||||
|
||||
sub new
|
||||
{
|
||||
my $class = shift;
|
||||
$class = ref($class) || $class;
|
||||
my ($msg) = @_;
|
||||
return bless { message => $msg }, $class;
|
||||
}
|
||||
|
||||
package VMXTemplate::Options;
|
||||
|
||||
sub new
|
||||
{
|
||||
my $class = shift;
|
||||
$class = ref($class) || $class;
|
||||
my ($options) = @_;
|
||||
|
||||
my $self = bless {
|
||||
begin_code => '<!--', # instruction start
|
||||
end_code => '-->', # instruction end
|
||||
begin_subst => '{', # substitution start (may be turned off via false)
|
||||
end_subst => '}', # substitution end (may be turned off via false)
|
||||
no_code_subst => 0, # do not substitute expressions in instructions
|
||||
eat_code_line => 1, # remove the "extra" lines which contain instructions only
|
||||
root => '.', # directory with templates
|
||||
cache_dir => undef, # compiled templates cache directory
|
||||
reload => 1, # 0 means to not check for new versions of cached templates
|
||||
filters => [], # filters to run on output of every template
|
||||
use_utf8 => 1, # use UTF-8 for all string operations on template variables
|
||||
raise_error => 0, # die() on fatal template errors
|
||||
log_error => 0, # send errors to standard error output
|
||||
print_error => 0, # print fatal template errors
|
||||
strip_space => 0, # strip spaces from beginning and end of each line
|
||||
auto_escape => undef, # "safe mode" (use 's' for HTML) - automatically escapes substituted
|
||||
# values via this functions if not escaped explicitly
|
||||
compiletime_functions => {},# custom compile-time functions (code generators)
|
||||
|
||||
input_filename => '',
|
||||
errors => [],
|
||||
}, $class;
|
||||
|
||||
$self->set($options);
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub set
|
||||
{
|
||||
my ($self, $options) = @_;
|
||||
for (keys %$options)
|
||||
{
|
||||
if (exists $self->{$_} && $_ ne 'errors')
|
||||
{
|
||||
$self->{$_} = $options->{$_};
|
||||
}
|
||||
}
|
||||
$self->{filters} = [] if ref $self->{filters} ne 'ARRAY';
|
||||
if ($self->{strip_space} && !grep { $_ eq 'strip_space' } @{$self->{filters}})
|
||||
{
|
||||
push @{$self->{filters}}, 'strip_space';
|
||||
}
|
||||
if (!$self->{begin_subst} || !$self->{end_subst})
|
||||
{
|
||||
$self->{begin_subst} = undef;
|
||||
$self->{end_subst} = undef;
|
||||
$self->{no_code_subst} = 0;
|
||||
}
|
||||
$self->{cache_dir} =~ s!/*$!/!so;
|
||||
if (!-w $self->{cache_dir})
|
||||
{
|
||||
die new VMXTemplate::Exception('VMXTemplate: cache_dir='.$self->{cache_dir}.' is not writable');
|
||||
}
|
||||
$self->{root} =~ s!/*$!/!so;
|
||||
}
|
||||
|
||||
sub get_errors
|
||||
{
|
||||
my ($self) = @_;
|
||||
if ($self->{print_error} && @{$self->{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}}).
|
||||
'</li></ul>';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
# Log an error or a warning
|
||||
sub error
|
||||
{
|
||||
my ($self, $e, $fatal) = @_;
|
||||
push @{$self->{errors}}, $e;
|
||||
if ($self->{raise_error} && $fatal)
|
||||
{
|
||||
die "VMXTemplate error: $e";
|
||||
}
|
||||
if ($self->{log_error})
|
||||
{
|
||||
print STDERR "VMXTemplate error: $e";
|
||||
}
|
||||
}
|
||||
|
||||
package VMXTemplate;
|
||||
|
||||
# Version of code classes, saved into compiled files
|
||||
use constant CODE_VERSION => 4;
|
||||
|
||||
sub new
|
||||
{
|
||||
my $class = shift;
|
||||
$class = ref($class) || $class;
|
||||
my ($options) = @_;
|
||||
|
||||
my $self = bless {
|
||||
tpldata => {},
|
||||
parent => undef,
|
||||
failed => {},
|
||||
function_search_path => {},
|
||||
options => new VMXTemplate::Options($options),
|
||||
compiler => undef,
|
||||
}, $class;
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
package VMXTemplate::Compiler;
|
||||
|
||||
# function subst()
|
||||
|
|
Loading…
Reference in New Issue