togglate 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3e882d0a7b6b8494adb72255373853b954d24bf
4
- data.tar.gz: 2dfa813070babdae9203d5b53075a4eb788ab8dd
3
+ metadata.gz: 7d5c239c406f9d4efc4110ed2bdbd52b8883a20b
4
+ data.tar.gz: 18e0df107f759b24428304dcf50eec2f38a3a95b
5
5
  SHA512:
6
- metadata.gz: 3e1034613d6c3c779ea614d79caf82c82c76fdbf34aa944574169d7dce51d68e621567802faeeee62d7f5b0bd6ca7bb640f2a83fbb3a875779d0fead09c82929
7
- data.tar.gz: f72edcd08d81e63d99566ecb0e24ae7dfafe00dc6c5bcc24f86db20a4991d6016543422eb15370cd4d47c51595fa41336253848773dd1e198354f128ff07e821
6
+ metadata.gz: 8486218902d6329ca64ce0ecf2566ef793ad7440b7837f8ce63615d4f3ea5717f5952ca8c1efa867e9342e26179e4c414031eb2d8af04340422c2852488d497a
7
+ data.tar.gz: b685a75c8849329e5a9df1644adc5fe409843b3fe88f17331c6a93b0f0ab22f73995b2745081b569b57c284e07afcee02abc96cf9889816981bb7c107b3acfaf
@@ -16,36 +16,44 @@ class Togglate::BlockWrapper
16
16
  end
17
17
 
18
18
  def run
19
- wrap_with chunk_by_space
19
+ wrap_chunks build_chunks
20
20
  end
21
21
 
22
22
  private
23
- def chunk_by_space(block_tags:[/^```/], space_re:/^\s*$/)
23
+ def build_chunks(block_tags:[/^```/, /^{%/])
24
24
  in_block = false
25
25
  @text.each_line.chunk do |line|
26
26
  in_block = !in_block if block_tags.any? { |ex| line.match ex }
27
- !line.match(space_re).nil? && !in_block
27
+ blank_line?(line) && !in_block
28
28
  end
29
29
  end
30
30
 
31
- def wrap_with(chunks)
32
- wrap_lines = chunks.inject([]) do |m, (is_space, lines)|
33
- if is_space || @wrap_exceptions.any? { |ex| lines[0].match ex }
31
+ def blank_line?(line, blank_line_re:/^\s*$/)
32
+ !line.match(blank_line_re).nil?
33
+ end
34
+
35
+ def wrap_chunks(chunks)
36
+ wrap_lines = chunks.inject([]) do |m, (is_blank_line, lines)|
37
+ if is_blank_line || @wrap_exceptions.any? { |ex| lines[0].match ex }
34
38
  m.push *lines
35
39
  else
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
40
+ m.push pretext(lines)
43
41
  m.push "\n\n", @wrapper[0], "\n", *lines, @wrapper[1], "\n"
44
42
  end
45
43
  end
46
44
  @translate ? hash_to_translation(wrap_lines).join : wrap_lines.join
47
45
  end
48
46
 
47
+ def pretext(lines)
48
+ if @translate
49
+ hash_value = lines.join.hash
50
+ sentences_to_translate[hash_value] = lines.join
51
+ hash_value
52
+ else
53
+ @pretext
54
+ end
55
+ end
56
+
49
57
  def set_translate_opt(opt)
50
58
  case opt
51
59
  when Hash, FalseClass, NilClass
@@ -66,12 +74,10 @@ class Togglate::BlockWrapper
66
74
  lines.map { |line| translates[line] || line }
67
75
  end
68
76
 
69
- using CoreExt
70
-
71
77
  def request_translation
72
78
  Mymemory.config.email = @email if @email
73
79
  res = {}
74
- sentences_to_translate.thread_with do |k, text|
80
+ thread_with(sentences_to_translate) do |k, text|
75
81
  begin
76
82
  timeout(@timeout) { res[k] = ::Mymemory.translate(text, @translate) }
77
83
  rescue Timeout::Error
@@ -80,4 +86,14 @@ class Togglate::BlockWrapper
80
86
  end
81
87
  res
82
88
  end
89
+
90
+ def thread_with(hash)
91
+ mem = []
92
+ hash.map do |*item|
93
+ Thread.new(*item) do |*_item|
94
+ mem << yield(*_item)
95
+ end
96
+ end.each(&:join)
97
+ mem
98
+ end
83
99
  end
data/lib/togglate/cli.rb CHANGED
@@ -12,7 +12,8 @@ module Togglate
12
12
  def create(file)
13
13
  text = File.read(file)
14
14
  opts = symbolize_keys(options)
15
- opts.update(wrap_exceptions:[/^```/, /^ {4}/]) if opts[:code_block]
15
+ blocks = [/^```/, /^ {4}/, /^{%/]
16
+ opts.update(wrap_exceptions:blocks) if opts[:code_block]
16
17
  opts.update(translate:nil) if opts[:translate].empty?
17
18
  puts Togglate.create(text, opts)
18
19
  rescue => e
@@ -1,3 +1,3 @@
1
1
  module Togglate
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/togglate.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require "mymemory"
2
2
 
3
3
  require "togglate/version"
4
- require "togglate/core_ext"
5
4
  require "togglate/block_wrapper"
6
5
  require "togglate/cli"
7
6
 
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Togglate::BlockWrapper do
4
+ before do
5
+ @text = "#title\n\n\ntext\n"
6
+ end
7
+
8
+ describe "#build_chunks" do
9
+ it "returns chunked array" do
10
+ wrapper = Togglate::BlockWrapper.new(@text)
11
+ exp = [[false, ["#title\n"]],
12
+ [true, ["\n", "\n"]],
13
+ [false, ["text\n"]]]
14
+ expect(wrapper.send(:build_chunks).to_a).to eq exp
15
+ end
16
+
17
+ context "with liquid tags" do
18
+ it "wraps liquid tag blocks as target blocks" do
19
+ text =<<-EOS
20
+ #title
21
+
22
+ text
23
+
24
+ {% highlight bash %}
25
+ bash code
26
+
27
+ here
28
+ {% endhighlight %}
29
+ EOS
30
+
31
+ wrapper = Togglate::BlockWrapper.new(text)
32
+ exp = [[false, ["#title\n"]],
33
+ [true, ["\n"]],
34
+ [false, ["text\n"]],
35
+ [true, ["\n"]],
36
+ [false, ["{% highlight bash %}\n",
37
+ "bash code\n",
38
+ "\n",
39
+ "here\n",
40
+ "{% endhighlight %}\n"]]]
41
+ expect(wrapper.send(:build_chunks).to_a).to eq exp
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#wrap_chunks" do
47
+ it "returns wrapped text" do
48
+ wrapper = Togglate::BlockWrapper.new(@text)
49
+ chunks = wrapper.send(:build_chunks)
50
+ exp = "[translation here]\n\n<!--original\n#title\n-->\n\n\n[translation here]\n\n<!--original\ntext\n-->\n"
51
+ expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
52
+ end
53
+
54
+ context "optional pre-text" do
55
+ it "returns wrapped text with a custom text" do
56
+ wrapper = Togglate::BlockWrapper.new(@text, pretext:"-- translation --")
57
+ chunks = wrapper.send(:build_chunks)
58
+ exp = "-- translation --\n\n<!--original\n"
59
+ expect(wrapper.send(:wrap_chunks, chunks)).to match(exp)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "Embed results of MyMemory translation service" do
65
+ describe ".new with translate option" do
66
+ it "sets en-ja option when passed true" do
67
+ wrapper = Togglate::BlockWrapper.new('I need you.', translate:true)
68
+ opt = {to: :ja}
69
+ expect(wrapper.instance_variable_get('@translate')).to eq opt
70
+ end
71
+
72
+ it "sets passed options" do
73
+ opt = {from: :en, to: :it, email:true}
74
+ wrapper = Togglate::BlockWrapper.new('I need you.', translate:opt)
75
+ expect(wrapper.instance_variable_get('@translate')).to eq opt
76
+ end
77
+ end
78
+
79
+ describe "#wrap_chunks" do
80
+ it "sets translated sentences to pretext" do
81
+ text = "#Title\n\nProgramming is fun.\n"
82
+ opt = {from: :en, to: :ja}
83
+ wrapper = Togglate::BlockWrapper.new(text, translate:opt)
84
+ chunks = wrapper.send(:build_chunks)
85
+ exp = /プログラミングは楽しいです.*<!--original/m
86
+ expect(wrapper.send(:wrap_chunks, chunks)).to match(exp)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -20,64 +20,3 @@ describe Togglate do
20
20
  end
21
21
  end
22
22
  end
23
-
24
- describe Togglate::BlockWrapper do
25
- before do
26
- @text = "#title\n\n\ntext\n"
27
- end
28
-
29
- describe "#chunk_by_space" do
30
- it "returns chunked array" do
31
- wrapper = Togglate::BlockWrapper.new(@text)
32
- exp = [[false, ["#title\n"]],
33
- [true, ["\n", "\n"]],
34
- [false, ["text\n"]]]
35
- expect(wrapper.send(:chunk_by_space).to_a).to eq exp
36
- end
37
- end
38
-
39
- describe "#wrap_with" do
40
- it "returns wrapped text" do
41
- wrapper = Togglate::BlockWrapper.new(@text)
42
- chunks = wrapper.send(:chunk_by_space)
43
- exp = "[translation here]\n\n<!--original\n#title\n-->\n\n\n[translation here]\n\n<!--original\ntext\n-->\n"
44
- expect(wrapper.send(:wrap_with, chunks)).to eq exp
45
- end
46
-
47
- context "optional pre-text" do
48
- it "returns wrapped text with a custom text" do
49
- wrapper = Togglate::BlockWrapper.new(@text, pretext:"-- translation --")
50
- chunks = wrapper.send(:chunk_by_space)
51
- exp = "-- translation --\n\n<!--original\n"
52
- expect(wrapper.send(:wrap_with, chunks)).to match(exp)
53
- end
54
- end
55
- end
56
-
57
- describe "Embed results of MyMemory translation service" do
58
- describe ".new with translate option" do
59
- it "sets en-ja option when passed true" do
60
- wrapper = Togglate::BlockWrapper.new('I need you.', translate:true)
61
- opt = {to: :ja}
62
- expect(wrapper.instance_variable_get('@translate')).to eq opt
63
- end
64
-
65
- it "sets passed options" do
66
- opt = {from: :en, to: :it, email:true}
67
- wrapper = Togglate::BlockWrapper.new('I need you.', translate:opt)
68
- expect(wrapper.instance_variable_get('@translate')).to eq opt
69
- end
70
- end
71
-
72
- describe "#wrap_with" do
73
- it "sets translated sentences to pretext" do
74
- text = "#Title\n\nProgramming is fun.\n"
75
- opt = {from: :en, to: :ja}
76
- wrapper = Togglate::BlockWrapper.new(text, translate:opt)
77
- chunks = wrapper.send(:chunk_by_space)
78
- exp = /プログラミングは楽しいです.*<!--original/m
79
- expect(wrapper.send(:wrap_with, chunks)).to match(exp)
80
- end
81
- end
82
- end
83
- end
data/togglate.gemspec CHANGED
@@ -16,7 +16,7 @@ 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
+ spec.required_ruby_version = '>=2.0.0'
20
20
 
21
21
  spec.add_dependency "thor"
22
22
  spec.add_dependency "mymemory"
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.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kyoendo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -113,12 +113,12 @@ files:
113
113
  - lib/togglate.rb
114
114
  - lib/togglate/block_wrapper.rb
115
115
  - lib/togglate/cli.rb
116
- - lib/togglate/core_ext.rb
117
116
  - lib/togglate/version.rb
118
117
  - spec/fixtures/README.ja.md
119
118
  - spec/fixtures/README.md
120
119
  - spec/fixtures/translated_text.json
121
120
  - spec/spec_helper.rb
121
+ - spec/togglate_block_wrapper_spec.rb
122
122
  - spec/togglate_cli_spec.rb
123
123
  - spec/togglate_spec.rb
124
124
  - togglate.gemspec
@@ -134,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
134
  requirements:
135
135
  - - ">="
136
136
  - !ruby/object:Gem::Version
137
- version: 2.1.0
137
+ version: 2.0.0
138
138
  required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  requirements:
140
140
  - - ">="
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  requirements: []
144
144
  rubyforge_project:
145
- rubygems_version: 2.2.0
145
+ rubygems_version: 2.2.2
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: Create base text for translation, in which original is togglable
@@ -151,6 +151,7 @@ test_files:
151
151
  - spec/fixtures/README.md
152
152
  - spec/fixtures/translated_text.json
153
153
  - spec/spec_helper.rb
154
+ - spec/togglate_block_wrapper_spec.rb
154
155
  - spec/togglate_cli_spec.rb
155
156
  - spec/togglate_spec.rb
156
157
  has_rdoc:
@@ -1,13 +0,0 @@
1
- module CoreExt
2
- refine Hash do
3
- def thread_with
4
- mem = []
5
- map do |*item|
6
- Thread.new(*item) do |*_item|
7
- mem << yield(*_item)
8
- end
9
- end.each(&:join)
10
- mem
11
- end
12
- end
13
- end