7 |
|
|
8 |
use Template; |
use Template; |
9 |
use List::Util qw/first/; |
use List::Util qw/first/; |
10 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
11 |
use URI::Escape qw/uri_escape_utf8/; |
use Encode; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.06 |
Version 0.07 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.06'; |
our $VERSION = '0.07'; |
24 |
|
|
25 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
26 |
|
|
45 |
|
|
46 |
sub new { |
sub new { |
47 |
my $class = shift; |
my $class = shift; |
48 |
my $self = {@_}; |
my $self = {@_}; |
49 |
bless($self, $class); |
bless($self, $class); |
50 |
|
|
51 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
52 |
|
|
53 |
# create Template toolkit instance |
# create Template toolkit instance |
54 |
$self->{'tt'} = Template->new( |
$self->{'tt'} = Template->new( |
55 |
INCLUDE_PATH => $self->{'include_path'}, |
INCLUDE_PATH => $self->{'include_path'}, |
56 |
FILTERS => $self->{'filter'}, |
#FILTERS => $self->{'filters'}, |
57 |
EVAL_PERL => 1, |
EVAL_PERL => 1, |
58 |
); |
); |
59 |
|
|
60 |
$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); |
$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); |
61 |
|
|
62 |
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
$log->debug("filters defined: ",dump($self->{'filters'})); |
63 |
|
|
64 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
65 |
} |
} |
145 |
|
|
146 |
filter to return links to search, usage in TT: |
filter to return links to search, usage in TT: |
147 |
|
|
148 |
[% search('FieldToDisplay','FieldToSearch','optional delimiter') %] |
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
149 |
|
|
150 |
=cut |
=cut |
151 |
|
|
157 |
|
|
158 |
return sub { |
return sub { |
159 |
|
|
160 |
my ($display,$search,$delimiter) = @_; |
my ($display,$search,$delimiter,$template) = @_; |
161 |
|
|
162 |
# default delimiter |
# default delimiter |
163 |
$delimiter ||= '¶<br/>', |
$delimiter ||= '¶<br/>', |
169 |
my $item = $data->{'data'}->{$display} || return; |
my $item = $data->{'data'}->{$display} || return; |
170 |
|
|
171 |
return unless($item->{'display'}); |
return unless($item->{'display'}); |
172 |
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'}) { |
173 |
|
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
174 |
|
return; |
175 |
|
} |
176 |
|
|
177 |
my @warn; |
my @warn; |
178 |
foreach my $type (qw/display search/) { |
foreach my $type (qw/display search/) { |
196 |
|
|
197 |
my $s; |
my $s; |
198 |
if ($s_el > 0) { |
if ($s_el > 0) { |
199 |
$s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search"; |
$s = $item->{'search'}->[$i]; |
200 |
|
die "can't find value $i for type search in field $search" unless (defined($s)); |
201 |
} else { |
} else { |
202 |
$s = $item->{'search'}->[0]; |
$s = $item->{'search'}->[0]; |
203 |
} |
} |
204 |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
205 |
$s = uri_escape_utf8( $s ); |
$s = __quotemeta( $s ); |
206 |
|
|
207 |
|
my $d = $item->{'display'}->[$i]; |
208 |
|
die "can't find value $i for type display in field $display" unless (defined($d)); |
209 |
|
|
210 |
|
my $template_arg = ''; |
211 |
|
$template_arg = qq{,'$template'} if ($template); |
212 |
|
|
213 |
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
if ($s && ! $d) { |
214 |
|
$d = $s; |
215 |
|
} elsif (! $s && $d) { |
216 |
|
$s = $d; |
217 |
|
} |
218 |
|
|
219 |
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s')">$d</a>}; |
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>} if ($s && $d); |
220 |
} |
} |
221 |
|
|
222 |
return join($delimiter, @html); |
return join($delimiter, @html); |
276 |
return "load_template($template); return false;"; |
return "load_template($template); return false;"; |
277 |
}; |
}; |
278 |
|
|
279 |
|
if ($self->{filters}) { |
280 |
|
$args->{f} = $self->{filters}; |
281 |
|
$log->debug("using f.filters"); |
282 |
|
} |
283 |
|
|
284 |
my $out; |
my $out; |
285 |
|
|
286 |
$self->{'tt'}->process( |
$self->{'tt'}->process( |
327 |
} |
} |
328 |
|
|
329 |
|
|
330 |
|
=head2 __quotemeta |
331 |
|
|
332 |
|
Helper to quote JavaScript-friendly characters |
333 |
|
|
334 |
|
=cut |
335 |
|
|
336 |
|
sub __quotemeta { |
337 |
|
local $_ = shift; |
338 |
|
# $_ = decode('iso-8859-2', $_); |
339 |
|
|
340 |
|
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
341 |
|
{ |
342 |
|
use bytes; |
343 |
|
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
344 |
|
} |
345 |
|
|
346 |
|
s/\\x09/\\t/g; |
347 |
|
s/\\x0A/\\n/g; |
348 |
|
s/\\x0D/\\r/g; |
349 |
|
s/"/\\"/g; |
350 |
|
s/\\x5C/\\\\/g; |
351 |
|
|
352 |
|
return $_; |
353 |
|
} |
354 |
|
|
355 |
=head1 AUTHOR |
=head1 AUTHOR |
356 |
|
|
357 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |