dickens 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea/*
data/.irbrc ADDED
@@ -0,0 +1,10 @@
1
+ unless defined?(reload!)
2
+ $files = []
3
+ def load!(file)
4
+ $files << file
5
+ load file
6
+ end
7
+ def reload!
8
+ $files.each { |f| load f }
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ ##v0.1.1
2
+ *Provided simple api for useing StarDict console version
3
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dickens.gemspec
4
+ gemspec
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Sergey Prikhodko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ #Dickens
2
+
3
+ ## SDCV installation
4
+
5
+ ## API methods
6
+ ### List
7
+
8
+ ### Find
9
+
10
+ ### Where
11
+
12
+ ### Configuration
13
+
14
+
@@ -0,0 +1,6 @@
1
+ require "rake"
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "dickens/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "dickens"
7
+ s.version = Dickens::VERSION
8
+ s.authors = ["Sergey Prikhodko"]
9
+ s.email = ["prikha@gmail.com"]
10
+ s.homepage = "http://github.com/prikha/dickens"
11
+ s.summary = "Dickens if for offline dictionaries lookup. It is built upon SDCV (StarDict console version)"
12
+ s.description = "For now you can gather all your dictionaries together and search through them getting pretty rails-style records back"
13
+
14
+ s.rubyforge_project = "dickens"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+
23
+ end
@@ -0,0 +1,27 @@
1
+ require "dickens/version"
2
+ require "dickens/dickens"
3
+ require "dickens/list_items"
4
+ require "dickens/find_items"
5
+
6
+ require 'rbconfig'
7
+ require RbConfig::CONFIG['target_os'] == 'mingw32' && !(RUBY_VERSION =~ /1.9/) ? 'win32/open3' : 'open3'
8
+
9
+ require 'active_support/core_ext/class/attribute_accessors'
10
+
11
+ begin
12
+ require 'active_support/core_ext/object/blank'
13
+ rescue LoadError
14
+ require 'active_support/core_ext/blank'
15
+ end
16
+
17
+ class Object
18
+ ##
19
+ # @person ? @person.name : nil
20
+ # vs
21
+ # @person.try(:name)
22
+ def try(method)
23
+ send method if respond_to? method
24
+ end
25
+ end
26
+
27
+
@@ -0,0 +1,83 @@
1
+
2
+ module Dickens
3
+ class StarDict
4
+ @@executable = "sdcv"
5
+ @@config = {
6
+ :use_dict => false,
7
+ :utf8_input => true,
8
+ :utf8_output => true,
9
+ :non_interactive => true,
10
+ :data_dir=>false
11
+ }
12
+ cattr_accessor :config, :executable
13
+
14
+
15
+ ##Использование:
16
+ # sdcv [ПАРАМЕТР...] words
17
+ #
18
+ #Параметры приложения:
19
+ #-v, --version display version information and exit
20
+ #-l, --list-dicts display list of available dictionaries and exit
21
+ #-u, --use-dict=bookname for search use only dictionary with this bookname
22
+ #-n, --non-interactive for use in scripts
23
+ #-0, --utf8-output output must be in utf8
24
+ #-1, --utf8-input input of sdcv in utf8
25
+ #-2, --data-dir=path/to/dir use this directory as path to stardict data directory
26
+
27
+ class << self
28
+ def find(word)
29
+ command = [@@executable, prepare_options, word.to_s].join(" ")
30
+ status, *response = invoke(command)
31
+ puts status
32
+ Dickens::FindItem.parse(response.join(""), word)
33
+ end
34
+
35
+ def where(word, dictionaries=[])
36
+ return find(word) if dictionaries.empty? or !dictionaries.is_a?(Array)
37
+ cache=@@config
38
+ result=[]
39
+ dictionaries.each do |d|
40
+ @@config[:use_dict] = d.try(:name) || d
41
+ result+= find(word)
42
+ end
43
+ @@config=cache
44
+ result
45
+ end
46
+
47
+ def list
48
+ Dickens::ListItem.parse invoke([@@executable, "--list-dicts"].join(" "))
49
+ end
50
+
51
+ protected
52
+
53
+ def invoke(command)
54
+ stdin, stdout, stderr = Open3.popen3(command)
55
+ return stdout.readlines
56
+ end
57
+
58
+ def prepare_options
59
+ raise WrongFormatError unless @@config.is_a? Hash
60
+ normalize_options(@@config).flatten
61
+ end
62
+
63
+ def normalize_options(options)
64
+ normalized_options = {}
65
+ options.each do |key, value|
66
+ next unless value
67
+ normalized_key = "--#{normalize_arg key}"
68
+ normalized_options[normalized_key] = normalize_value(value)
69
+ end
70
+ normalized_options
71
+ end
72
+
73
+ def normalize_arg(arg)
74
+ arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
75
+ end
76
+
77
+ def normalize_value(value)
78
+ value.is_a?(TrueClass) ? nil : "\"#{value.to_s}\""
79
+ end
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,29 @@
1
+ module Dickens
2
+ class FindItem
3
+ attr_accessor :article, :dictionary, :word, :matching
4
+
5
+ def initialize(array, word)
6
+ extract = /-->(?<dic>.*)\n-->(?<w>.*)\n\n(?<a>[\W|\w|.]*)/.match(array)
7
+ @dictionary= extract[:dic]
8
+ @word= extract[:w]
9
+ @article= extract[:a].strip
10
+ @matching= (@word == word)
11
+ end
12
+
13
+ def to_s
14
+ "-->#{dictionary}\n-->#{word}\n===\n#{article}"
15
+ end
16
+
17
+ def self.parse(string,word)
18
+ out=[]
19
+
20
+ #split console output by article delimiter but preserve the delimiter info
21
+ #then slice them by pairs [delimiter, article] and join back
22
+ #then initialize FindItems
23
+ string.split(/(-->.*\n-->.*\n)/).reject(&:empty?).each_slice(2).map(&:join).each do |e|
24
+ out<<Dickens::FindItem.new(e, word)
25
+ end
26
+ out
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Dickens
2
+ class ListItem
3
+ attr_accessor :name, :word_count
4
+ def self.parse(stdout)
5
+ stdout.map!{|item|
6
+ next if stdout.index(item)==0
7
+ self.new(item)
8
+ }.compact!
9
+ end
10
+
11
+ def initialize(string)
12
+ @name, @word_count = string.gsub(/\n/,"").split(" ")
13
+ @word_count= @word_count.to_i
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Dickens
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ #coding:utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Dickens::StarDict do
5
+ it "should FIND the result" do
6
+ Dickens::StarDict.find("петля").should_not be_nil
7
+ Dickens::StarDict.find("петля").should be_a(Array)
8
+ Dickens::StarDict.find("петля").first.should be_a(Dickens::FindItem)
9
+ end
10
+
11
+ if "should return NIL if nothing was found"
12
+ Dickens::StarDict.find("wrong_string_query").should be_nil
13
+ end
14
+
15
+
16
+ it "should LIST the dictionaries" do
17
+ Dickens::StarDict.list.should be_a(Array)
18
+ Dickens::StarDict.list.count.should be Dickens::StarDict.list.compact.count
19
+ Dickens::StarDict.list.first.should be_a(Dickens::ListItem)
20
+ end
21
+ end
22
+
23
+ describe Dickens::FindItem do
24
+ before do
25
+ @item=Dickens::StarDict.find("петля").first
26
+ end
27
+ it { @item.should respond_to(:dictionary)}
28
+ it { @item.should respond_to(:word)}
29
+ it { @item.should respond_to(:article)}
30
+ it { @item.should respond_to(:matching)}
31
+ end
@@ -0,0 +1 @@
1
+ require 'dickens'
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dickens
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergey Prikhodko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70205088649280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70205088649280
25
+ description: For now you can gather all your dictionaries together and search through
26
+ them getting pretty rails-style records back
27
+ email:
28
+ - prikha@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .irbrc
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - LICENCE
38
+ - README.md
39
+ - Rakefile
40
+ - dickens.gemspec
41
+ - lib/dickens.rb
42
+ - lib/dickens/dickens.rb
43
+ - lib/dickens/find_items.rb
44
+ - lib/dickens/list_items.rb
45
+ - lib/dickens/version.rb
46
+ - spec/dickens/dickens_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: http://github.com/prikha/dickens
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project: dickens
68
+ rubygems_version: 1.8.17
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Dickens if for offline dictionaries lookup. It is built upon SDCV (StarDict
72
+ console version)
73
+ test_files:
74
+ - spec/dickens/dickens_spec.rb
75
+ - spec/spec_helper.rb