8 |
/; |
/; |
9 |
use Data::Dumper; |
use Data::Dumper; |
10 |
use WebPAC::Store 0.08; |
use WebPAC::Store 0.08; |
11 |
use WebPAC::Output::TT 0.06; |
use WebPAC::Output::TT 0.07; |
12 |
use WebPAC::Search::Estraier 0.05; |
use WebPAC::Search::Estraier 0.05; |
13 |
use File::Slurp; |
use File::Slurp; |
14 |
use Time::HiRes; |
use Time::HiRes; |
15 |
|
use Encode qw/encode decode from_to/; |
16 |
|
use Data::HTMLDumper; |
17 |
|
|
18 |
=head1 NAME |
=head1 NAME |
19 |
|
|
50 |
template: 'html_ffzg_results_short.tt' |
template: 'html_ffzg_results_short.tt' |
51 |
# encoding comming from webpac |
# encoding comming from webpac |
52 |
webpac_encoding: 'iso-8859-2' |
webpac_encoding: 'iso-8859-2' |
|
# encoding expected by Catalyst |
|
|
out_encoding: 'UTF-8' |
|
53 |
|
|
54 |
=cut |
=cut |
55 |
|
|
78 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
79 |
|
|
80 |
# save config parametars in object |
# save config parametars in object |
81 |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) { |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) { |
82 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
83 |
$c->config->{webpac}->{$f}; |
$c->config->{webpac}->{$f}; |
84 |
$log->debug("self->{$f} = " . $self->{$f}); |
$log->debug("self->{$f} = " . $self->{$f}); |
96 |
|
|
97 |
$self->{out} = new WebPAC::Output::TT( |
$self->{out} = new WebPAC::Output::TT( |
98 |
include_path => $template_path, |
include_path => $template_path, |
99 |
filters => { foo => sub { shift } }, |
filters => { |
100 |
|
dump_html => sub { |
101 |
|
my $t = shift || return; |
102 |
|
#return Data::HTMLDumper->Dumper( $t ); |
103 |
|
return Data::HTMLDumper->Dump([$t],[qw/dump/]); |
104 |
|
} |
105 |
|
}, |
106 |
); |
); |
107 |
|
|
108 |
# default template from config.yaml |
# default template from config.yaml |
109 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
110 |
|
|
|
$self->{iconv} = new Text::Iconv( |
|
|
$c->config->{webpac}->{webpac_encoding}, |
|
|
$c->config->{webpac}->{out_encoding} |
|
|
); |
|
|
|
|
111 |
$log->debug("converting encoding from webpac_encoding '" . |
$log->debug("converting encoding from webpac_encoding '" . |
112 |
$c->config->{webpac}->{webpac_encoding} . |
$c->config->{webpac}->{webpac_encoding} . |
|
"' to '" . |
|
|
$c->config->{webpac}->{out_encoding} . |
|
113 |
"'" |
"'" |
114 |
); |
); |
115 |
|
|
144 |
|
|
145 |
my $log = $self->{log}; |
my $log = $self->{log}; |
146 |
|
|
147 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("search args: " . Dumper( $args )); |
148 |
|
|
149 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
150 |
|
|
228 |
|
|
229 |
$t = time(); |
$t = time(); |
230 |
|
|
231 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
|
|
|
|
$times->{iconv} += time() - $t; |
|
232 |
|
|
233 |
push @html_results, $html; |
push @html_results, $html; |
234 |
|
|
237 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
238 |
|
|
239 |
$log->debug( sprintf( |
$log->debug( sprintf( |
240 |
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
"time spent: db = %.2f, out = %.2f", |
241 |
$times->{db}, $times->{out}, $times->{iconv}, |
$times->{db}, $times->{out}, |
242 |
) ); |
) ); |
243 |
|
|
244 |
return \@html_results; |
return \@html_results; |
261 |
|
|
262 |
my $args = {@_}; |
my $args = {@_}; |
263 |
my $log = $self->{log}; |
my $log = $self->{log}; |
264 |
$log->debug("args: " . Dumper( $args )); |
$log->debug("record args: " . Dumper( $args )); |
265 |
|
|
266 |
foreach my $f (qw/record_uri template/) { |
foreach my $f (qw/record_uri template/) { |
267 |
$log->fatal("need $f") unless ($args->{$f}); |
$log->fatal("need $f") unless ($args->{$f}); |
289 |
config => $self->{databases}->{$database}, |
config => $self->{databases}->{$database}, |
290 |
); |
); |
291 |
|
|
292 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
293 |
|
|
294 |
return $html; |
return $html; |
295 |
} |
} |
299 |
|
|
300 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
301 |
|
|
302 |
It will use C<Text::Iconv> to convert content encoding back to |
It will use C<Encode> to convert content encoding back to |
303 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
304 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
305 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
310 |
sub save_html { |
sub save_html { |
311 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
312 |
|
|
313 |
|
# FIXME Should this be UTF-8 or someting? |
314 |
|
my $js_encoding = $self->{webpac_encoding}; |
315 |
|
$js_encoding = 'UTF-16'; |
316 |
|
|
317 |
sub _conv_js { |
sub _conv_js { |
318 |
my $t = shift || return; |
return '0x' . $_[1]; |
319 |
return $self->{iconv}->convert(chr(hex($t))); |
return encode($_[0], chr(hex($_[1]))); |
320 |
} |
} |
321 |
$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex; |
#$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex; |
322 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
323 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
324 |
$content =~ s/\n\r/\n/gs; |
$content =~ s/\n\r/\n/gs; |
325 |
|
|
326 |
my ($from, $to) = ( |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
327 |
$self->{out_encoding}, |
$self->{log}->debug("convert encoding to $disk_encoding"); |
328 |
$self->{webpac_encoding}, |
from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content"); |
|
); |
|
|
|
|
|
$self->{log}->debug("using iconv to convert from $from to $to encoding"); |
|
|
|
|
|
my $iconv_on_save = new Text::Iconv($from, $to) |
|
|
|| $self->{log}->fatal("can't create iconv for saving"); |
|
|
|
|
|
$content = $iconv_on_save->convert( $content ) || die "no content?"; |
|
329 |
|
|
330 |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
331 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
347 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
348 |
|
|
349 |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
|
#$content = $q->escapeHTML($iconv_utf8->convert($content)); |
|
|
$content = $self->{iconv}->convert($content); |
|
350 |
|
|
351 |
return $content; |
return decode($self->{webpac_encoding}, $content); |
352 |
} |
} |
353 |
|
|
354 |
=head1 AUTHOR |
=head1 AUTHOR |
355 |
|
|
356 |
Dobrica Pavlinusic |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
357 |
|
|
358 |
=head1 LICENSE |
=head1 LICENSE |
359 |
|
|