bing_dictionary 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/bin/dict +27 -4
- data/lib/bing_dictionary.rb +18 -23
- data/lib/bing_dictionary/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f98c43922d4b8eb91fc37102a51a839a58532f2
|
4
|
+
data.tar.gz: 29562da51e61ee74c77ae6202905a0f4fdba8768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0c7bfbd5134fe649a0be47bb50d29c39416b260ef3c48168e1a42d10c1b08964d9f22f9d4d733075b01d0f27f585c06738d745abc025d2e7dfe1b7fd01e53a7
|
7
|
+
data.tar.gz: 8f6a1f96a3afa854e01d581fb89287f777674c90d24c267b6e7f894eaa0718d3f3b609703c0a410ab0b315b6639cf97b09ef7f6082854cb17e57261fdb4cb7d4
|
data/README.md
CHANGED
@@ -49,12 +49,17 @@ Now long sentence is supported
|
|
49
49
|
单片机发生的讲解,不懂得可以看看!
|
50
50
|
...
|
51
51
|
|
52
|
+
## Local cache supported
|
53
|
+
All queried word default cached to ~/.bing_dictionary.db.
|
54
|
+
So it's much faster when you query the word again.
|
55
|
+
|
52
56
|
## More options
|
53
57
|
|
54
58
|
$ dict --help
|
55
59
|
Example: dict hello
|
56
60
|
-p, --[no-]pronounce Pronounce the word
|
57
|
-
-
|
61
|
+
-c, --[no-]cache Use cache from ~/.bing_dictionary.db (Default on)
|
62
|
+
-j, --jump Jump to web page
|
58
63
|
-v, --version Show the version
|
59
64
|
|
60
65
|
## Work with vim
|
data/bin/dict
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require "bing_dictionary"
|
4
3
|
require 'optparse'
|
5
4
|
|
6
5
|
options = {}
|
@@ -9,7 +8,10 @@ option_parser = OptionParser.new do |opts|
|
|
9
8
|
opts.on("-p", "--[no-]pronounce", "Pronounce the word") do |v|
|
10
9
|
options[:pronounce] = v
|
11
10
|
end
|
12
|
-
opts.on("-
|
11
|
+
opts.on("-c", "--[no-]cache", "Use cache from ~/.bing_dictionary.db (Default on)") do |v|
|
12
|
+
options[:cache] = v
|
13
|
+
end
|
14
|
+
opts.on("-j", "--jump", "Jump to web page") do |v|
|
13
15
|
options[:jump] = v
|
14
16
|
end
|
15
17
|
opts.on("-v", "--version", "Show the version") do |v|
|
@@ -21,11 +23,32 @@ if ARGV.empty?
|
|
21
23
|
option_parser.parse('--help')
|
22
24
|
else
|
23
25
|
option_parser.parse!
|
26
|
+
word = ARGV.join(' ')
|
24
27
|
if options[:version]
|
28
|
+
require "bing_dictionary"
|
25
29
|
puts BingDictionary::VERSION
|
26
30
|
elsif options[:jump]
|
27
|
-
`open http://cn.bing.com/dict/?q=#{CGI::escape(
|
31
|
+
`open http://cn.bing.com/dict/?q=#{CGI::escape(word)}`
|
28
32
|
else
|
29
|
-
|
33
|
+
require 'dbm'
|
34
|
+
DBM.open(File.join(Dir.home, '.bing_dictionary')) do |cache|
|
35
|
+
if cache.key?(word) && options[:cache] != false
|
36
|
+
puts cache[word]
|
37
|
+
else
|
38
|
+
require "bing_dictionary"
|
39
|
+
|
40
|
+
backup = $stdout
|
41
|
+
$stdout = StringIO.new
|
42
|
+
BingDictionary::Base.translate(word, options)
|
43
|
+
out = $stdout.string
|
44
|
+
$stdout = backup
|
45
|
+
|
46
|
+
if out && out.length > 0
|
47
|
+
cache[word] = out
|
48
|
+
puts out
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
30
52
|
end
|
31
53
|
end
|
54
|
+
|
data/lib/bing_dictionary.rb
CHANGED
@@ -5,7 +5,7 @@ require "bing_dictionary/version"
|
|
5
5
|
|
6
6
|
module BingDictionary
|
7
7
|
class Base
|
8
|
-
|
8
|
+
attr_accessor :doc, :options, :word
|
9
9
|
|
10
10
|
def self.translate(word, options = {})
|
11
11
|
self.new(word, options).translate
|
@@ -16,8 +16,9 @@ module BingDictionary
|
|
16
16
|
|
17
17
|
def initialize(word, options = {})
|
18
18
|
file = open("http://cn.bing.com/dict/?q=#{CGI::escape(word)}")
|
19
|
-
|
20
|
-
|
19
|
+
self.word = word
|
20
|
+
self.doc = Nokogiri::HTML(file)
|
21
|
+
self.options = options
|
21
22
|
end
|
22
23
|
|
23
24
|
def translate
|
@@ -25,15 +26,15 @@ module BingDictionary
|
|
25
26
|
machine if doc.at_css('.smt_hw')
|
26
27
|
sentence if doc.at_css('#sentenceSeg .se_li')
|
27
28
|
guess if doc.at_css('.dym_area')
|
28
|
-
pronounce if doc.at_css('#headword') &&
|
29
|
+
pronounce if doc.at_css('#headword') && options[:pronounce]
|
29
30
|
end
|
30
31
|
|
31
32
|
def head
|
32
33
|
puts doc.at_css('#headword').text
|
33
|
-
puts doc.at_css('.hd_tf_lh').text
|
34
|
+
puts with_color(doc.at_css('.hd_tf_lh').text, :green)
|
34
35
|
puts
|
35
36
|
doc.at_css('.hd_area').next_sibling.css('li').each do |li|
|
36
|
-
puts li.at_css('.pos').text
|
37
|
+
puts with_color(with_fixed(li.at_css('.pos').text, 5), :blue) + li.at_css('.def').text
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
@@ -46,13 +47,13 @@ module BingDictionary
|
|
46
47
|
def machine
|
47
48
|
puts doc.at_css('.smt_hw').text
|
48
49
|
puts doc.at_css('.p1-10').text
|
49
|
-
puts doc.at_css('.p1-11').text
|
50
|
+
puts with_color(doc.at_css('.p1-11').text, :green)
|
50
51
|
end
|
51
52
|
|
52
53
|
def sentence
|
53
54
|
puts
|
54
55
|
doc.css('#sentenceSeg .se_li').first(4).map do |li|
|
55
|
-
puts li.css('.sen_en').text
|
56
|
+
puts with_highlight(li.css('.sen_en').text, word)
|
56
57
|
puts li.css('.sen_cn').text
|
57
58
|
puts
|
58
59
|
end
|
@@ -71,24 +72,18 @@ module BingDictionary
|
|
71
72
|
end
|
72
73
|
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
class String
|
79
|
-
COLORS = %w(black red green yellow blue magenta cyan white)
|
80
|
-
COLORS.each_with_index do |color, idx|
|
81
|
-
define_method color do
|
82
|
-
$stdout.tty? ? "\e[3#{idx}m" << self.to_s << "\e[0m" : self
|
75
|
+
COLORS = %i[ black red green yellow blue magenta cyan white ]
|
76
|
+
def with_color(str, color)
|
77
|
+
"\e[3#{COLORS.index(color)}m" << str.to_s << "\e[0m"
|
83
78
|
end
|
84
79
|
|
85
|
-
|
86
|
-
|
80
|
+
def with_fixed(str, width)
|
81
|
+
width = width - str.each_char.count { |c| c =~ /\p{Han}/ }
|
82
|
+
width > 0 ? ("%-#{width}s" % str) : str
|
87
83
|
end
|
88
|
-
end
|
89
84
|
|
90
|
-
|
91
|
-
|
92
|
-
|
85
|
+
def with_highlight(str, keyword)
|
86
|
+
str.gsub(/#{keyword}/i) { |s| with_color(s, :yellow) }
|
87
|
+
end
|
93
88
|
end
|
94
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bing_dictionary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lingceng
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.6.8
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Command line dictionary grabing http://cn.bing.com/
|