1 |
package Webpacus::Model::WebPAC; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use lib '/data/webpac2/lib'; |
6 |
use base qw/ |
7 |
Catalyst::Model |
8 |
/; |
9 |
use WebPAC::Store 0.08; |
10 |
use Search::Estraier 0.04; |
11 |
use File::Slurp; |
12 |
use Time::HiRes qw/time/; |
13 |
use Encode qw/encode decode from_to/; |
14 |
use Template; |
15 |
|
16 |
=head1 NAME |
17 |
|
18 |
Webpacus::Model::WebPAC - Catalyst Model |
19 |
|
20 |
=head1 SYNOPSIS |
21 |
|
22 |
See L<Webpacus> and L<WebPAC>. |
23 |
|
24 |
=head1 DESCRIPTION |
25 |
|
26 |
Catalyst Model for access to WebPAC data. |
27 |
|
28 |
=head2 new |
29 |
|
30 |
Configuration for hyperestraier in C<config.yaml> like this: |
31 |
|
32 |
--- #YAML:1.0 |
33 |
# DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!! |
34 |
|
35 |
# configuration for hyper estraier full text search engine |
36 |
hyperestraier: |
37 |
masterurl: 'http://localhost:1978/node/webpac2' |
38 |
defaultnode: 'webpac2' |
39 |
defaultdepth: 1 |
40 |
user: 'admin' |
41 |
passwd: 'admin' |
42 |
hits_on_page: 100 |
43 |
hits_for_pager: 1000 |
44 |
|
45 |
webpac: |
46 |
db_path: '/data/webpac2/db' |
47 |
template_path: '/data/webpac2/conf/output/tt' |
48 |
template: 'html_ffzg_results_short.tt' |
49 |
# encoding comming from webpac |
50 |
webpac_encoding: 'iso-8859-2' |
51 |
|
52 |
=cut |
53 |
|
54 |
sub new { |
55 |
my ( $self, $c, $config ) = @_; |
56 |
|
57 |
$self = $self->NEXT::new($c, $config); |
58 |
$self->config($config); |
59 |
|
60 |
my $log = $c->log; |
61 |
$self->{log} = $log; |
62 |
|
63 |
my $est_cfg = $c->config->{hyperestraier}; |
64 |
$est_cfg->{'log'} = $log; |
65 |
|
66 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
67 |
|
68 |
$log->dumper($est_cfg, 'est_cfg'); |
69 |
|
70 |
if (! $est_cfg->{database}) { |
71 |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
72 |
$log->info("using default node $defaultnode"); |
73 |
$est_cfg->{database} = $defaultnode; |
74 |
} |
75 |
|
76 |
my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database}; |
77 |
|
78 |
$log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}"); |
79 |
|
80 |
$self->{est_node} = Search::Estraier::Node->new( |
81 |
url => $url, |
82 |
user => $est_cfg->{user}, |
83 |
passwd => $est_cfg->{passwd}, |
84 |
); |
85 |
|
86 |
$log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); |
87 |
|
88 |
# save config parametars in object |
89 |
foreach my $f (qw/ |
90 |
db_path template_path hits_on_page webpac_encoding defaultdepth |
91 |
masterurl defaultnode |
92 |
/) { |
93 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
94 |
$c->config->{webpac}->{$f}; |
95 |
$log->debug("self->{$f} = " . $self->{$f}); |
96 |
} |
97 |
my $db_path = $self->{db_path}; |
98 |
my $template_path = $self->{template_path}; |
99 |
|
100 |
$log->debug("using db path '$db_path', template path '$template_path'"); |
101 |
|
102 |
$self->{db} = new WebPAC::Store( |
103 |
path => $db_path, |
104 |
read_only => 1, |
105 |
database => $est_cfg->{database}, |
106 |
); |
107 |
|
108 |
# default template from config.yaml |
109 |
$self->{template} ||= $c->config->{webpac}->{template}; |
110 |
|
111 |
$log->debug("converting encoding from webpac_encoding '" . |
112 |
$c->config->{webpac}->{webpac_encoding} . |
113 |
"'" |
114 |
); |
115 |
|
116 |
$self->{databases} = $c->config->{databases} || $log->fatal("can't find databases in config"); |
117 |
|
118 |
# create Template toolkit instance |
119 |
$self->{'tt'} = Template->new( |
120 |
INCLUDE_PATH => $template_path, |
121 |
FILTERS => { |
122 |
dump_html => sub { |
123 |
return unless (@_); |
124 |
my $out; |
125 |
my $i = 1; |
126 |
foreach my $v (@_) { |
127 |
$out .= qq{<div id="dump_$i">} . |
128 |
Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) . |
129 |
qq{</div>}; |
130 |
$i++; |
131 |
} |
132 |
$out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out); |
133 |
return $out; |
134 |
} |
135 |
}, |
136 |
EVAL_PERL => 1, |
137 |
); |
138 |
|
139 |
return $self; |
140 |
|
141 |
} |
142 |
|
143 |
=head2 setup_site |
144 |
|
145 |
$self->setup_site('site_name'); |
146 |
|
147 |
Change node URL and database name according to site name (if available) or fallback |
148 |
to C<defaultnode> from configuration. |
149 |
|
150 |
=cut |
151 |
|
152 |
sub setup_site { |
153 |
my $self = shift; |
154 |
|
155 |
my $site = shift || $self->{defaultnode}; |
156 |
|
157 |
$self->{log}->fatal("setup_site can't find site or defaultnode") unless ($site); |
158 |
|
159 |
my $url = $self->{masterurl} . '/node/' . $site; |
160 |
$self->{est_node}->set_url( $url ); |
161 |
$self->{log}->debug("setup_site $site using $url"); |
162 |
} |
163 |
|
164 |
=head2 search |
165 |
|
166 |
my $m->search( |
167 |
phrase => 'query phrase', |
168 |
add_attr => \@add_attr |
169 |
get_attr => [ '@uri' ], |
170 |
max => 42, |
171 |
template => 'result_template.tt', |
172 |
depth => 1, |
173 |
); |
174 |
|
175 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
176 |
C<template> which will (if specified) return results in HTML using |
177 |
selected template. |
178 |
|
179 |
=cut |
180 |
|
181 |
sub search { |
182 |
my $self = shift; |
183 |
|
184 |
my $search_start_t = time(); |
185 |
|
186 |
my $args = {@_}; |
187 |
|
188 |
my $log = $self->{log}; |
189 |
|
190 |
$log->dumper($args, 'args'); |
191 |
|
192 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
193 |
|
194 |
my $template_filename = $args->{template} || $self->{template}; |
195 |
|
196 |
$args->{max} ||= $self->{'hits_for_pager'}; |
197 |
if (! $args->{max}) { |
198 |
$args->{max} = 100; |
199 |
$log->warn("max not set when calling model. Using default of $args->{max}"); |
200 |
} |
201 |
|
202 |
my $times; # store some times for benchmarking |
203 |
|
204 |
my $t = time(); |
205 |
|
206 |
# transfer depth of search |
207 |
if (! $args->{depth}) { |
208 |
my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration"); |
209 |
$args->{depth} = $default; |
210 |
$log->warn("using default search depth $default"); |
211 |
} |
212 |
$args->{depth} ||= 0; |
213 |
|
214 |
$log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]'); |
215 |
|
216 |
# |
217 |
# construct condition for Hyper Estraier |
218 |
# |
219 |
my $cond = Search::Estraier::Condition->new(); |
220 |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
221 |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
222 |
map { |
223 |
$cond->add_attr( $_ ); |
224 |
$log->debug(" + $_"); |
225 |
} @{ $args->{add_attr} }; |
226 |
}; |
227 |
|
228 |
$cond->set_phrase( $query ) if ($query); |
229 |
$cond->set_options( $args->{options} ) if ($args->{options}); |
230 |
$cond->set_order( $args->{order} ) if ($args->{order}); |
231 |
|
232 |
my $max = $args->{max} || 7; |
233 |
my $page = $args->{page} || 1; |
234 |
if ($page < 1) { |
235 |
$log->warn("page number $page < 1"); |
236 |
$page = 1; |
237 |
} |
238 |
|
239 |
$cond->set_max( $page * $max ); |
240 |
|
241 |
my $result = $self->{est_node}->search($cond, $args->{depth}); |
242 |
my $hits = $result->doc_num; |
243 |
|
244 |
$times->{est} += time() - $t; |
245 |
|
246 |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
247 |
|
248 |
$log->dumper($result->{hints}, 'result->hints' ); |
249 |
|
250 |
# |
251 |
# fetch results |
252 |
# |
253 |
|
254 |
my @results; |
255 |
|
256 |
for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { |
257 |
|
258 |
$t = time(); |
259 |
|
260 |
#$log->debug("get_doc($i)"); |
261 |
my $doc = $result->get_doc( $i ); |
262 |
if (! $doc) { |
263 |
$log->warn("can't find result $i"); |
264 |
next; |
265 |
} |
266 |
|
267 |
my $hash; |
268 |
|
269 |
foreach my $attr (@{ $args->{get_attr} }) { |
270 |
my $val = $doc->attr( $attr ); |
271 |
#$log->debug("attr $attr = ", $val || 'undef'); |
272 |
$hash->{$attr} = $val if (defined($val)); |
273 |
} |
274 |
|
275 |
$times->{hash} += time() - $t; |
276 |
|
277 |
next unless ($hash); |
278 |
|
279 |
if (! $args->{'template'}) { |
280 |
push @results, $hash; |
281 |
} else { |
282 |
my ($database, $prefix, $id); |
283 |
|
284 |
if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
285 |
($database, $prefix,$id) = ($1,$2,$3); |
286 |
} else { |
287 |
$log->warn("can't decode database/prefix/id from " . $hash->{'@uri'}); |
288 |
next; |
289 |
} |
290 |
|
291 |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
292 |
|
293 |
$t = time(); |
294 |
|
295 |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
296 |
if (! $ds) { |
297 |
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
298 |
next; |
299 |
} |
300 |
|
301 |
$times->{db} += time() - $t; |
302 |
|
303 |
$t = time(); |
304 |
|
305 |
my $html = $self->apply( |
306 |
template => $template_filename, |
307 |
data => $ds, |
308 |
record_uri => "${database}/${prefix}/${id}", |
309 |
config => $self->{databases}->{$database}, |
310 |
); |
311 |
|
312 |
$times->{apply} += time() - $t; |
313 |
|
314 |
$t = time(); |
315 |
|
316 |
$html = decode($self->{webpac_encoding}, $html); |
317 |
|
318 |
$times->{decode} += time() - $t; |
319 |
|
320 |
push @results, $html; |
321 |
} |
322 |
|
323 |
} |
324 |
|
325 |
$log->debug( sprintf( |
326 |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
327 |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
328 |
) ); |
329 |
|
330 |
return \@results; |
331 |
} |
332 |
|
333 |
=head2 record |
334 |
|
335 |
my $html = $m->record( |
336 |
mfn => 42, |
337 |
template => 'foo.tt', |
338 |
); |
339 |
|
340 |
This will load one record, convert it to html using C<template> and return |
341 |
it. |
342 |
|
343 |
=cut |
344 |
|
345 |
sub record { |
346 |
my $self = shift; |
347 |
|
348 |
my $args = {@_}; |
349 |
my $log = $self->{log}; |
350 |
$log->dumper( $args, 'args' ); |
351 |
|
352 |
foreach my $f (qw/record_uri template/) { |
353 |
$log->fatal("need $f") unless ($args->{$f}); |
354 |
} |
355 |
|
356 |
my ($database, $prefix, $id); |
357 |
|
358 |
if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) { |
359 |
($database, $prefix, $id) = ($1,$2,$3); |
360 |
} else { |
361 |
$log->error("can't parse $args->{record_uri} into prefix, database and uri"); |
362 |
return; |
363 |
} |
364 |
|
365 |
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database ); |
366 |
if (! $ds) { |
367 |
$log->error("can't load_ds( $database/$prefix/$id )"); |
368 |
return; |
369 |
} |
370 |
|
371 |
my $html = $self->apply( |
372 |
template => $args->{template}, |
373 |
data => $ds, |
374 |
record_uri => $args->{record_uri}, |
375 |
config => $self->{databases}->{$database}, |
376 |
); |
377 |
|
378 |
$html = decode($self->{webpac_encoding}, $html); |
379 |
|
380 |
return $html; |
381 |
} |
382 |
|
383 |
|
384 |
=head2 save_html |
385 |
|
386 |
$m->save_html( '/full/path/to/file', $content ); |
387 |
|
388 |
It will use C<Encode> to convert content encoding back to |
389 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
390 |
strip extra newlines at beginning and end, and save to |
391 |
C</full/path/to/file.new> and if that succeeds, just rename |
392 |
it over original file which should be atomic on filesystem level. |
393 |
|
394 |
=cut |
395 |
|
396 |
sub save_html { |
397 |
my ($self, $path, $content) = @_; |
398 |
|
399 |
# FIXME Should this be UTF-8 or someting? |
400 |
my $js_encoding = $self->{webpac_encoding}; |
401 |
$js_encoding = 'UTF-16'; |
402 |
|
403 |
sub _conv_js { |
404 |
return '0x' . $_[1]; |
405 |
return encode($_[0], chr(hex($_[1]))); |
406 |
} |
407 |
#$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex; |
408 |
$content =~ s/^[\n\r]+//s; |
409 |
$content =~ s/[\n\r]+$/\n/s; |
410 |
$content =~ s/\n\r/\n/gs; |
411 |
|
412 |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
413 |
$self->{log}->debug("convert encoding to $disk_encoding"); |
414 |
from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content"); |
415 |
|
416 |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
417 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
418 |
} |
419 |
|
420 |
=head2 load_html |
421 |
|
422 |
my $html = $m->load_html('/full/path/to/file'); |
423 |
|
424 |
This will convert file from Webpac encoding to Catalyst and |
425 |
convert that data to escaped HTML (for sending into |
426 |
C<< <textarea/> >> tags in html. |
427 |
|
428 |
=cut |
429 |
|
430 |
sub load_html { |
431 |
my ($self, $path) = @_; |
432 |
|
433 |
die "no path?" unless ($path); |
434 |
|
435 |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
436 |
|
437 |
return decode($self->{webpac_encoding}, $content); |
438 |
} |
439 |
|
440 |
|
441 |
=head2 apply |
442 |
|
443 |
Create output from in-memory data structure using Template Toolkit template. |
444 |
|
445 |
my $text = $tt->apply( |
446 |
template => 'text.tt', |
447 |
data => $ds, |
448 |
record_uri => 'database/prefix/mfn', |
449 |
); |
450 |
|
451 |
It also has follwing template toolikit filter routies defined: |
452 |
|
453 |
=cut |
454 |
|
455 |
sub apply { |
456 |
my $self = shift; |
457 |
|
458 |
my $args = {@_}; |
459 |
|
460 |
my $log = $self->{log} || die "no log?"; |
461 |
|
462 |
foreach my $a (qw/template data/) { |
463 |
$log->fatal("need $a") unless ($args->{$a}); |
464 |
} |
465 |
|
466 |
=head3 tt_filter_type |
467 |
|
468 |
filter to return values of specified from $ds, usage from TT template is in form |
469 |
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
470 |
|
471 |
[% d('Title') %] |
472 |
[% d('Author',', ' %] |
473 |
|
474 |
=cut |
475 |
|
476 |
sub tt_filter_type { |
477 |
my ($data,$type) = @_; |
478 |
|
479 |
die "no data?" unless ($data); |
480 |
$type ||= 'display'; |
481 |
|
482 |
my $default_delimiter = { |
483 |
'display' => '¶<br/>', |
484 |
'index' => '\n', |
485 |
}; |
486 |
|
487 |
return sub { |
488 |
|
489 |
my ($name,$join) = @_; |
490 |
|
491 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
492 |
# Hm? Should we die here? |
493 |
return unless ($name); |
494 |
|
495 |
my $item = $data->{'data'}->{$name} || return; |
496 |
|
497 |
my $v = $item->{$type} || return; |
498 |
|
499 |
if (ref($v) eq 'ARRAY') { |
500 |
if ($#{$v} == 0) { |
501 |
$v = $v->[0]; |
502 |
} else { |
503 |
$join = $default_delimiter->{$type} unless defined($join); |
504 |
$v = join($join, @{$v}); |
505 |
} |
506 |
} else { |
507 |
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
508 |
} |
509 |
|
510 |
return $v; |
511 |
} |
512 |
} |
513 |
|
514 |
$args->{'d'} = tt_filter_type($args, 'display'); |
515 |
$args->{'display'} = tt_filter_type($args, 'display'); |
516 |
|
517 |
=head3 tt_filter_search |
518 |
|
519 |
filter to return links to search, usage in TT: |
520 |
|
521 |
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
522 |
|
523 |
=cut |
524 |
|
525 |
sub tt_filter_search { |
526 |
|
527 |
my ($data) = @_; |
528 |
|
529 |
die "no data?" unless ($data); |
530 |
|
531 |
return sub { |
532 |
|
533 |
my ($display,$search,$delimiter,$template) = @_; |
534 |
|
535 |
# default delimiter |
536 |
$delimiter ||= '¶<br/>', |
537 |
|
538 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
539 |
# Hm? Should we die here? |
540 |
return unless ($display); |
541 |
|
542 |
my $item = $data->{'data'}->{$display} || return; |
543 |
|
544 |
return unless($item->{'display'}); |
545 |
if (! $item->{'search'}) { |
546 |
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
547 |
return; |
548 |
} |
549 |
|
550 |
my @warn; |
551 |
foreach my $type (qw/display search/) { |
552 |
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
553 |
} |
554 |
|
555 |
if (@warn) { |
556 |
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
557 |
return; |
558 |
} |
559 |
my @html; |
560 |
|
561 |
my $d_el = $#{ $item->{'display'} }; |
562 |
my $s_el = $#{ $item->{'search'} }; |
563 |
|
564 |
# easy, both fields have same number of elements or there is just |
565 |
# one search and multiple display |
566 |
if ( $d_el == $s_el || $s_el == 0 ) { |
567 |
|
568 |
foreach my $i ( 0 .. $d_el ) { |
569 |
|
570 |
my $s; |
571 |
if ($s_el > 0) { |
572 |
$s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search"; |
573 |
} else { |
574 |
$s = $item->{'search'}->[0]; |
575 |
} |
576 |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
577 |
$s = __quotemeta( $s ); |
578 |
|
579 |
my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display"; |
580 |
|
581 |
my $template_arg = ''; |
582 |
$template_arg = qq{,'$template'} if ($template); |
583 |
|
584 |
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
585 |
} |
586 |
|
587 |
return join($delimiter, @html); |
588 |
} else { |
589 |
my $html = qq{<div class="notice">WARNING: we should really support if there is $d_el display elements and $s_el search elements, but currently there is no nice way to do so, so we will just display values</div>}; |
590 |
my $v = $item->{'display'}; |
591 |
|
592 |
if ($#{$v} == 0) { |
593 |
$html .= $v->[0]; |
594 |
} else { |
595 |
$html .= join($delimiter, @{$v}); |
596 |
} |
597 |
return $html; |
598 |
} |
599 |
} |
600 |
} |
601 |
|
602 |
$args->{'search'} = tt_filter_search($args); |
603 |
|
604 |
=head3 load_rec |
605 |
|
606 |
Used mostly for onClick events like this: |
607 |
|
608 |
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
609 |
|
610 |
It will automatically do sanity checking and create correct JavaScript code. |
611 |
|
612 |
=cut |
613 |
|
614 |
$args->{'load_rec'} = sub { |
615 |
my @errors; |
616 |
|
617 |
my $record_uri = shift or push @errors, "record_uri missing"; |
618 |
my $template = shift or push @errors, "template missing"; |
619 |
|
620 |
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
621 |
push @errors, "invalid format of record_uri: $record_uri"; |
622 |
} |
623 |
|
624 |
if (@errors) { |
625 |
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
626 |
} else { |
627 |
return "load_rec('$record_uri','$template'); return false;"; |
628 |
} |
629 |
}; |
630 |
|
631 |
=head3 load_template |
632 |
|
633 |
Used to re-submit search request and load results in different template |
634 |
|
635 |
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
636 |
|
637 |
=cut |
638 |
|
639 |
$args->{'load_template'} = sub { |
640 |
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
641 |
return "load_template($template); return false;"; |
642 |
}; |
643 |
|
644 |
my $out; |
645 |
|
646 |
$self->{'tt'}->process( |
647 |
$args->{'template'}, |
648 |
$args, |
649 |
\$out |
650 |
) || $log->error( "apply can't process template: ", $self->{'tt'}->error() ); |
651 |
|
652 |
return $out; |
653 |
} |
654 |
|
655 |
|
656 |
=head2 __quotemeta |
657 |
|
658 |
Helper to quote JavaScript-friendly characters |
659 |
|
660 |
=cut |
661 |
|
662 |
sub __quotemeta { |
663 |
local $_ = shift; |
664 |
$_ = decode('iso-8859-2', $_); |
665 |
|
666 |
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
667 |
{ |
668 |
use bytes; |
669 |
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
670 |
} |
671 |
|
672 |
s/\\x09/\\t/g; |
673 |
s/\\x0A/\\n/g; |
674 |
s/\\x0D/\\r/g; |
675 |
s/"/\\"/g; |
676 |
s/\\x5C/\\\\/g; |
677 |
|
678 |
return $_; |
679 |
} |
680 |
|
681 |
|
682 |
|
683 |
=head1 AUTHOR |
684 |
|
685 |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
686 |
|
687 |
=head1 LICENSE |
688 |
|
689 |
This library is free software, you can redistribute it and/or modify |
690 |
it under the same terms as Perl itself. |
691 |
|
692 |
=cut |
693 |
|
694 |
1; |