#!/usr/bin/perl # Yen-Ming Lee # 2007/01/09 use Encode qw(encode decode from_to); use Term::ANSIColor qw(:constants); use Data::Dumper; use LWP::Simple; use encoding "utf-8"; use strict; ydict(shift); sub ydict { my $p = shift; die if !$p; my $html = get("http://tw.dictionary.yahoo.com/search?p=$p"); from_to($html, "utf-8", "big5") if $ENV{'LC_CTYPE'} =~ /Big5/; my $i = 0; $html =~ s/\r//g; $html =~ s/\n//g; if ($html =~ m{}) { if ($html =~ m{.*?>(\S+)<.*?}) { my $q = $1; print BOLD . YELLOW . "\nERROR: $p -> $q\n" . RESET; return ydict($q); } else { print BOLD . YELLOW . "ERROR: $p\n" . RESET; return; } } print BOLD . YELLOW . "\n$p\n" . RESET; while ($html =~ m{
(.*?)
}i) { my $type = $1; my $line = $2; $html = $'; my $color; my $bold = BOLD; my $reset = RESET; $line =~ s/^\s+//; $line =~ s/\s+$//; if ($type eq 'cixin') { $i = 0; $color = BOLD . RED; } elsif ($type eq 'chi' or $type eq 'eng') { $color = CYAN; $reset = RESET . $color; $line = "\t$line"; } elsif ($type eq 'explain') { $i++; $line = "$i $line"; } else { $color = BOLD . BLUE; next; } $line =~ s,,$bold,g; $line =~ s,,$reset,g; $line =~ s/<[^>]+>//g; print $color . "$line\n" . RESET; } print "\n"; }