--- trunk/lib/WebPAC/Output/TT.pm 2005/12/24 11:24:10 320 +++ trunk/lib/WebPAC/Output/TT.pm 2006/01/08 22:41:45 376 @@ -8,7 +8,7 @@ use Template; use List::Util qw/first/; use Data::Dumper; -use URI::Escape qw/uri_escape_utf8/; +use Encode; =head1 NAME @@ -16,11 +16,11 @@ =head1 VERSION -Version 0.06 +Version 0.07 =cut -our $VERSION = '0.06'; +our $VERSION = '0.07'; =head1 SYNOPSIS @@ -45,21 +45,21 @@ sub new { my $class = shift; - my $self = {@_}; - bless($self, $class); + my $self = {@_}; + bless($self, $class); my $log = $self->_get_logger; # create Template toolkit instance $self->{'tt'} = Template->new( INCLUDE_PATH => $self->{'include_path'}, - FILTERS => $self->{'filter'}, + #FILTERS => $self->{'filters'}, EVAL_PERL => 1, ); $log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); - $log->debug("filters defined: ",Dumper($self->{'filter'})); + $log->debug("filters defined: ",Dumper($self->{'filters'})); $self ? return $self : return undef; } @@ -169,7 +169,10 @@ my $item = $data->{'data'}->{$display} || return; return unless($item->{'display'}); - die "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)" unless($item->{'search'}); + if (! $item->{'search'}) { + warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; + return; + } my @warn; foreach my $type (qw/display search/) { @@ -193,16 +196,27 @@ my $s; if ($s_el > 0) { - $s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search"; + $s = $item->{'search'}->[$i]; + die "can't find value $i for type search in field $search" unless (defined($s)); } else { $s = $item->{'search'}->[0]; } #$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; - $s = uri_escape_utf8( $s ); + $s = __quotemeta( $s ); + + my $d = $item->{'display'}->[$i]; + die "can't find value $i for type display in field $display" unless (defined($d)); + + my $template_arg = ''; + $template_arg = qq{,'$template'} if ($template); - my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; + if ($s && ! $d) { + $d = $s; + } elsif (! $s && $d) { + $s = $d; + } - push @html, qq{$d}; + push @html, qq{$d} if ($s && $d); } return join($delimiter, @html); @@ -262,6 +276,11 @@ return "load_template($template); return false;"; }; + if ($self->{filters}) { + $args->{f} = $self->{filters}; + $log->debug("using f.filters"); + } + my $out; $self->{'tt'}->process( @@ -308,6 +327,31 @@ } +=head2 __quotemeta + +Helper to quote JavaScript-friendly characters + +=cut + +sub __quotemeta { + local $_ = shift; + $_ = decode('iso-8859-2', $_); + + s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); + { + use bytes; + s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; + } + + s/\\x09/\\t/g; + s/\\x0A/\\n/g; + s/\\x0D/\\r/g; + s/"/\\"/g; + s/\\x5C/\\\\/g; + + return $_; +} + =head1 AUTHOR Dobrica Pavlinusic, C<< >>