/[webpac2]/Webpacus/lib/Webpacus/Model/WebPAC.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /Webpacus/lib/Webpacus/Model/WebPAC.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 322 by dpavlin, Sun Dec 25 23:31:59 2005 UTC revision 348 by dpavlin, Sat Jan 7 17:34:16 2006 UTC
# Line 12  use WebPAC::Output::TT 0.06; Line 12  use WebPAC::Output::TT 0.06;
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    
# Line 48  Configuration for hyperestraier in C<con Line 49  Configuration for hyperestraier in C<con
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    
# Line 78  sub new { Line 77  sub new {
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});
# Line 102  sub new { Line 101  sub new {
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    
# Line 229  sub search { Line 221  sub search {
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    
# Line 240  sub search { Line 230  sub search {
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;
# Line 292  sub record { Line 282  sub record {
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  }  }
# Line 302  sub record { Line 292  sub record {
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
# Line 313  it over original file which should be at Line 303  it over original file which should be at
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: $!";
# Line 354  sub load_html { Line 340  sub load_html {
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    

Legend:
Removed from v.322  
changed lines
  Added in v.348

  ViewVC Help
Powered by ViewVC 1.1.26