--- trunk/lib/WebPAC/Output/TT.pm 2005/12/17 22:32:28 280
+++ Webpacus/lib/Webpacus/Controller/Output.pm 2006/01/02 18:39:55 342
@@ -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.05
+Version 0.06
=cut
-our $VERSION = '0.05';
+our $VERSION = '0.06';
=head1 SYNOPSIS
@@ -145,7 +145,7 @@
filter to return links to search, usage in TT:
- [% search('FieldToDisplay','FieldToSearch','optional delimiter') %]
+ [% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %]
=cut
@@ -157,7 +157,7 @@
return sub {
- my ($display,$search,$delimiter) = @_;
+ my ($display,$search,$delimiter,$template) = @_;
# default delimiter
$delimiter ||= '¶
',
@@ -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/) {
@@ -198,13 +201,14 @@
$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";
- push @html, <<__JS_LINK_SEARCH__
-$d
-__JS_LINK_SEARCH__
+ my $template_arg = '';
+ $template_arg = qq{,'$template'} if ($template);
+
+ push @html, qq{$d};
}
return join($delimiter, @html);
@@ -310,6 +314,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<< >>