這是本文件的舊版!
#!/usr/bin/perl
# Yen-Ming Lee <leeym@leeym.com>
# 2007/01/09
use Encode qw(encode decode from_to);
use Term::ANSIColor qw(:constants);
use Data::Dumper;
use LWP::Simple;
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{<em class="warning">})
{
if ($html =~ m{<em class="warning">.*?>(\S+)<.*?</em>})
{
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{<div class=p(\w+)>(.*?)</div>}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,<b>,$bold,g;
$line =~ s,</b>,$reset,g;
$line =~ s/<[^>]+>//g;
print $color . "$line\n" . RESET;
}
print "\n";
}
#!/bin/sh
curl -sd "p=$1" http://tw.dictionary.yahoo.com/search | \
awk "/pexplain/{ print } \
/peng/{ print } \
/pchi/{ print ; print \"\n\" }" | \
sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
$ chmod +x filename
執行:
$ ./filename [單字]