youdao-dict 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3096a7902c572ffb7edf0a5567ab71f46c8b9890
4
+ data.tar.gz: 9f1fe0e9641bb6911f9f1b2290407678af602ba1
5
+ SHA512:
6
+ metadata.gz: ecd08f62bac6a5fe0a1426dca7f5b6c60a92b69cde3ba1a2213dce116e29b883a7960b419086c9ff5ba00f8d8783c242a5aa3eabac8e338f555903c80c899eb0
7
+ data.tar.gz: d60befc63c23c708650426a02cc684ea09eff39f8162bf5d4bca606b53a16164c2ebea2e5dd51a772c1400edf7b14503f7e8e6605a74d140ed2281e8ce058c7a
@@ -0,0 +1 @@
1
+ pkg/*
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system
4
+ - gem update bundler
5
+ rvm:
6
+ - 1.9
7
+ - 2.3
8
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in youdao-dict.gemspec
4
+ gemspec
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ desc 'Run specs'
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.verbose = false
8
+ t.rspec_opts = '--color --order random'
9
+ end
10
+
11
+ RuboCop::RakeTask.new
12
+
13
+ #task default: [:spec, :rubocop]
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ require 'youdao-dict'
4
+
5
+ ## script entrace
6
+ YoudaoDict.new.translate(ARGV.join ' ')
@@ -0,0 +1,3 @@
1
+ module YoudaoDict
2
+ VERSION = '0.1.2'.freeze
3
+ end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'net/http'
5
+ require 'rexml/document'
6
+
7
+ class YoudaoDict
8
+
9
+ def translate(word)
10
+ if word && !word.empty?
11
+ @word = word
12
+ parse Net::HTTP.get( "dict.youdao.com", "/fsearch?q=#{word}" )
13
+ end
14
+ end
15
+
16
+ def parse(src)
17
+ xml = REXML::Document.new(src)
18
+ parse_phonetic_symbol xml
19
+
20
+ __ "辞典翻译: #{@word}", "="
21
+ parse_dict_trans xml
22
+
23
+ __ "网络释义", "="
24
+ parse_web_trans xml
25
+ end
26
+
27
+ protected
28
+ # 音标
29
+ def parse_phonetic_symbol( xml )
30
+ xml.each_node('//phonetic-symbol') do |node|
31
+ if node.text
32
+ indent
33
+ print @word
34
+ puts " [#{node.text}]".cyan
35
+ end
36
+ end
37
+ end
38
+
39
+ # 辞典
40
+ def parse_dict_trans( xml )
41
+ xml.each_node('//translation/content') do |node|
42
+ indent
43
+ puts node.text.green
44
+ end
45
+ end
46
+
47
+ # 网络释义
48
+ def parse_web_trans( xml )
49
+ xml.each_node('//yodao-web-dict/web-translation') do |node|
50
+ __ node.first_node('key/').text
51
+ node.each_node('trans/value/') do |val|
52
+ indent
53
+ puts val.text
54
+ end
55
+ end
56
+ end
57
+
58
+ private
59
+ def __(t, pad='-', len=30 )
60
+ puts " #{t} ".center(len, pad)
61
+ end
62
+
63
+ def indent(idt=2)
64
+ print " " * idt
65
+ end
66
+
67
+ end
68
+
69
+ ##################################
70
+ ######## Helper classes ##########
71
+ ##################################
72
+
73
+ module REXML
74
+ # REXML::Element patch
75
+ # for better readablitiy
76
+ class Element
77
+ def each_node(path, &block); XPath.each(self, path, &block); end
78
+ def first_node(path); XPath.first(self, path); end
79
+ end
80
+ end
81
+
82
+ class String
83
+ COLORS = %w(black red green yellow blue magenta cyan white)
84
+ COLORS.each_with_index do |color, idx|
85
+ define_method color do
86
+ "\e[3#{idx}m" << self.to_s << "\e[0m"
87
+ end
88
+
89
+ define_method "#{color}_bg" do
90
+ "\e[4#{idx}m" << self.to_s << "\e[0m"
91
+ end
92
+ end
93
+ end
94
+
@@ -0,0 +1,24 @@
1
+ # 有道词典命令行版
2
+
3
+ 这是一个ruby脚本,用来在命令行中直接查询单词含义,免去打开软件或浏览器的麻烦。
4
+ 支持英译中、中译英。
5
+
6
+ ## 使用方法
7
+
8
+ ~~~
9
+ ./youdao-dict <要查询的单词>
10
+ ~~~
11
+
12
+ 例如:
13
+
14
+ ~~~shell
15
+ ./youdao-dict cake
16
+ ./youdao-dict 蛋糕
17
+ ~~~
18
+
19
+ 建议用alias简化,例如在$HOME/.bashrc或$HOME/.profile中加入
20
+
21
+ ~~~shell
22
+ alias d="/path/to/youdao-dict"
23
+ ~~~
24
+
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'youdao-dict'
7
+ s.version = YoudaoDict::VERSION
8
+ s.authors = ['hejack0207']
9
+ s.email = ['hejack0207@sina.com']
10
+ s.license = 'MIT'
11
+ s.homepage = 'https://github.com/hejack0207/Command-Line-Youdao-Dictionary'
12
+ s.summary = 'Command line executable for youdao web dictionary'
13
+ s.description = 'Command line executable for youdao web dictionary.'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_runtime_dependency('highline', '~> 1.7.2')
21
+
22
+ s.add_development_dependency('rspec', '~> 3.2')
23
+ s.add_development_dependency('rake')
24
+ if RUBY_VERSION < '2.0'
25
+ s.add_development_dependency('rubocop', '~> 0.41.1')
26
+ s.add_development_dependency('json', '< 2.0')
27
+ else
28
+ s.add_development_dependency('rubocop', '~> 0.46')
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youdao-dict
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - hejack0207
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.46'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.46'
69
+ description: Command line executable for youdao web dictionary.
70
+ email:
71
+ - hejack0207@sina.com
72
+ executables:
73
+ - youdao-dict
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Rakefile
81
+ - bin/youdao-dict
82
+ - lib/version.rb
83
+ - lib/youdao-dict.rb
84
+ - readme.md
85
+ - youdao-dict.gemspec
86
+ homepage: https://github.com/hejack0207/Command-Line-Youdao-Dictionary
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Command line executable for youdao web dictionary
110
+ test_files: []