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 |
|
|
17 |
=head1 NAME |
=head1 NAME |
18 |
|
|
49 |
template: 'html_ffzg_results_short.tt' |
template: 'html_ffzg_results_short.tt' |
50 |
# encoding comming from webpac |
# encoding comming from webpac |
51 |
webpac_encoding: 'iso-8859-2' |
webpac_encoding: 'iso-8859-2' |
|
# encoding expected by Catalyst |
|
|
out_encoding: 'UTF-8' |
|
52 |
|
|
53 |
=cut |
=cut |
54 |
|
|
77 |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); |
78 |
|
|
79 |
# save config parametars in object |
# save config parametars in object |
80 |
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/) { |
81 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
82 |
$c->config->{webpac}->{$f}; |
$c->config->{webpac}->{$f}; |
83 |
$log->debug("self->{$f} = " . $self->{$f}); |
$log->debug("self->{$f} = " . $self->{$f}); |
101 |
# default template from config.yaml |
# default template from config.yaml |
102 |
$self->{template} ||= $c->config->{webpac}->{template}; |
$self->{template} ||= $c->config->{webpac}->{template}; |
103 |
|
|
|
$self->{iconv} = new Text::Iconv( |
|
|
$c->config->{webpac}->{webpac_encoding}, |
|
|
$c->config->{webpac}->{out_encoding} |
|
|
); |
|
|
|
|
104 |
$log->debug("converting encoding from webpac_encoding '" . |
$log->debug("converting encoding from webpac_encoding '" . |
105 |
$c->config->{webpac}->{webpac_encoding} . |
$c->config->{webpac}->{webpac_encoding} . |
|
"' to '" . |
|
|
$c->config->{webpac}->{out_encoding} . |
|
106 |
"'" |
"'" |
107 |
); |
); |
108 |
|
|
221 |
|
|
222 |
$t = time(); |
$t = time(); |
223 |
|
|
224 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
|
|
|
|
$times->{iconv} += time() - $t; |
|
225 |
|
|
226 |
push @html_results, $html; |
push @html_results, $html; |
227 |
|
|
230 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
231 |
|
|
232 |
$log->debug( sprintf( |
$log->debug( sprintf( |
233 |
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
"time spent: db = %.2f, out = %.2f", |
234 |
$times->{db}, $times->{out}, $times->{iconv}, |
$times->{db}, $times->{out}, |
235 |
) ); |
) ); |
236 |
|
|
237 |
return \@html_results; |
return \@html_results; |
282 |
config => $self->{databases}->{$database}, |
config => $self->{databases}->{$database}, |
283 |
); |
); |
284 |
|
|
285 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = decode($self->{webpac_encoding}, $html); |
286 |
|
|
287 |
return $html; |
return $html; |
288 |
} |
} |
292 |
|
|
293 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
294 |
|
|
295 |
It will use C<Text::Iconv> to convert content encoding back to |
It will use C<Encode> to convert content encoding back to |
296 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
297 |
strip extra newlines at beginning and end, and save to |
strip extra newlines at beginning and end, and save to |
298 |
C</full/path/to/file.new> and if that succeeds, just rename |
C</full/path/to/file.new> and if that succeeds, just rename |
303 |
sub save_html { |
sub save_html { |
304 |
my ($self, $path, $content) = @_; |
my ($self, $path, $content) = @_; |
305 |
|
|
306 |
|
# FIXME Should this be UTF-8 or someting? |
307 |
|
my $js_encoding = $self->{webpac_encoding}; |
308 |
|
$js_encoding = 'UTF-16'; |
309 |
|
|
310 |
sub _conv_js { |
sub _conv_js { |
311 |
my $t = shift || return; |
return '0x' . $_[1]; |
312 |
return $self->{iconv}->convert(chr(hex($t))); |
return encode($_[0], chr(hex($_[1]))); |
313 |
} |
} |
314 |
$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; |
315 |
$content =~ s/^[\n\r]+//s; |
$content =~ s/^[\n\r]+//s; |
316 |
$content =~ s/[\n\r]+$/\n/s; |
$content =~ s/[\n\r]+$/\n/s; |
317 |
$content =~ s/\n\r/\n/gs; |
$content =~ s/\n\r/\n/gs; |
318 |
|
|
319 |
my ($from, $to) = ( |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
320 |
$self->{out_encoding}, |
$self->{log}->debug("convert encoding to $disk_encoding"); |
321 |
$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?"; |
|
322 |
|
|
323 |
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 $!"; |
324 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
340 |
die "no path?" unless ($path); |
die "no path?" unless ($path); |
341 |
|
|
342 |
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); |
|
343 |
|
|
344 |
return $content; |
return decode($self->{webpac_encoding}, $content); |
345 |
} |
} |
346 |
|
|
347 |
=head1 AUTHOR |
=head1 AUTHOR |
348 |
|
|
349 |
Dobrica Pavlinusic |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
350 |
|
|
351 |
=head1 LICENSE |
=head1 LICENSE |
352 |
|
|