konjac 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -59,6 +59,9 @@ Utilize a document's implied language (English) and translate into Japanese:
59
59
 
60
60
  konjac translate test_en.txt into japanese
61
61
 
62
+ Use multiple dictionaries:
63
+
64
+ konjac translate financial_report_en.txt into japanese using finance
62
65
 
63
66
  Extended Example
64
67
  ----------------
@@ -90,7 +93,7 @@ Extended Example
90
93
  en: " "
91
94
  ja: !ruby/regexp /\s{2,}/
92
95
  ja:
93
- ja: \\1\\2
96
+ ja: "\\1\\2"
94
97
  en: !ruby/regexp /([^\w])\s([^\w])/
95
98
  - # Delete extraneous spaces
96
99
  en:
data/lib/konjac/cli.rb CHANGED
@@ -12,7 +12,7 @@ module Konjac
12
12
  when "help"
13
13
  show_help
14
14
  else
15
- raise ArgumentError.new("Valid commands are translate or add")
15
+ raise InvalidCommandError.new("Valid commands are translate or add")
16
16
  end
17
17
  end
18
18
 
@@ -24,6 +24,16 @@ module Konjac
24
24
  private
25
25
 
26
26
  def translate
27
+ # Get dictionaries
28
+ using_index = ARGV.index("using")
29
+ unless using_index.nil?
30
+ ARGV.delete_at using_index
31
+ dictionaries = []
32
+ while ARGV.length > using_index && !["from", "to", "into"].include?(ARGV[using_index])
33
+ dictionaries << ARGV.delete_at(using_index)
34
+ end
35
+ end
36
+
27
37
  # Get from language
28
38
  from_index = ARGV.index("from")
29
39
  unless from_index.nil?
@@ -34,7 +44,7 @@ module Konjac
34
44
  # Get to language
35
45
  to_index = ARGV.index("to") || ARGV.index("into")
36
46
  if to_index.nil?
37
- raise Exception::InvalidLanguageError.new("You must supply a to language")
47
+ raise InvalidLanguageError.new("You must supply a to language")
38
48
  else
39
49
  ARGV.delete_at to_index
40
50
  to_lang = ARGV.delete_at(to_index)
@@ -45,21 +55,21 @@ module Konjac
45
55
  while !ARGV.empty?
46
56
  files += Dir.glob(File.expand_path(ARGV.shift))
47
57
  end
48
- raise Exception::FileNotFoundError.new("File not found") if files.empty?
58
+ raise FileNotFoundError.new("File not found") if files.empty?
49
59
  files.uniq!
50
60
 
51
61
  # Determine from language from first filename if not overridden
52
62
  if from_lang.nil?
53
63
  from_lang = Utils.extract_language_code_from_filename(files[0])
54
64
  if from_lang.nil?
55
- raise Exception::InvalidLanguageError.new("You must supply a to language")
65
+ raise InvalidLanguageError.new("You must supply a to language")
56
66
  end
57
67
  end
58
68
 
59
69
  from_lang = Language.find(from_lang).to_s
60
70
  to_lang = Language.find(to_lang).to_s
61
71
 
62
- Translator.translate files, from_lang, to_lang
72
+ Translator.translate files, from_lang, to_lang, dictionaries
63
73
  end
64
74
  end
65
75
  end
@@ -3,13 +3,14 @@ require "yaml"
3
3
  module Konjac
4
4
  module Dictionary
5
5
  class << self
6
- attr_accessor :from_lang, :to_lang, :path
6
+ attr_accessor :from_lang, :to_lang, :dictionaries, :pairs
7
7
 
8
8
  BLANK = /^\s*$/
9
9
 
10
- def load(from_lang, to_lang, opts = {})
10
+ def load(from_lang, to_lang, dictionaries, opts = {})
11
11
  # Set defaults for optional arguments
12
- opts = { :force => false, :name => "dict" }.merge(opts)
12
+ opts = { :force => false }.merge(opts)
13
+ dictionaries = ["dict"] if dictionaries.nil?
13
14
 
14
15
  # Allow both symbol and string arguments for languages
15
16
  from_lang = from_lang.to_s
@@ -20,16 +21,27 @@ module Konjac
20
21
  to_template = build_replacement_template(from_lang, to_lang)
21
22
 
22
23
  # Get full path from name
23
- full_path = File.expand_path("~/.konjac/#{opts[:name]}.yml")
24
24
 
25
- return @pairs if loaded?(from_lang, to_lang, full_path) || opts[:force]
25
+ return @pairs if loaded?(from_lang, to_lang, dictionaries) || opts[:force]
26
26
 
27
27
  # Save variables to cache so we can avoid repetitive requests
28
- cache_load from_lang, to_lang, opts
28
+ cache_load from_lang, to_lang, dictionaries
29
29
 
30
30
  # Make sure dictionary exists and load
31
- verify_dictionary_exists full_path
32
- @dictionary = ::YAML.load_file(full_path)
31
+ @dictionary = []
32
+ dictionaries.each do |dict|
33
+ if dict =~ /[\/.]/
34
+ sub_dictionaries = Dir.glob(File.expand_path(dict))
35
+ else
36
+ sub_dictionaries = Dir.glob(File.expand_path("~/.konjac/#{dict}.yml"))
37
+ end
38
+
39
+ sub_dictionaries.each do |sub_dict|
40
+ verify_dictionary_exists sub_dict
41
+ temp = ::YAML.load_file(sub_dict)
42
+ @dictionary += temp if temp.is_a?(Array)
43
+ end
44
+ end
33
45
 
34
46
  # Build a list of search and replace pairs
35
47
  @pairs = []
@@ -81,10 +93,10 @@ module Konjac
81
93
 
82
94
  # Tests whether the same from language, to language and dictionary path
83
95
  # have been loaded before
84
- def loaded?(from_lang, to_lang, full_path)
85
- (@from_lang == from_lang) &&
86
- (@to_lang == to_lang ) &&
87
- (@path == full_path)
96
+ def loaded?(from_lang, to_lang, dictionaries)
97
+ (@from_lang == from_lang) &&
98
+ (@to_lang == to_lang ) &&
99
+ (@dictionaries == dictionaries)
88
100
  end
89
101
 
90
102
  # Builds a regular expression template for the language depending on
@@ -123,10 +135,12 @@ module Konjac
123
135
  end
124
136
  end
125
137
 
126
- def cache_load(from_lang, to_lang, opts)
127
- @from_lang = from_lang
128
- @to_lang = to_lang
129
- @path = path
138
+ # Caches variables so we can determine later on whether to reload the
139
+ # dictionaries or not
140
+ def cache_load(from_lang, to_lang, dictionaries)
141
+ @from_lang = from_lang
142
+ @to_lang = to_lang
143
+ @dictionaries = dictionaries
130
144
  end
131
145
  end
132
146
  end
@@ -1,6 +1,5 @@
1
1
  module Konjac
2
- module Exception
3
- class FileNotFoundError < StandardError; end
4
- class InvalidLanguageError < StandardError; end
5
- end
2
+ class FileNotFoundError < StandardError; end
3
+ class InvalidCommandError < StandardError; end
4
+ class InvalidLanguageError < StandardError; end
6
5
  end
@@ -231,7 +231,7 @@ module Konjac
231
231
  end
232
232
 
233
233
  # Return nil if nothing found
234
- raise Exception::InvalidLanguageError.new("Language not found: #{lang}")
234
+ raise InvalidLanguageError.new("Language not found: #{lang}")
235
235
  end
236
236
  end
237
237
 
@@ -2,15 +2,14 @@
2
2
  module Konjac
3
3
  module Translator
4
4
  class << self
5
- def translate(files, from_lang, to_lang)
6
- pairs = Dictionary.load(from_lang, to_lang)
5
+ def translate(files, from_lang, to_lang, dictionaries)
6
+ pairs = Dictionary.load(from_lang, to_lang, dictionaries)
7
7
 
8
8
  files.each do |source|
9
9
  # Read in file and replace matches in content
10
10
  content = File.read(source)
11
11
  pairs.each do |pair|
12
12
  search, replace = pair
13
- puts "search = '%s', replace = '%s'" % [search, replace]
14
13
  content.gsub! search, replace
15
14
  end
16
15
 
@@ -1,3 +1,3 @@
1
1
  module Konjac
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/konjac.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require "konjac/version"
2
+ require "konjac/exception"
2
3
  autoload :FileUtils, "fileutils"
3
4
 
4
5
  module Konjac
5
6
  # Set up autoload for all modules
6
7
  autoload :CLI, "konjac/cli"
7
8
  autoload :Dictionary, "konjac/dictionary"
8
- autoload :Exception, "konjac/exception"
9
9
  autoload :Language, "konjac/language"
10
10
  autoload :Translator, "konjac/translator"
11
11
  autoload :Utils, "konjac/utils"
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + "/spec_helper"
3
+ require "tempfile"
4
+
5
+ describe CLI do
6
+ it "should fail on an invalid subcommand" do
7
+ ARGV = ["invalid"]
8
+ lambda { CLI.start }.should raise_error InvalidCommandError
9
+ end
10
+
11
+ describe "temporary files" do
12
+ before :each do
13
+ # Create dictionary
14
+ @dictionary = Tempfile.new(["dict", ".yml"])
15
+ @dictionary.write <<-eos
16
+ -
17
+ en: dogs
18
+ ja: 犬
19
+ eos
20
+ @dictionary.rewind
21
+
22
+ # Create English text to translate
23
+ @english = Tempfile.new(["english", "_en.txt"])
24
+ @english.write "I like dogs."
25
+ @english.rewind
26
+ end
27
+
28
+ it "should correctly translate English text" do
29
+ begin
30
+ ARGV = ["translate", @english.path, "into", "japanese", "using", @dictionary.path]
31
+ CLI.start
32
+ converted_path = Utils.build_converted_file_name(@english.path, "en", "ja")
33
+ puts File.read(@english.path).should == "I like dogs."
34
+ File.read(converted_path).should == "I like 犬.\n"
35
+ ensure
36
+ @dictionary.close!
37
+ @english.close!
38
+ File.delete converted_path
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,9 +1,20 @@
1
+ # coding: utf-8
1
2
  require File.dirname(__FILE__) + "/spec_helper"
3
+ require "tempfile"
2
4
 
3
5
  describe Dictionary do
4
6
  before :each do
5
-
7
+ @dictionary = Tempfile.new(["dict", ".yml"])
8
+ @dictionary.write <<-eos
9
+ -
10
+ en: dogs
11
+ ja: 犬
12
+ eos
13
+ @dictionary.rewind
6
14
  end
7
15
 
8
- it "should correctly load YAML"
16
+ it "should correctly load YAML" do
17
+ Dictionary.load "en", "ja", [@dictionary.path]
18
+ Dictionary.pairs.should == [[/\bdogs\b/, "犬"]]
19
+ end
9
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konjac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &70123693467000 !ruby/object:Gem::Requirement
16
+ requirement: &70364018145820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70123693467000
24
+ version_requirements: *70364018145820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70123693404260 !ruby/object:Gem::Requirement
27
+ requirement: &70364018145360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70123693404260
35
+ version_requirements: *70364018145360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sdoc
38
- requirement: &70123693403660 !ruby/object:Gem::Requirement
38
+ requirement: &70364018144900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70123693403660
46
+ version_requirements: *70364018144900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: autotest
49
- requirement: &70123693403240 !ruby/object:Gem::Requirement
49
+ requirement: &70364018144480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70123693403240
57
+ version_requirements: *70364018144480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: autotest-fsevent
60
- requirement: &70123693402820 !ruby/object:Gem::Requirement
60
+ requirement: &70364018144020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70123693402820
68
+ version_requirements: *70364018144020
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: autotest-growl
71
- requirement: &70123693402380 !ruby/object:Gem::Requirement
71
+ requirement: &70364018143580 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70123693402380
79
+ version_requirements: *70364018143580
80
80
  description: A Ruby command-line utility for translating files using a YAML wordlist
81
81
  email:
82
82
  - bryan.mckelvey@gmail.com
@@ -102,6 +102,7 @@ files:
102
102
  - lib/konjac/translator.rb
103
103
  - lib/konjac/utils.rb
104
104
  - lib/konjac/version.rb
105
+ - spec/cli_spec.rb
105
106
  - spec/dictionary_spec.rb
106
107
  - spec/language_spec.rb
107
108
  - spec/spec_helper.rb
@@ -131,6 +132,7 @@ signing_key:
131
132
  specification_version: 3
132
133
  summary: A Ruby command-line utility for translating files using a YAML wordlist
133
134
  test_files:
135
+ - spec/cli_spec.rb
134
136
  - spec/dictionary_spec.rb
135
137
  - spec/language_spec.rb
136
138
  - spec/spec_helper.rb