Changeset 1530

Show
Ignore:
Timestamp:
08/20/06 21:27:36
Author:
ko
Message:

use Term::Encoding to detect query and results encoding automatically.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/bin/plagger-search

    r1391 r1530  
    33use warnings; 
    44 
    5 use Encode
     5use Encode ()
    66use FindBin; 
    77use Getopt::Long; 
     
    1515 
    1616my $context = Plagger->new(config => $path); 
    17 my $query = Encode::decode_utf8($ARGV[0]); 
     17my $query = decode($ARGV[0]); 
    1818 
    1919my @feeds = $context->search($query); 
    2020 
    2121my $feed = $feeds[0]; 
    22 binmode STDOUT, ":utf8"; 
    23 print "Search for '$query': ", $feed->count, " entries found.\n\n"; 
     22binmode STDOUT, ":utf8" unless $context->conf->{log}->{encoding}; 
     23exit unless $feed; 
     24print "Search for '" . encode($query) . "': ", $feed->count , " entries found.\n\n"; 
    2425 
    2526for my $entry ($feed->entries) { 
    26     print $entry->title, $entry->author ? "(by " . $entry->author . ")" : '', "\n"; 
    27     print $entry->body; # summary 
     27    print encode($entry->title), $entry->author ? "(by " . encode($entry->author) . ")" : '', "\n"; 
     28    print encode($entry->body); # summary 
    2829    print $entry->permalink, "\n"; 
    2930} 
    3031 
     32sub decode { 
     33    my $str = shift; 
     34    if ($context->conf->{log}->{encoding}) { 
     35        $str = Encode::decode($context->conf->{log}->{encoding}, $str); 
     36    } else { 
     37        $str = Encode::decode_utf8($str); 
     38    } 
     39    return $str; 
     40} 
     41 
     42sub encode { 
     43    my $str = shift; 
     44    if ($context->conf->{log}->{encoding}) { 
     45        $str = Encode::decode_utf8($str) unless utf8::is_utf8($str); 
     46        $str = Encode::encode($context->conf->{log}->{encoding}, $str); 
     47    } 
     48    return $str; 
     49}