konjac 0.1.4 → 0.1.5

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.
data/README.md CHANGED
@@ -53,20 +53,20 @@ Usage
53
53
 
54
54
  Translate all text files in the current directory from Japanese into English:
55
55
 
56
- konjac translate *.txt from japanese to english
56
+ konjac translate *.txt --from japanese --to english
57
57
 
58
58
  Utilize a document's implied language (English) and translate into Japanese:
59
59
 
60
- konjac translate test_en.txt into japanese
60
+ konjac translate test_en.txt --to japanese
61
61
 
62
62
  Use multiple dictionaries:
63
63
 
64
- konjac translate financial_report_en.txt into japanese using finance
64
+ konjac translate financial_report_en.txt --to japanese --using finance
65
65
 
66
66
  Extract text from a DOCX document (creates a plain-text `test.konjac` file from
67
67
  `test.docx`):
68
68
 
69
- konjac extract test
69
+ konjac export test
70
70
 
71
71
  Import tags file back into DOCX document (file created is named
72
72
  `test_imported.docx`):
data/konjac.gemspec CHANGED
@@ -19,10 +19,12 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_runtime_dependency "nokogiri"
22
- s.add_development_dependency "bundler"
23
- s.add_development_dependency "rspec"
24
- s.add_development_dependency "sdoc"
22
+ s.add_runtime_dependency "term-ansicolor"
23
+ s.add_runtime_dependency "trollop"
25
24
  s.add_development_dependency "autotest"
26
25
  s.add_development_dependency "autotest-fsevent"
27
26
  s.add_development_dependency "autotest-growl"
27
+ s.add_development_dependency "bundler"
28
+ s.add_development_dependency "rspec"
29
+ s.add_development_dependency "sdoc"
28
30
  end
data/lib/konjac/cli.rb CHANGED
@@ -1,94 +1,114 @@
1
+ require "term/ansicolor"
2
+ require "trollop"
3
+
1
4
  module Konjac
2
5
  module CLI
3
- autoload :SubCommand, "konjac/cli/sub_command"
4
- autoload :SubCommandManager, "konjac/cli/sub_command_manager"
6
+ class Color
7
+ extend Term::ANSIColor
8
+ end
5
9
 
6
10
  class << self
7
- def init_sub_commands
8
- @valid_commands = SubCommandManager.new
9
- @valid_commands.add :extract, [:export, :e, :x], "Extract text from a DOCX file" do
10
- Word.extract_docx_tags(ARGV)
11
- end
12
- @valid_commands.add :help, [:h, :"-h", :"?"], "Show help" do
13
- show_help
14
- end
15
- @valid_commands.add :import, [:i, :m], "Import text back into a DOCX file" do
16
- Word.import_docx_tags(ARGV)
17
- end
18
- @valid_commands.add :translate, [:t], "Translate a file" do
19
- translate
20
- end
21
- end
11
+ SUB_COMMANDS = {
12
+ "edit" => "Edit the tags file for a .docx file",
13
+ "export" => "Export text tags from a .docx file",
14
+ "import" => "Import text tags into a .docx file",
15
+ "translate" => "Translate a file according to ~/.konjac/dict.yml"
16
+ }
17
+ BANNER = <<-eos
18
+ #{Color.bold "%s"}
22
19
 
23
- def start
24
- init_sub_commands
25
- show_help if ARGV.empty?
20
+ #{Color.bold "Usage:"}
21
+ konjac %s [#{Color.underscore "options"}] <filenames>+%s
22
+ where [#{Color.underscore "options"}] are:
23
+ eos
26
24
 
27
- # Subcommand
28
- sub_command = ARGV.shift
29
- result = @valid_commands.execute(sub_command)
30
-
31
- if result.nil?
32
- show_help
33
- raise InvalidCommandError.new("Invalid subcommand: #{sub_command}")
25
+ def start
26
+ global_opts = Trollop::options do
27
+ version "konjac #{Konjac::VERSION} (c) 2012 Bryan McKelvey"
28
+ banner BANNER % [
29
+ "konjac is a Ruby command-line utility for translating files " +
30
+ "using a YAML wordlist",
31
+ "[#{Color.underscore "subcommand"}]",
32
+ "\n\nwhere [#{Color.underscore "subcommand"}] is any one of:\n%s\n" %
33
+ Konjac::CLI.describe_subcommands
34
+ ]
35
+ opt :dry_run, "Don't actually do anything", :short => "n"
36
+ opt :quiet, "Suppress error messages"
37
+ stop_on SUB_COMMANDS.keys
34
38
  end
35
39
 
36
- result
40
+ # Get subcommand
41
+ cmd = ARGV.shift
42
+ sc_banner = BANNER % [SUB_COMMANDS[cmd], cmd, ""]
43
+ cmd_opts = case cmd
44
+ when "edit"
45
+ Trollop::options do
46
+ banner sc_banner
47
+ opt :editor, "A command indicating which editor to use (e.g. vi %s)"
48
+ end
49
+ Word.edit_docx_tags ARGV, opts
50
+ when "extract", "export"
51
+ Trollop::options do
52
+ banner sc_banner
53
+ end
54
+ Word.extract_docx_tags ARGV
55
+ when "import"
56
+ Trollop::options do
57
+ banner sc_banner
58
+ end
59
+ Word.import_docx_tags ARGV
60
+ when "translate"
61
+ opts = Trollop::options do
62
+ banner sc_banner
63
+ opt :from, "The language from which to translate", :type => :string
64
+ opt :to, "The language into which to translate", :type => :string
65
+ opt :using, "The names of dictionaries to use", :type => :string,
66
+ :default => "dict", :multi => true
67
+ opt :use_cache, "Use cached dictionary", :default => false
68
+ opt :word, "Translate a word or phrase", :default => false
69
+ end
70
+ result = translate(ARGV, opts)
71
+ puts result if opts[:word]
72
+ else
73
+ Trollop::die "unknown subcommand #{cmd.inspect}"
74
+ end
37
75
  end
38
76
 
39
- def show_help
40
- puts @valid_commands.to_s
77
+ # Describe the subcommands available to the command line interface
78
+ def describe_subcommands
79
+ text = []
80
+ leftcol_width = SUB_COMMANDS.keys.map(&:length).max
81
+ SUB_COMMANDS.each do |sc, desc|
82
+ text << " %#{leftcol_width}s: %s" % [sc, desc]
83
+ end
84
+ text.join "\n"
41
85
  end
42
86
 
43
87
  private
88
+
89
+ def translate(files, opts = {})
90
+ to_lang = Language.find(opts[:to]).to_s
44
91
 
45
- def translate
46
- # Get dictionaries
47
- using_index = ARGV.index("using")
48
- unless using_index.nil?
49
- ARGV.delete_at using_index
50
- dictionaries = []
51
- while ARGV.length > using_index && !["from", "to", "into"].include?(ARGV[using_index])
52
- dictionaries << ARGV.delete_at(using_index)
53
- end
54
- end
55
-
56
- # Get from language
57
- from_index = ARGV.index("from")
58
- unless from_index.nil?
59
- ARGV.delete_at from_index
60
- from_lang = ARGV.delete_at(from_index)
61
- end
62
-
63
- # Get to language
64
- to_index = ARGV.index("to") || ARGV.index("into")
65
- if to_index.nil?
66
- raise InvalidLanguageError.new("You must supply a to language")
92
+ if opts[:word]
93
+ from_lang = Language.find(opts[:from]).to_s
94
+ Translator.translate_word ARGV[0].dup, from_lang, to_lang, opts
67
95
  else
68
- ARGV.delete_at to_index
69
- to_lang = ARGV.delete_at(to_index)
70
- end
71
-
72
- # Get a list of files to translate
73
- files = []
74
- while !ARGV.empty?
75
- files += Dir.glob(File.expand_path(ARGV.shift))
76
- end
77
- raise FileNotFoundError.new("File not found") if files.empty?
78
- files.uniq!
96
+ # Get a list of files to translate
97
+ parsed_files = Utils.parse_files(files)
98
+ raise FileNotFoundError.new("File not found") if parsed_files.empty?
79
99
 
80
- # Determine from language from first filename if not overridden
81
- if from_lang.nil?
82
- from_lang = Utils.extract_language_code_from_filename(files[0])
83
- if from_lang.nil?
84
- raise InvalidLanguageError.new("You must supply a to language")
100
+ # Determine from language from first filename if not supplied
101
+ if opts[:from].nil?
102
+ opts[:from] = Utils.extract_language_code_from_filename(parsed_files[0])
103
+ if opts[:from].nil?
104
+ raise InvalidLanguageError.new("You must supply a from language")
105
+ end
85
106
  end
86
- end
87
107
 
88
- from_lang = Language.find(from_lang).to_s
89
- to_lang = Language.find(to_lang).to_s
108
+ from_lang = Language.find(opts[:from]).to_s
90
109
 
91
- Translator.translate files, from_lang, to_lang, dictionaries
110
+ Translator.translate_files parsed_files, from_lang, to_lang, opts
111
+ end
92
112
  end
93
113
  end
94
114
  end
@@ -7,28 +7,29 @@ module Konjac
7
7
 
8
8
  BLANK = /^\s*$/
9
9
 
10
- def load(from_lang, to_lang, dictionaries, opts = {})
11
- # Set defaults for optional arguments
12
- opts = { :force => false }.merge(opts)
13
- dictionaries = ["dict"] if dictionaries.nil?
14
-
10
+ def load(from_lang, to_lang, opts = {})
15
11
  # Allow both symbol and string arguments for languages
16
12
  from_lang = from_lang.to_s
17
13
  to_lang = to_lang.to_s
18
14
 
19
15
  # Ignore everything if we've been here before
20
- return @pairs if loaded?(from_lang, to_lang, dictionaries) || opts[:force]
16
+ return @pairs if loaded?(from_lang, to_lang, opts[:using])
17
+
18
+ if opts[:use_cache]
19
+ @pairs = load_serialized(from_lang, to_lang, opts[:using])
20
+ return @pairs unless @pairs.nil?
21
+ end
21
22
 
22
23
  # Build a regex template for the from language
23
24
  from_template = build_regex_template(from_lang)
24
25
  to_template = build_replacement_template(from_lang, to_lang)
25
26
 
26
27
  # Save variables to cache so we can avoid repetitive requests
27
- cache_load from_lang, to_lang, dictionaries
28
+ cache_load from_lang, to_lang, opts[:using]
28
29
 
29
30
  # Make sure dictionary exists and load
30
31
  @dictionary = []
31
- dictionaries.each do |dict|
32
+ opts[:using].each do |dict|
32
33
  if dict =~ /[\/.]/
33
34
  sub_dictionaries = Dir.glob(File.expand_path(dict))
34
35
  else
@@ -49,6 +50,7 @@ module Konjac
49
50
  @pairs << pair unless pair.nil?
50
51
  end
51
52
 
53
+ save_serialized from_lang, to_lang, dictionaries, @pairs
52
54
  @pairs
53
55
  end
54
56
 
@@ -104,9 +106,10 @@ module Konjac
104
106
  # Tests whether the same from language, to language and dictionary path
105
107
  # have been loaded before
106
108
  def loaded?(from_lang, to_lang, dictionaries)
107
- (@from_lang == from_lang) &&
108
- (@to_lang == to_lang ) &&
109
- (@dictionaries == dictionaries)
109
+ (@from_lang == from_lang ) &&
110
+ (@to_lang == to_lang ) &&
111
+ (@dictionaries == dictionaries) &&
112
+ !@pairs.nil?
110
113
  end
111
114
 
112
115
  # Builds a regular expression template for the language depending on
@@ -152,6 +155,30 @@ module Konjac
152
155
  @to_lang = to_lang
153
156
  @dictionaries = dictionaries
154
157
  end
158
+
159
+ def load_serialized(from_lang, to_lang, dictionaries)
160
+ file_name = File.expand_path("~/.konjac/marshal/%s_%s_%s" %
161
+ [from_lang, to_lang, dictionaries.join("_")])
162
+ if File.exists?(file_name)
163
+ Marshal.load file_name
164
+ else
165
+ nil
166
+ end
167
+ end
168
+
169
+ def save_serialized(from_lang, to_lang, dictionaries, pairs)
170
+ file_name = File.expand_path("~/.konjac/marshal/%s_%s_%s" %
171
+ [from_lang, to_lang, dictionaries.join("_")])
172
+
173
+ # Create directory structure if necessary
174
+ unless File.exists?(file_name)
175
+ FileUtils.mkdir_p File.dirname(file_name)
176
+ end
177
+
178
+ File.open(file_name, "w") do |file|
179
+ Marshal.dump(pairs, file)
180
+ end
181
+ end
155
182
  end
156
183
  end
157
184
  end
@@ -1,5 +1,4 @@
1
1
  module Konjac
2
2
  class FileNotFoundError < StandardError; end
3
- class InvalidCommandError < StandardError; end
4
3
  class InvalidLanguageError < StandardError; end
5
4
  end
@@ -2,16 +2,13 @@
2
2
  module Konjac
3
3
  module Translator
4
4
  class << self
5
- def translate(files, from_lang, to_lang, dictionaries)
6
- pairs = Dictionary.load(from_lang, to_lang, dictionaries)
5
+ def translate_files(files, from_lang, to_lang, opts = {})
6
+ load_dictionary from_lang, to_lang, opts
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
- pairs.each do |pair|
12
- search, replace = pair
13
- content.gsub! search, replace
14
- end
11
+ content = translate_content(content)
15
12
 
16
13
  # Write changed content to file
17
14
  File.open(Utils.build_converted_file_name(source, from_lang, to_lang), "w") do |file|
@@ -19,6 +16,27 @@ module Konjac
19
16
  end
20
17
  end
21
18
  end
19
+
20
+ def translate_word(word, from_lang, to_lang, opts = {})
21
+ load_dictionary from_lang, to_lang, opts
22
+
23
+ translate_content word
24
+ end
25
+
26
+ def translate_content(content)
27
+ @pairs.each do |pair|
28
+ search, replace = pair
29
+ content.gsub! search, replace
30
+ end
31
+
32
+ content
33
+ end
34
+
35
+ private
36
+
37
+ def load_dictionary(from_lang, to_lang, opts)
38
+ @pairs = Dictionary.load(from_lang, to_lang, opts)
39
+ end
22
40
  end
23
41
  end
24
42
  end
data/lib/konjac/utils.rb CHANGED
@@ -24,6 +24,18 @@ module Konjac
24
24
 
25
25
  "#{dirname}/#{basename}_#{to_lang}#{extname}"
26
26
  end
27
+
28
+ # Parses a list of files
29
+ def parse_files(files, ext = nil)
30
+ files = [files] unless files.is_a?(Array)
31
+ parsed_files = []
32
+ while !files.empty?
33
+ file = files.shift
34
+ file = file.sub(/\.[^\\\/]*$/, "") + ext unless ext.nil?
35
+ parsed_files += Dir.glob(File.expand_path(file))
36
+ end
37
+ parsed_files.uniq
38
+ end
27
39
  end
28
40
  end
29
41
  end
@@ -1,3 +1,3 @@
1
1
  module Konjac
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/konjac/word.rb CHANGED
@@ -5,103 +5,107 @@ module Konjac
5
5
  class << self
6
6
  # Extracts the text content from a Microsoft Word 2003+ Document
7
7
  def import_docx_tags(files)
8
- files.each do |file|
9
- sub_files = Dir.glob(File.expand_path(file))
10
- sub_files.each do |sub_file|
11
- # Build the list of paths we need to work with
12
- dirname = File.dirname(sub_file)
13
- basename = File.basename(sub_file, ".*")
14
- orig_docx = "#{dirname}/#{basename}.docx"
15
- new_path = "#{dirname}/#{basename}_imported.docx"
16
- xml_path = "#{dirname}/#{basename}.xml"
17
- tags_path = "#{dirname}/#{basename}.konjac"
18
- out_path = "#{dirname}/word/document.xml"
19
-
20
- # Open the original XML file and the updated tags
21
- writer = Nokogiri::XML(File.read(xml_path))
22
- nodes = writer.xpath("//w:t")
23
- tags = TagManager.new(tags_path)
24
-
25
- # Overwrite each <w:t> tag's content with the new tag
26
- tags.all.each do |tag|
27
- if tag.translated?
28
- nodes[tag.index].content = tag.translated
29
- end
8
+ sub_files = Utils.force_extension(files, ".docx")
9
+ sub_files.each do |sub_file|
10
+ # Build the list of paths we need to work with
11
+ dirname = File.dirname(sub_file)
12
+ basename = File.basename(sub_file, ".*")
13
+ orig_docx = "#{dirname}/#{basename}.docx"
14
+ new_path = "#{dirname}/#{basename}_imported.docx"
15
+ xml_path = "#{dirname}/#{basename}.xml"
16
+ tags_path = "#{dirname}/#{basename}.konjac"
17
+ out_path = "#{dirname}/word/document.xml"
18
+
19
+ # Open the original XML file and the updated tags
20
+ writer = Nokogiri::XML(File.read(xml_path))
21
+ nodes = writer.xpath("//w:t")
22
+ tags = TagManager.new(tags_path)
23
+
24
+ # Overwrite each <w:t> tag's content with the new tag
25
+ tags.all.each do |tag|
26
+ if tag.translated?
27
+ nodes[tag.index].content = tag.translated
30
28
  end
29
+ end
31
30
 
32
- # Create a directory for word/document.xml if necessary
33
- unless File.directory?("#{dirname}/word")
34
- FileUtils.mkdir "#{dirname}/word"
35
- end
31
+ # Create a directory for word/document.xml if necessary
32
+ unless File.directory?("#{dirname}/word")
33
+ FileUtils.mkdir "#{dirname}/word"
34
+ end
36
35
 
37
- # Write the modified XML to a file
38
- File.open(out_path, "w") do |file|
39
- file.write writer.to_xml.gsub(/\n\s*/, "").sub(/\?></, "?>\n<")
40
- end
36
+ # Write the modified XML to a file
37
+ File.open(out_path, "w") do |file|
38
+ file.write writer.to_xml.gsub(/\n\s*/, "").sub(/\?></, "?>\n<")
39
+ end
41
40
 
42
- # Copy the original file
43
- FileUtils.cp orig_docx, new_path
41
+ # Copy the original file
42
+ FileUtils.cp orig_docx, new_path
44
43
 
45
- # Add the new document XML to the copied file
46
- system "cd #{dirname} && zip -q #{new_path} word/document.xml"
47
- end
44
+ # Add the new document XML to the copied file
45
+ system "cd #{dirname} && zip -q #{new_path} word/document.xml"
48
46
  end
49
47
  end
50
48
 
51
49
  # Extracts the text content from a Microsoft Word 2003+ Document
52
50
  def extract_docx_tags(files)
53
- files.each do |file|
54
- sub_files = Dir.glob(File.expand_path(file))
55
- sub_files.each do |sub_file|
56
- # Build a list of all the paths we're working with
57
- dirname = File.dirname(sub_file)
58
- basename = File.basename(sub_file, ".*")
59
- orig_docx = "#{dirname}/#{basename}.docx"
60
- xml_path = "#{dirname}/#{basename}_orig.xml"
61
- clean_path = "#{dirname}/#{basename}.xml"
62
- tags_path = "#{dirname}/#{basename}.konjac"
63
-
64
- # Unzip the DOCX's word/document.xml file and pipe the output into
65
- # an XML with the same base name as the DOCX
66
- system "unzip -p #{orig_docx} word/document.xml > #{xml_path}"
67
-
68
- # Read in the XML file and extract the content from each <w:t> tag
69
- cleaner = Nokogiri::XML(File.read(xml_path))
70
- File.open(tags_path, "w") do |tags_file|
71
- # Remove all grammar and spellcheck tags
72
- cleaner.xpath("//w:proofErr").remove
73
-
74
- nodes = cleaner.xpath("//w:r")
75
- prev = nil
76
- nodes.each do |node|
77
- unless prev.nil?
78
- if (prev.next_sibling == node) && compare_nodes(prev, node)
79
- begin
80
- node.at_xpath("w:t").content = prev.at_xpath("w:t").content +
81
- node.at_xpath("w:t").content
82
- prev.remove
83
- rescue
84
- end
51
+ sub_files = Utils.force_extension(files, ".docx")
52
+ sub_files.each do |sub_file|
53
+ # Build a list of all the paths we're working with
54
+ dirname = File.dirname(sub_file)
55
+ basename = File.basename(sub_file, ".*")
56
+ orig_docx = "#{dirname}/#{basename}.docx"
57
+ xml_path = "#{dirname}/#{basename}_orig.xml"
58
+ clean_path = "#{dirname}/#{basename}.xml"
59
+ tags_path = "#{dirname}/#{basename}.konjac"
60
+
61
+ # Unzip the DOCX's word/document.xml file and pipe the output into
62
+ # an XML with the same base name as the DOCX
63
+ system "unzip -p #{orig_docx} word/document.xml > #{xml_path}"
64
+
65
+ # Read in the XML file and extract the content from each <w:t> tag
66
+ cleaner = Nokogiri::XML(File.read(xml_path))
67
+ File.open(tags_path, "w") do |tags_file|
68
+ # Remove all grammar and spellcheck tags
69
+ cleaner.xpath("//w:proofErr").remove
70
+
71
+ nodes = cleaner.xpath("//w:r")
72
+ prev = nil
73
+ nodes.each do |node|
74
+ unless prev.nil?
75
+ if (prev.next_sibling == node) && compare_nodes(prev, node)
76
+ begin
77
+ node.at_xpath("w:t").content = prev.at_xpath("w:t").content +
78
+ node.at_xpath("w:t").content
79
+ prev.remove
80
+ rescue
85
81
  end
86
82
  end
87
-
88
- prev = node
89
- end
90
-
91
- # Write the tags file
92
- index = 0
93
- cleaner.xpath("//w:t").each do |node|
94
- tags_file.puts "[[KJ-%i]]%s" % [index, additional_info(node)]
95
- tags_file.puts "> %s" % node.content
96
- index += 1
97
83
  end
84
+
85
+ prev = node
98
86
  end
99
87
 
100
- # Write the cleaned-up XML to a file for inspection
101
- File.open(clean_path, "w") do |xml|
102
- xml.puts cleaner.to_xml
88
+ # Write the tags file
89
+ index = 0
90
+ cleaner.xpath("//w:t").each do |node|
91
+ tags_file.puts "[[KJ-%i]]%s" % [index, additional_info(node)]
92
+ tags_file.puts "> %s" % node.content
93
+ index += 1
103
94
  end
104
95
  end
96
+
97
+ # Write the cleaned-up XML to a file for inspection
98
+ File.open(clean_path, "w") do |xml|
99
+ xml.puts cleaner.to_xml
100
+ end
101
+ end
102
+ end
103
+
104
+ # Opens the .konjac tag files for the specified DOCX files
105
+ def edit_docx_tags(files)
106
+ sub_files = Utils.force_extension(files, ".konjac")
107
+ sub_files.each do |sub_file|
108
+ system "$EDITOR #{sub_file}"
105
109
  end
106
110
  end
107
111
 
data/spec/cli_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe CLI do
12
12
 
13
13
  it "should fail on an invalid subcommand" do
14
14
  set_argv "invalid"
15
- lambda { CLI.start }.should raise_error InvalidCommandError
15
+ lambda { CLI.start }.should raise_error SystemExit
16
16
  end
17
17
 
18
18
  describe "temporary files" do
@@ -32,25 +32,15 @@ describe CLI do
32
32
  @english.rewind
33
33
 
34
34
  # Set ARGV
35
- set_argv "translate", @english.path, "into", "japanese", "using",
36
- @dictionary.path
35
+ set_argv "translate", "-t", "japanese", "-u", @dictionary.path,
36
+ @english.path
37
37
  end
38
38
 
39
39
  it "should correctly translate English text" do
40
- begin
41
- CLI.start
42
- converted_path = Utils.build_converted_file_name(@english.path, "en", "ja")
43
- File.read(@english.path).should == "I like dogs."
44
- File.read(converted_path).should == "I like 犬.\n"
45
- ensure
46
- @dictionary.close!
47
- @english.close!
48
- File.delete converted_path
49
- end
50
- end
51
-
52
- it "should return the name of the sub_command" do
53
- CLI.start.should == :translate
40
+ CLI.start
41
+ converted_path = Utils.build_converted_file_name(@english.path, "en", "ja")
42
+ File.read(@english.path).should == "I like dogs."
43
+ File.read(converted_path).should == "I like 犬.\n"
54
44
  end
55
45
  end
56
46
  end
@@ -37,7 +37,7 @@ describe Dictionary do
37
37
 
38
38
  describe "when converting from English to Japanese" do
39
39
  before :each do
40
- Dictionary.load "en", "ja", [@dictionary.path]
40
+ Dictionary.load "en", "ja", { :using => [@dictionary.path] }
41
41
  end
42
42
 
43
43
  it "should correctly load a simple term" do
@@ -68,7 +68,7 @@ describe Dictionary do
68
68
 
69
69
  describe "when converting from Japanese to English" do
70
70
  before :each do
71
- Dictionary.load "ja", "en", [@dictionary.path]
71
+ Dictionary.load "ja", "en", { :using => [@dictionary.path] }
72
72
  end
73
73
 
74
74
  it "should add whitespace to term replacement" do
@@ -78,7 +78,7 @@ describe Dictionary do
78
78
 
79
79
  describe "when converting from English to Spanish" do
80
80
  before :each do
81
- Dictionary.load "en", "es", [@dictionary.path]
81
+ Dictionary.load "en", "es", { :using => [@dictionary.path] }
82
82
  end
83
83
 
84
84
  it "should not add whitespace to term replacement" do
@@ -6,6 +6,12 @@ describe Language do
6
6
  Language.find(:eng).should == :en
7
7
  end
8
8
 
9
+ it "should find French" do
10
+ Language.find(:french).should == :fr
11
+ Language.find(:fre).should == :fr
12
+ Language.find(:fra).should == :fr
13
+ end
14
+
9
15
  it "should find Japanese" do
10
16
  Language.find(:japanese).should == :ja
11
17
  Language.find(:jpn).should == :ja
data/spec/utils_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require File.dirname(__FILE__) + "/spec_helper"
2
+ require "tempfile"
2
3
 
3
4
  describe Utils do
4
5
  it "should extract the correct language from a filename" do
@@ -10,4 +11,52 @@ describe Utils do
10
11
  Utils.build_converted_file_name("./test.txt", "en", "ja").should == "./test_ja.txt"
11
12
  Utils.build_converted_file_name("./test_fr.txt", "en", "ja").should == "./test_fr_ja.txt"
12
13
  end
14
+
15
+ describe "when forcing an extension" do
16
+ before :each do
17
+ @konjac = Tempfile.new(["test", ".konjac"])
18
+ @kon2 = Tempfile.new(["test2", ".konjac"])
19
+ @docx = Tempfile.new(["test", ".docx"])
20
+ @doc2 = Tempfile.new(["test2", ".docx"])
21
+ end
22
+
23
+ it "should work when the user does not specify an extension" do
24
+ files_found = Utils.parse_files("#{Dir.tmpdir}/*", ".konjac")
25
+ files_found.should include @konjac.path
26
+ files_found.should include @kon2.path
27
+ files_found.should_not include @docx.path
28
+ files_found.should_not include @doc2.path
29
+ files_found = Utils.parse_files("#{Dir.tmpdir}/*", ".docx")
30
+ files_found.should include @docx.path
31
+ files_found.should include @doc2.path
32
+ files_found.should_not include @konjac.path
33
+ files_found.should_not include @kon2.path
34
+ end
35
+
36
+ it "should work when the user does specify an extension" do
37
+ files_found = Utils.parse_files("#{Dir.tmpdir}/*.docx", ".konjac")
38
+ files_found.should include @konjac.path
39
+ files_found.should include @kon2.path
40
+ files_found.should_not include @docx.path
41
+ files_found.should_not include @doc2.path
42
+ files_found = Utils.parse_files("#{Dir.tmpdir}/*.docx", ".docx")
43
+ files_found.should include @docx.path
44
+ files_found.should include @doc2.path
45
+ files_found.should_not include @konjac.path
46
+ files_found.should_not include @kon2.path
47
+ end
48
+
49
+ it "should work when the user supplies wildcards" do
50
+ files_found = Utils.parse_files("#{Dir.tmpdir}/*.*", ".konjac")
51
+ files_found.should include @konjac.path
52
+ files_found.should include @kon2.path
53
+ files_found.should_not include @docx.path
54
+ files_found.should_not include @doc2.path
55
+ files_found = Utils.parse_files("#{Dir.tmpdir}/*.*", ".docx")
56
+ files_found.should include @docx.path
57
+ files_found.should include @doc2.path
58
+ files_found.should_not include @konjac.path
59
+ files_found.should_not include @kon2.path
60
+ end
61
+ end
13
62
  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.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-13 00:00:00.000000000 Z
12
+ date: 2012-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70265140795800 !ruby/object:Gem::Requirement
16
+ requirement: &70250662109380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,32 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70265140795800
24
+ version_requirements: *70250662109380
25
25
  - !ruby/object:Gem::Dependency
26
- name: bundler
27
- requirement: &70265140794440 !ruby/object:Gem::Requirement
26
+ name: term-ansicolor
27
+ requirement: &70250662108960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70250662108960
36
+ - !ruby/object:Gem::Dependency
37
+ name: trollop
38
+ requirement: &70250662108540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70250662108540
47
+ - !ruby/object:Gem::Dependency
48
+ name: autotest
49
+ requirement: &70250662124620 !ruby/object:Gem::Requirement
28
50
  none: false
29
51
  requirements:
30
52
  - - ! '>='
@@ -32,10 +54,10 @@ dependencies:
32
54
  version: '0'
33
55
  type: :development
34
56
  prerelease: false
35
- version_requirements: *70265140794440
57
+ version_requirements: *70250662124620
36
58
  - !ruby/object:Gem::Dependency
37
- name: rspec
38
- requirement: &70265140793660 !ruby/object:Gem::Requirement
59
+ name: autotest-fsevent
60
+ requirement: &70250662124060 !ruby/object:Gem::Requirement
39
61
  none: false
40
62
  requirements:
41
63
  - - ! '>='
@@ -43,10 +65,10 @@ dependencies:
43
65
  version: '0'
44
66
  type: :development
45
67
  prerelease: false
46
- version_requirements: *70265140793660
68
+ version_requirements: *70250662124060
47
69
  - !ruby/object:Gem::Dependency
48
- name: sdoc
49
- requirement: &70265140792780 !ruby/object:Gem::Requirement
70
+ name: autotest-growl
71
+ requirement: &70250662123300 !ruby/object:Gem::Requirement
50
72
  none: false
51
73
  requirements:
52
74
  - - ! '>='
@@ -54,10 +76,10 @@ dependencies:
54
76
  version: '0'
55
77
  type: :development
56
78
  prerelease: false
57
- version_requirements: *70265140792780
79
+ version_requirements: *70250662123300
58
80
  - !ruby/object:Gem::Dependency
59
- name: autotest
60
- requirement: &70265140791700 !ruby/object:Gem::Requirement
81
+ name: bundler
82
+ requirement: &70250662122840 !ruby/object:Gem::Requirement
61
83
  none: false
62
84
  requirements:
63
85
  - - ! '>='
@@ -65,10 +87,10 @@ dependencies:
65
87
  version: '0'
66
88
  type: :development
67
89
  prerelease: false
68
- version_requirements: *70265140791700
90
+ version_requirements: *70250662122840
69
91
  - !ruby/object:Gem::Dependency
70
- name: autotest-fsevent
71
- requirement: &70265140790860 !ruby/object:Gem::Requirement
92
+ name: rspec
93
+ requirement: &70250662122420 !ruby/object:Gem::Requirement
72
94
  none: false
73
95
  requirements:
74
96
  - - ! '>='
@@ -76,10 +98,10 @@ dependencies:
76
98
  version: '0'
77
99
  type: :development
78
100
  prerelease: false
79
- version_requirements: *70265140790860
101
+ version_requirements: *70250662122420
80
102
  - !ruby/object:Gem::Dependency
81
- name: autotest-growl
82
- requirement: &70265140789760 !ruby/object:Gem::Requirement
103
+ name: sdoc
104
+ requirement: &70250662121920 !ruby/object:Gem::Requirement
83
105
  none: false
84
106
  requirements:
85
107
  - - ! '>='
@@ -87,7 +109,7 @@ dependencies:
87
109
  version: '0'
88
110
  type: :development
89
111
  prerelease: false
90
- version_requirements: *70265140789760
112
+ version_requirements: *70250662121920
91
113
  description: A Ruby command-line utility for translating files using a YAML wordlist
92
114
  email:
93
115
  - bryan.mckelvey@gmail.com
@@ -107,8 +129,6 @@ files:
107
129
  - konjac.gemspec
108
130
  - lib/konjac.rb
109
131
  - lib/konjac/cli.rb
110
- - lib/konjac/cli/sub_command.rb
111
- - lib/konjac/cli/sub_command_manager.rb
112
132
  - lib/konjac/dictionary.rb
113
133
  - lib/konjac/exception.rb
114
134
  - lib/konjac/language.rb
@@ -1,20 +0,0 @@
1
- module Konjac
2
- module CLI
3
- class SubCommand
4
- attr :name, :aliases, :description
5
-
6
- def initialize(name, aliases, description, &block)
7
- @name, @aliases, @description = name, aliases, description
8
- @block = block
9
- end
10
-
11
- def alias_text
12
- @aliases.join ", "
13
- end
14
-
15
- def execute
16
- @block.call
17
- end
18
- end
19
- end
20
- end
@@ -1,77 +0,0 @@
1
- module Konjac
2
- module CLI
3
- class SubCommandManager
4
- def initialize
5
- @sub_commands ||= []
6
- end
7
-
8
- def add(name, aliases, description, &block)
9
- @sub_commands << SubCommand.new(name, aliases, description, &block)
10
- end
11
-
12
- def execute(sub_command)
13
- sub_command = sub_command.to_sym
14
-
15
- @sub_commands.each do |sc|
16
- if sc.name == sub_command || sc.aliases.include?(sub_command)
17
- sc.execute
18
- return sc.name
19
- end
20
- end
21
-
22
- return nil
23
- end
24
-
25
- def all
26
- @sub_commands
27
- end
28
-
29
- def get_lengths
30
- @lengths = { :name => 8, :aliases => 7, :description => 11 }
31
-
32
- @sub_commands.each do |sc|
33
- if sc.name.length > @lengths[:name]
34
- @lengths[:name] = sc.name.length
35
- end
36
- if sc.alias_text.length > @lengths[:aliases]
37
- @lengths[:aliases] = sc.alias_text.length
38
- end
39
- if sc.description.length > @lengths[:description]
40
- @lengths[:description] = sc.description.length
41
- end
42
- end
43
- end
44
-
45
- def to_s
46
- get_lengths
47
- text = "\n"
48
-
49
- header_lines = "%s %s %s\n" % [
50
- "-" * @lengths[:name],
51
- "-" * @lengths[:aliases],
52
- "-" * @lengths[:description]
53
- ]
54
-
55
- # Create headers
56
- text << header_lines
57
- text << "%s %s %s\n" % [
58
- "Commands".ljust(@lengths[:name]),
59
- "Aliases".ljust(@lengths[:aliases]),
60
- "Description".ljust(@lengths[:description])
61
- ]
62
- text << header_lines
63
-
64
- # Write descriptions of each subcommand
65
- @sub_commands.each do |sc|
66
- text << "%s %s %s\n" % [
67
- sc.name.to_s.ljust(@lengths[:name]),
68
- sc.alias_text.to_s.ljust(@lengths[:aliases]),
69
- sc.description.to_s.ljust(@lengths[:description])
70
- ]
71
- end
72
-
73
- text
74
- end
75
- end
76
- end
77
- end