togglate 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/togglate/block_wrapper.rb +54 -4
- data/lib/togglate/cli.rb +11 -1
- data/lib/togglate/core_ext.rb +13 -0
- data/lib/togglate/version.rb +1 -1
- data/lib/togglate.rb +3 -0
- data/spec/fixtures/README.md +1 -1
- data/spec/fixtures/translated_text.json +1 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/togglate_cli_spec.rb +7 -0
- data/spec/togglate_spec.rb +42 -4
- data/togglate.gemspec +3 -0
- metadata +34 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0bd54100b950a27e6a14dc0726059bb3c685b73
|
4
|
+
data.tar.gz: 31260ffc060f2eacef3e0c50d109c09f1aca348e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0b5fc69dd995054151e8facb47833b3cf1c2c61b3243287810cf8d61cecd3b7a25db9e84861178415dc261f6c43ae842a30c1c03ba945f90ea972e47224c277
|
7
|
+
data.tar.gz: 53bb9896d923f5fb8a325fbd76e59a325a763bbadacb5b5254771129d004d6dd77e6db3ea8585c68a410ad5db272fa36134060dc83bb4e995ee4b73604bd91ec
|
@@ -1,11 +1,18 @@
|
|
1
|
+
require "timeout"
|
2
|
+
|
1
3
|
class Togglate::BlockWrapper
|
2
4
|
def initialize(text,
|
3
|
-
wrapper:%
|
5
|
+
wrapper:%w(<!--original -->),
|
6
|
+
pretext:"[translation here]",
|
4
7
|
wrap_exceptions:[],
|
5
8
|
**opts)
|
6
9
|
@text = text
|
7
10
|
@wrapper = wrapper
|
11
|
+
@pretext = pretext
|
8
12
|
@wrap_exceptions = wrap_exceptions
|
13
|
+
@translate = set_translate_opt(opts[:translate])
|
14
|
+
@timeout = opts.fetch(:timeout, 3)
|
15
|
+
@email = opts[:email]
|
9
16
|
end
|
10
17
|
|
11
18
|
def run
|
@@ -22,12 +29,55 @@ class Togglate::BlockWrapper
|
|
22
29
|
end
|
23
30
|
|
24
31
|
def wrap_with(chunks)
|
25
|
-
chunks.inject([]) do |m, (is_space, lines)|
|
32
|
+
wrap_lines = chunks.inject([]) do |m, (is_space, lines)|
|
26
33
|
if is_space || @wrap_exceptions.any? { |ex| lines[0].match ex }
|
27
34
|
m.push *lines
|
28
35
|
else
|
29
|
-
|
36
|
+
if @translate
|
37
|
+
hash_value = lines.join.hash
|
38
|
+
sentences_to_translate[hash_value] = lines.join
|
39
|
+
m.push hash_value
|
40
|
+
else
|
41
|
+
m.push @pretext
|
42
|
+
end
|
43
|
+
m.push "\n\n", @wrapper[0], "\n", *lines, @wrapper[1], "\n"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
@translate ? hash_to_translation(wrap_lines).join : wrap_lines.join
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_translate_opt(opt)
|
50
|
+
case opt
|
51
|
+
when Hash, FalseClass, NilClass
|
52
|
+
opt
|
53
|
+
when TrueClass
|
54
|
+
{to: :ja}
|
55
|
+
else
|
56
|
+
raise ArgumentError
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def sentences_to_translate
|
61
|
+
@sentences_to_translate ||= {}
|
62
|
+
end
|
63
|
+
|
64
|
+
def hash_to_translation(lines)
|
65
|
+
translates = request_translation
|
66
|
+
lines.map { |line| translates[line] || line }
|
67
|
+
end
|
68
|
+
|
69
|
+
using CoreExt
|
70
|
+
|
71
|
+
def request_translation
|
72
|
+
Mymemory.config.email = @email if @email
|
73
|
+
res = {}
|
74
|
+
sentences_to_translate.thread_with do |k, text|
|
75
|
+
begin
|
76
|
+
timeout(@timeout) { res[k] = ::Mymemory.translate(text, @translate) }
|
77
|
+
rescue Timeout::Error
|
78
|
+
res[k] = @pretext
|
30
79
|
end
|
31
|
-
end
|
80
|
+
end
|
81
|
+
res
|
32
82
|
end
|
33
83
|
end
|
data/lib/togglate/cli.rb
CHANGED
@@ -7,9 +7,12 @@ module Togglate
|
|
7
7
|
option :embed_code, aliases:'-e', default:true, type: :boolean, desc:"Enable code embeding to false"
|
8
8
|
option :toggle_link_text, type: :array, default:["*", "hide"]
|
9
9
|
option :code_block, aliases:'-c', default:false, type: :boolean, desc:"Enable code blocks not to be wrapped"
|
10
|
+
option :translate, aliases:'-t', type: :hash, default:{}, desc:"Embed machine translated text. ex.-t=to:ja"
|
11
|
+
option :email, desc:"Passing a valid email extends a limit of Mymemory anonymous usage from 100 to 1000 requests/day"
|
10
12
|
def create(file)
|
11
13
|
opts = symbolize_keys(options)
|
12
14
|
opts.update(wrap_exceptions:[/^```/, /^ {4}/]) if opts[:code_block]
|
15
|
+
opts.update(translate:nil) if opts[:translate].empty?
|
13
16
|
puts Togglate.create(file, opts)
|
14
17
|
end
|
15
18
|
|
@@ -35,7 +38,14 @@ module Togglate
|
|
35
38
|
|
36
39
|
no_tasks do
|
37
40
|
def symbolize_keys(options)
|
38
|
-
options.inject({})
|
41
|
+
options.inject({}) do |h, (k,v)|
|
42
|
+
h[k.intern] =
|
43
|
+
case v
|
44
|
+
when Hash then symbolize_keys(v)
|
45
|
+
else v
|
46
|
+
end
|
47
|
+
h
|
48
|
+
end
|
39
49
|
end
|
40
50
|
end
|
41
51
|
end
|
data/lib/togglate/version.rb
CHANGED
data/lib/togglate.rb
CHANGED
data/spec/fixtures/README.md
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
{"responseData":{"translatedText":"プログラミングは楽しいです。"},"responseDetails":"", "responseStatus":200, "responderId":"234", "matches":[{"id":"0", "segment":"Programming is fun.", "translation":"プログラミングは楽しいです。", "quality":"70", "reference":"Machine Translation provided by Google, Microsoft, Worldlingo or MyMemory customized engine.", "usage-count":1, "subject":"All", "created-by":"MT!", "last-updated-by":"MT!", "create-date":"2014-02-23", "last-update-date":"2014-02-23", "match":0.85}, {"id":"427501654", "segment":"Programming", "translation":"プログラミング", "quality":"80", "reference":"\/\/ja.wikipedia.org\/wiki\/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0", "usage-count":6, "subject":"All", "created-by":"Wikipedia", "last-updated-by":"Wikipedia", "create-date":"2013-08-24 08:51:26", "last-update-date":"2013-08-24 08:51:26", "match":0.66}, {"id":"427408903", "segment":"Programming", "translation":"打ち込み", "quality":"80", "reference":"\/\/en.wikipedia.org\/wiki\/Programming_(music)", "usage-count":3, "subject":"All", "created-by":"", "last-updated-by":"Wikipedia", "create-date":"2012-08-19 22:36:30", "last-update-date":"2012-08-19 22:36:30", "match":0.65}]}
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
2
|
require 'togglate'
|
3
3
|
require "stringio"
|
4
|
+
require "fakeweb"
|
4
5
|
|
5
6
|
class String
|
6
7
|
def ~
|
@@ -17,4 +18,9 @@ end
|
|
17
18
|
|
18
19
|
RSpec.configure do |c|
|
19
20
|
c.include Helpers
|
21
|
+
c.before do
|
22
|
+
FakeWeb.clean_registry
|
23
|
+
body = File.read(File.join(source_root, 'translated_text.json'))
|
24
|
+
FakeWeb.register_uri(:get, %r(http://mymemory\.translated\.net), body:body)
|
25
|
+
end
|
20
26
|
end
|
data/spec/togglate_cli_spec.rb
CHANGED
@@ -60,6 +60,13 @@ describe Togglate::CLI do
|
|
60
60
|
expect($stdout.string).to match(/<script.*showme.*<\/script>/m)
|
61
61
|
end
|
62
62
|
end
|
63
|
+
|
64
|
+
context "translate option with Mymemory API" do
|
65
|
+
it "embeds translated text as its pretext" do
|
66
|
+
Togglate::CLI.start(['create', 'README.md', '--translate', 'to:ja'])
|
67
|
+
expect($stdout.string).to match(/プログラミングは楽しい/)
|
68
|
+
end
|
69
|
+
end
|
63
70
|
end
|
64
71
|
|
65
72
|
describe "#append_code" do
|
data/spec/togglate_spec.rb
CHANGED
@@ -7,10 +7,13 @@ describe Togglate do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
describe Togglate::BlockWrapper do
|
10
|
+
before do
|
11
|
+
@text = "#title\n\n\ntext\n"
|
12
|
+
end
|
13
|
+
|
10
14
|
describe "#chunk_by_space" do
|
11
15
|
it "returns chunked array" do
|
12
|
-
|
13
|
-
wrapper = Togglate::BlockWrapper.new(text)
|
16
|
+
wrapper = Togglate::BlockWrapper.new(@text)
|
14
17
|
exp = [[false, ["#title\n"]],
|
15
18
|
[true, ["\n", "\n"]],
|
16
19
|
[false, ["text\n"]]]
|
@@ -20,11 +23,46 @@ describe Togglate::BlockWrapper do
|
|
20
23
|
|
21
24
|
describe "#wrap_with" do
|
22
25
|
it "returns wrapped text" do
|
23
|
-
|
24
|
-
wrapper = Togglate::BlockWrapper.new(text)
|
26
|
+
wrapper = Togglate::BlockWrapper.new(@text)
|
25
27
|
chunks = wrapper.send(:chunk_by_space)
|
26
28
|
exp = "[translation here]\n\n<!--original\n#title\n-->\n\n\n[translation here]\n\n<!--original\ntext\n-->\n"
|
27
29
|
expect(wrapper.send(:wrap_with, chunks)).to eq exp
|
28
30
|
end
|
31
|
+
|
32
|
+
context "optional pre-text" do
|
33
|
+
it "returns wrapped text with a custom text" do
|
34
|
+
wrapper = Togglate::BlockWrapper.new(@text, pretext:"-- translation --")
|
35
|
+
chunks = wrapper.send(:chunk_by_space)
|
36
|
+
exp = "-- translation --\n\n<!--original\n"
|
37
|
+
expect(wrapper.send(:wrap_with, chunks)).to match(exp)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "Embed results of MyMemory translation service" do
|
43
|
+
describe ".new with translate option" do
|
44
|
+
it "sets en-ja option when passed true" do
|
45
|
+
wrapper = Togglate::BlockWrapper.new('I need you.', translate:true)
|
46
|
+
opt = {to: :ja}
|
47
|
+
expect(wrapper.instance_variable_get('@translate')).to eq opt
|
48
|
+
end
|
49
|
+
|
50
|
+
it "sets passed options" do
|
51
|
+
opt = {from: :en, to: :it, email:true}
|
52
|
+
wrapper = Togglate::BlockWrapper.new('I need you.', translate:opt)
|
53
|
+
expect(wrapper.instance_variable_get('@translate')).to eq opt
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#wrap_with" do
|
58
|
+
it "sets translated sentences to pretext" do
|
59
|
+
text = "#Title\n\nProgramming is fun.\n"
|
60
|
+
opt = {from: :en, to: :ja}
|
61
|
+
wrapper = Togglate::BlockWrapper.new(text, translate:opt)
|
62
|
+
chunks = wrapper.send(:chunk_by_space)
|
63
|
+
exp = /プログラミングは楽しいです.*<!--original/m
|
64
|
+
expect(wrapper.send(:wrap_with, chunks)).to match(exp)
|
65
|
+
end
|
66
|
+
end
|
29
67
|
end
|
30
68
|
end
|
data/togglate.gemspec
CHANGED
@@ -16,9 +16,12 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
|
+
spec.required_ruby_version = '>=2.1.0'
|
19
20
|
|
20
21
|
spec.add_dependency "thor"
|
22
|
+
spec.add_dependency "mymemory"
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.5"
|
22
24
|
spec.add_development_dependency "rake"
|
23
25
|
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "fakeweb"
|
24
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: togglate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kyoendo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mymemory
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +80,20 @@ dependencies:
|
|
66
80
|
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: fakeweb
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
69
97
|
description:
|
70
98
|
email:
|
71
99
|
- postagie@gmail.com
|
@@ -85,8 +113,10 @@ files:
|
|
85
113
|
- lib/togglate.rb
|
86
114
|
- lib/togglate/block_wrapper.rb
|
87
115
|
- lib/togglate/cli.rb
|
116
|
+
- lib/togglate/core_ext.rb
|
88
117
|
- lib/togglate/version.rb
|
89
118
|
- spec/fixtures/README.md
|
119
|
+
- spec/fixtures/translated_text.json
|
90
120
|
- spec/spec_helper.rb
|
91
121
|
- spec/togglate_cli_spec.rb
|
92
122
|
- spec/togglate_spec.rb
|
@@ -103,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
133
|
requirements:
|
104
134
|
- - ">="
|
105
135
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
136
|
+
version: 2.1.0
|
107
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
138
|
requirements:
|
109
139
|
- - ">="
|
@@ -117,6 +147,7 @@ specification_version: 4
|
|
117
147
|
summary: Create base text for translation, in which original is togglable
|
118
148
|
test_files:
|
119
149
|
- spec/fixtures/README.md
|
150
|
+
- spec/fixtures/translated_text.json
|
120
151
|
- spec/spec_helper.rb
|
121
152
|
- spec/togglate_cli_spec.rb
|
122
153
|
- spec/togglate_spec.rb
|