konjac 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/bin/konjac +13 -0
- data/konjac.gemspec +24 -0
- data/lib/konjac/cli.rb +60 -0
- data/lib/konjac/dictionary.rb +58 -0
- data/lib/konjac/exceptions.rb +5 -0
- data/lib/konjac/manager.rb +0 -0
- data/lib/konjac/translator.rb +33 -0
- data/lib/konjac/version.rb +3 -0
- data/lib/konjac.rb +10 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012 Bryan McKelvey
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
konjac
|
2
|
+
======
|
3
|
+
|
4
|
+
A Ruby command-line utility for translating files using a YAML wordlist
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
Create a file in `~/.konjac/` called `dict.yml`. The file should have the
|
10
|
+
following format:
|
11
|
+
|
12
|
+
languages:
|
13
|
+
en:
|
14
|
+
- english
|
15
|
+
- eng
|
16
|
+
ja:
|
17
|
+
- japanese
|
18
|
+
- jpn
|
19
|
+
|
20
|
+
...
|
21
|
+
|
22
|
+
terms:
|
23
|
+
-
|
24
|
+
en: japanese
|
25
|
+
ja: nihongo
|
26
|
+
-
|
27
|
+
en: book
|
28
|
+
ja: hon
|
29
|
+
|
30
|
+
Name
|
31
|
+
----
|
32
|
+
|
33
|
+
*Hon'yaku* means "translation" in Japanese. This utility relies on a YAML
|
34
|
+
wordlist. *Konnyaku* (Japanese for "konjac") rhymes with *hon'yaku* and is a
|
35
|
+
type of yam. Also, Doraemon had something called a *hon'yaku konnyaku* that
|
36
|
+
allowed one to listen to animals. But I digress.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/konjac
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby -Ku
|
2
|
+
require "konjac"
|
3
|
+
begin
|
4
|
+
Konjac::CLI.start
|
5
|
+
rescue Interrupt => e
|
6
|
+
# Konjac.ui.error "\nQuitting..."
|
7
|
+
# Konjac.ui.debug e.backtrace.join("\n")
|
8
|
+
exit 1
|
9
|
+
rescue SystemExit => e
|
10
|
+
exit e.status
|
11
|
+
rescue Exception => e
|
12
|
+
raise e
|
13
|
+
end
|
data/konjac.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "konjac/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "konjac"
|
7
|
+
s.version = Konjac::VERSION
|
8
|
+
s.authors = ["Bryan McKelvey"]
|
9
|
+
s.email = ["bryan.mckelvey@gmail.com"]
|
10
|
+
s.homepage = "http://brymck.herokuapp.com/"
|
11
|
+
s.summary = %q{A Ruby command-line utility for translating files using a YAML wordlist}
|
12
|
+
s.description = %q{A Ruby command-line utility for translating files using a YAML wordlist}
|
13
|
+
|
14
|
+
s.rubyforge_project = "konjac"
|
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
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "trollop"
|
24
|
+
end
|
data/lib/konjac/cli.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Konjac
|
2
|
+
module CLI
|
3
|
+
class << self
|
4
|
+
def start
|
5
|
+
show_help if ARGV.empty?
|
6
|
+
|
7
|
+
# Subcommand
|
8
|
+
case ARGV.shift
|
9
|
+
when "translate"
|
10
|
+
translate
|
11
|
+
when "add"
|
12
|
+
when "help"
|
13
|
+
show_help
|
14
|
+
else
|
15
|
+
raise ArgumentError.new("Valid commands are translate or add")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def show_help
|
20
|
+
puts "Help"
|
21
|
+
exit 0
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def translate
|
27
|
+
from_index = ARGV.index("from")
|
28
|
+
to_index = ARGV.index("to")
|
29
|
+
|
30
|
+
# Get the to and from languages and delete them from ARGV
|
31
|
+
unless from_index.nil? || to_index.nil?
|
32
|
+
if from_index < to_index
|
33
|
+
ARGV.delete_at to_index
|
34
|
+
to_lang = ARGV.delete_at(to_index)
|
35
|
+
ARGV.delete_at from_index
|
36
|
+
from_lang = ARGV.delete_at(from_index)
|
37
|
+
else
|
38
|
+
ARGV.delete_at to_index
|
39
|
+
to_lang = ARGV.delete_at(to_index)
|
40
|
+
ARGV.delete_at from_index
|
41
|
+
from_lang = ARGV.delete_at(from_index)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
puts "You must supply both a to and from language"
|
45
|
+
puts "Example: konjac translate foo.txt from english to japanese"
|
46
|
+
exit 1
|
47
|
+
end
|
48
|
+
|
49
|
+
# Get a list of files to translate
|
50
|
+
files = []
|
51
|
+
while !ARGV.empty?
|
52
|
+
files += Dir.glob(File.expand_path(ARGV.shift))
|
53
|
+
end
|
54
|
+
files.uniq!
|
55
|
+
|
56
|
+
Translator.translate files, from_lang, to_lang
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
module Konjac
|
4
|
+
module Dictionary
|
5
|
+
class << self
|
6
|
+
def load(from_lang, to_lang, opts = {})
|
7
|
+
opts = { :force => false }.merge(opts)
|
8
|
+
return @pairs if loaded? && !opts[:force]
|
9
|
+
|
10
|
+
|
11
|
+
dict_dir = File.expand_path("~/.konjac/")
|
12
|
+
dict_path = dict_dir + "/dict.yml"
|
13
|
+
|
14
|
+
unless File.file?(dict_path)
|
15
|
+
FileUtils.mkpath dict_dir
|
16
|
+
FileUtils.touch dict_path
|
17
|
+
end
|
18
|
+
|
19
|
+
@dictionary = ::YAML.load_file(dict_path)
|
20
|
+
|
21
|
+
# Parse languages
|
22
|
+
from_lang = parse_language(from_lang)
|
23
|
+
to_lang = parse_language(to_lang)
|
24
|
+
|
25
|
+
# Build a list of search and replace pairs
|
26
|
+
@pairs = []
|
27
|
+
@dictionary["terms"].each do |term|
|
28
|
+
if term.has_key?(from_lang) && term.has_key?(to_lang)
|
29
|
+
@pairs << [term[from_lang], term[to_lang]]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@loaded = true
|
34
|
+
@pairs
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def loaded?
|
40
|
+
!!@loaded
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse_language(lang)
|
44
|
+
if @dictionary["languages"].has_key?(lang)
|
45
|
+
return lang
|
46
|
+
else
|
47
|
+
@dictionary["languages"].each do |main, alternatives|
|
48
|
+
return main if alternatives.include?(lang)
|
49
|
+
end
|
50
|
+
|
51
|
+
# If no match is found, give an error message and exit
|
52
|
+
raise Exceptions::InvalidLanguageError.new("No match found for language \"#{lang}\"")
|
53
|
+
exit 1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
File without changes
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Konjac
|
2
|
+
module Translator
|
3
|
+
class << self
|
4
|
+
def translate(files, from_lang, to_lang)
|
5
|
+
pairs = Dictionary.load(from_lang, to_lang)
|
6
|
+
|
7
|
+
files.each do |source|
|
8
|
+
# Read in file and replace matches in content
|
9
|
+
content = File.read(source)
|
10
|
+
pairs.each do |pair|
|
11
|
+
content.gsub! pair[0], pair[1]
|
12
|
+
end
|
13
|
+
|
14
|
+
# Write changed content to file
|
15
|
+
File.open(build_converted_file_name(source), "w") do |file|
|
16
|
+
file.puts content
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# Build converted file name by appending "_converted" to the file name
|
24
|
+
def build_converted_file_name(source)
|
25
|
+
dirname = File.dirname(source)
|
26
|
+
basename = File.basename(source, ".*")
|
27
|
+
extname = File.extname(source)
|
28
|
+
|
29
|
+
"#{dirname}/#{basename}_converted#{extname}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/konjac.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "konjac/version"
|
2
|
+
autoload :FileUtils, "fileutils"
|
3
|
+
|
4
|
+
module Konjac
|
5
|
+
autoload :CLI, "konjac/cli"
|
6
|
+
autoload :Dictionary, "konjac/dictionary"
|
7
|
+
autoload :Exceptions, "konjac/exceptions"
|
8
|
+
autoload :Manager, "konjac/manager"
|
9
|
+
autoload :Translator, "konjac/translator"
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: konjac
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bryan McKelvey
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &23263176 !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: *23263176
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: trollop
|
27
|
+
requirement: &23231568 !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: *23231568
|
36
|
+
description: A Ruby command-line utility for translating files using a YAML wordlist
|
37
|
+
email:
|
38
|
+
- bryan.mckelvey@gmail.com
|
39
|
+
executables:
|
40
|
+
- konjac
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- bin/konjac
|
50
|
+
- konjac.gemspec
|
51
|
+
- lib/konjac.rb
|
52
|
+
- lib/konjac/cli.rb
|
53
|
+
- lib/konjac/dictionary.rb
|
54
|
+
- lib/konjac/exceptions.rb
|
55
|
+
- lib/konjac/manager.rb
|
56
|
+
- lib/konjac/translator.rb
|
57
|
+
- lib/konjac/version.rb
|
58
|
+
homepage: http://brymck.herokuapp.com/
|
59
|
+
licenses: []
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project: konjac
|
78
|
+
rubygems_version: 1.8.11
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: A Ruby command-line utility for translating files using a YAML wordlist
|
82
|
+
test_files: []
|