Wowboxdict



使用方法

  • 完整字串查詢
  1. 在 virtual mode 按 y yank 後
  2. 按 CTRL+k 查詢
  • 單一單字查詢
  1. 在單字前按 CTRL+w 直接查詢

在 vimrc 新增

map <C-K> :!wowboxdict.pl "<C-R>""<CR>
map <C-W> viwy:!wowboxdict.pl <C-R>"<CR>

然後新增 wowboxdict.pl

#! /usr/bin/env perl
# little parser to request ENG -> Chinese translation for World of Warcraft
 
use Term::ANSIColor qw(:constants);
use LWP::Simple;
use strict;
use warnings;
 
my $trigger = 0;
 
if ( $#ARGV == -1 ) {
    print WHITE . "<魔獸藏寶箱翻譯助手>" . RESET . " ";
    while (<>) {
        chomp;
        if ( "$_" eq '' ) {
            print WHITE . "<魔獸藏寶箱翻譯助手>" . RESET . " ";
            next;
        }
        wowboxdict($_);
        print WHITE . "<魔獸藏寶箱翻譯助手>" . RESET . " ";
    }
}
else {
    while ( $#ARGV != -1 ) {
        if ($trigger) {
            print "\n";
        }
        else {
            $trigger = 1;
        }
        wowboxdict(shift);
    }
}
 
sub wowboxdict {
    my $p = shift;
    die if !$p;
 
    my $translate;
 
    # replace space with "+"
    my $p1 = $p;
    $p1 =~ s/ /+/g;
 
    my $html = get("http://wowbox.tw/translate.php?search=$p1");
 
    print BOLD. RED . "連線異常!請檢查您的網路" . RESET . "\n"
      if !$html;
    return if !$html;
 
    $html =~ s/\r//g;
    $html =~ s/\n//g;
 
    #<p>搜尋: 繁體中文 '13213'<br></p>
    if ( $html =~ m{<p>搜尋:.*<br></p>} ) {
        print BOLD . YELLOW . "查無此字: $p\n" . RESET;
        return;
    }
    print BOLD . YELLOW . "$p 的查詢結果: " . RESET;
    print "http://wowbox.tw/translate.php?search=$p1\n";
 
    while (
        $html =~ m{>(\d+)個物品 (\S+)<br><table class='top'>(.*?)</table>})
    {
        print BOLD . WHITE . "$1 個物品在 $2\n" . RESET;
        $translate = $3;
        $html      = $';
        $translate =~ s/<tr><th>//g;
        $translate =~ s/<tr><td class=\'row\d\'>/\n/g;
        $translate =~ s/<th>/\t/g;
        $translate =~ s/<td class=\'row\d\'>/\t/g;
        print "$translate\n";
    }
}

截圖

123.JPG
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License