togglate 0.1.3 → 0.1.4

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: 7d5c239c406f9d4efc4110ed2bdbd52b8883a20b
4
- data.tar.gz: 18e0df107f759b24428304dcf50eec2f38a3a95b
3
+ metadata.gz: 8838d984a567e19332eefd8db08ac37d50087ba9
4
+ data.tar.gz: 918a7a0d84ca37b6e509b03ee9f02b2e431d48f0
5
5
  SHA512:
6
- metadata.gz: 8486218902d6329ca64ce0ecf2566ef793ad7440b7837f8ce63615d4f3ea5717f5952ca8c1efa867e9342e26179e4c414031eb2d8af04340422c2852488d497a
7
- data.tar.gz: b685a75c8849329e5a9df1644adc5fe409843b3fe88f17331c6a93b0f0ab22f73995b2745081b569b57c284e07afcee02abc96cf9889816981bb7c107b3acfaf
6
+ metadata.gz: 963997000ded8bfa81f8246e6ba3dd6f09dfedf0fc207921919095001896bfc7e84d191f0dd5fdef4eb0a65d73a100fe22d36688f7a6521813bcdb0c5ddf9cec
7
+ data.tar.gz: 42948d55aa29bd9802fca99a13ba90335771c333bb1d9811f4dd410b222470e706ea20e17f8583477df6e4d883fabf6bce2f011510637c8f399d8221cbeab94a
@@ -13,6 +13,7 @@ class Togglate::BlockWrapper
13
13
  @translate = set_translate_opt(opts[:translate])
14
14
  @timeout = opts.fetch(:timeout, 5)
15
15
  @email = opts[:email]
16
+ @indent_re = /^\s{4,}\S/
16
17
  end
17
18
 
18
19
  def run
@@ -20,11 +21,23 @@ class Togglate::BlockWrapper
20
21
  end
21
22
 
22
23
  private
23
- def build_chunks(block_tags:[/^```/, /^{%/])
24
+ def build_chunks
24
25
  in_block = false
26
+ in_indent = false
25
27
  @text.each_line.chunk do |line|
26
- in_block = !in_block if block_tags.any? { |ex| line.match ex }
27
- blank_line?(line) && !in_block
28
+ in_block = in_block?(line, in_block)
29
+ prev_indent = in_indent
30
+ in_indent = in_indented_block?(line, in_indent, in_block)
31
+
32
+ if true_to_false?(prev_indent, in_indent)
33
+ if blank_line?(line)
34
+ true
35
+ else
36
+ :alone # line just after 4 indent block marked :alone
37
+ end
38
+ else
39
+ blank_line?(line) && !in_block && !in_indent
40
+ end
28
41
  end
29
42
  end
30
43
 
@@ -32,18 +45,72 @@ class Togglate::BlockWrapper
32
45
  !line.match(blank_line_re).nil?
33
46
  end
34
47
 
48
+ def true_to_false?(prev, curr)
49
+ # this captures the in-out state transition on 4 indent block.
50
+ [prev, curr] == [true, false]
51
+ end
52
+
53
+ def in_block?(line, in_block, block_tags:[/^```/, /^{%/])
54
+ return !in_block if block_tags.any? { |ex| line.match ex }
55
+ in_block
56
+ end
57
+
58
+ def in_indented_block?(line, status, in_block)
59
+ return false if in_block
60
+ if !status && line.match(@indent_re) ||
61
+ status && line.match(/^\s{,3}\S/)
62
+ !status
63
+ else
64
+ status
65
+ end
66
+ end
67
+
35
68
  def wrap_chunks(chunks)
69
+ # a line just after 4 indent block(marked :alone) is
70
+ # saved to local var 'reserve', then it is merged with
71
+ # next lines or wrapped solely depend the type of next lines
72
+ reserve = nil
36
73
  wrap_lines = chunks.inject([]) do |m, (is_blank_line, lines)|
37
- if is_blank_line || @wrap_exceptions.any? { |ex| lines[0].match ex }
74
+ next m.tap { reserve = lines } if is_blank_line == :alone
75
+
76
+ if is_blank_line || exception_block?(lines.first)
77
+ if reserve
78
+ m.push "\n"
79
+ m.push *wrapped_block(reserve)
80
+ end
38
81
  m.push *lines
39
82
  else
40
- m.push pretext(lines)
41
- m.push "\n\n", @wrapper[0], "\n", *lines, @wrapper[1], "\n"
83
+ if reserve
84
+ m.push "\n"
85
+ lines = reserve + lines
86
+ end
87
+ m.push *wrapped_block(lines)
42
88
  end
89
+ m.tap { reserve = nil }
43
90
  end
44
91
  @translate ? hash_to_translation(wrap_lines).join : wrap_lines.join
45
92
  end
46
93
 
94
+ def wrapped_block(contents)
95
+ [
96
+ pretext(contents),
97
+ "\n\n",
98
+ @wrapper[0],
99
+ "\n",
100
+ *contents,
101
+ @wrapper[1],
102
+ "\n"
103
+ ]
104
+ end
105
+
106
+ def exception_block?(line)
107
+ if @wrap_exceptions.empty?
108
+ false
109
+ else
110
+ (@wrap_exceptions + [@indent_re]).any? { |ex| line.match ex }
111
+ end
112
+ end
113
+
47
114
  def pretext(lines)
48
115
  if @translate
49
116
  hash_value = lines.join.hash
data/lib/togglate/cli.rb CHANGED
@@ -12,7 +12,7 @@ module Togglate
12
12
  def create(file)
13
13
  text = File.read(file)
14
14
  opts = symbolize_keys(options)
15
- blocks = [/^```/, /^ {4}/, /^{%/]
15
+ blocks = [/^```/, /^{%/]
16
16
  opts.update(wrap_exceptions:blocks) if opts[:code_block]
17
17
  opts.update(translate:nil) if opts[:translate].empty?
18
18
  puts Togglate.create(text, opts)
@@ -1,3 +1,3 @@
1
1
  module Togglate
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -16,6 +16,12 @@ Programming is fun.
16
16
  % ruby title.rb
17
17
  -->
18
18
 
19
+ ほんとに楽しい。
20
+
21
+ <!--original
22
+ Really fun.
23
+ -->
24
+
19
25
  ```ruby
20
26
  def hello
21
27
  puts :hello
@@ -4,6 +4,8 @@ Programming is fun.
4
4
 
5
5
  % ruby title.rb
6
6
 
7
+ Really fun.
8
+
7
9
  ```ruby
8
10
  def hello
9
11
  puts :hello
@@ -41,13 +41,86 @@ EOS
41
41
  expect(wrapper.send(:build_chunks).to_a).to eq exp
42
42
  end
43
43
  end
44
+
45
+ context "with fenced code blocks" do
46
+ it "wraps fenced code blocks as target blocks" do
47
+ text = <<-EOS
48
+ # title
49
+
50
+ text
51
+
52
+ ``` ruby
53
+ puts 'Hello'
54
+
55
+ p :World
56
+ ```
57
+ EOS
58
+ wrapper = Togglate::BlockWrapper.new(text)
59
+ exp = [
60
+ [false, ["# title\n"]],
61
+ [true, ["\n"]],
62
+ [false, ["text\n"]],
63
+ [true, ["\n"]],
64
+ [false, ["``` ruby\n", "puts 'Hello'\n", "\n", "p :World\n", "```\n"]]
65
+ ]
66
+ expect(wrapper.send(:build_chunks).to_a).to eq exp
67
+ end
68
+ end
69
+
70
+ context "with 4 indented code blocks" do
71
+ it "wraps them as target blocks" do
72
+ text =<<-EOS
73
+ #title
74
+
75
+ tell application 'Foo'
76
+
77
+ beep
78
+
79
+ end tell
80
+
81
+ line
82
+ EOS
83
+
84
+ wrapper = Togglate::BlockWrapper.new(text)
85
+ exp = [[false, ["#title\n"]],
86
+ [true, ["\n"]],
87
+ [false, [" tell application 'Foo'\n",
88
+ "\n",
89
+ " beep\n",
90
+ "\n",
91
+ " end tell\n",
92
+ "\n"]],
93
+ [:alone, [" line\n"]]]
94
+ expect(wrapper.send(:build_chunks).to_a).to eq exp
95
+ end
96
+ end
44
97
  end
45
98
 
46
99
  describe "#wrap_chunks" do
100
+ before do
101
+ @text =<<-EOS
102
+ #title
103
+
104
+ text
105
+ EOS
106
+ end
107
+
47
108
  it "returns wrapped text" do
48
109
  wrapper = Togglate::BlockWrapper.new(@text)
49
110
  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"
111
+ exp =<<-EOS
112
+ [translation here]
113
+
114
+ <!--original
115
+ #title
116
+ -->
117
+
118
+ [translation here]
119
+
120
+ <!--original
121
+ text
122
+ -->
123
+ EOS
51
124
  expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
52
125
  end
53
126
 
@@ -59,6 +132,84 @@ EOS
59
132
  expect(wrapper.send(:wrap_chunks, chunks)).to match(exp)
60
133
  end
61
134
  end
135
+
136
+ context "text has 4 indented code blocks" do
137
+ it "wraps sentences after the code blocks correctly" do
138
+ text =<<-EOS
139
+ #title
140
+
141
+ % echo hello
142
+
143
+ line
144
+ line2
145
+ EOS
146
+ wrapper = Togglate::BlockWrapper.new(text)
147
+ chunks = wrapper.send(:build_chunks)
148
+ exp =<<-EOS
149
+ [translation here]
150
+
151
+ <!--original
152
+ #title
153
+ -->
154
+
155
+ [translation here]
156
+
157
+ <!--original
158
+ % echo hello
159
+
160
+ -->
161
+
162
+ [translation here]
163
+
164
+ <!--original
165
+ line
166
+ line2
167
+ -->
168
+ EOS
169
+ expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
170
+ end
171
+
172
+ it "wraps sentences after the code blocks correctly 2" do
173
+ text =<<-EOS
174
+ #title
175
+
176
+ % echo hello
177
+
178
+ line
179
+
180
+ line2
181
+ EOS
182
+ wrapper = Togglate::BlockWrapper.new(text)
183
+ chunks = wrapper.send(:build_chunks)
184
+ exp =<<-EOS
185
+ [translation here]
186
+
187
+ <!--original
188
+ #title
189
+ -->
190
+
191
+ [translation here]
192
+
193
+ <!--original
194
+ % echo hello
195
+
196
+ -->
197
+
198
+ [translation here]
199
+
200
+ <!--original
201
+ line
202
+ -->
203
+
204
+ [translation here]
205
+
206
+ <!--original
207
+ line2
208
+ -->
209
+ EOS
210
+ expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
211
+ end
212
+ end
62
213
  end
63
214
 
64
215
  describe "Embed results of MyMemory translation service" do
@@ -21,7 +21,7 @@ describe Togglate::CLI do
21
21
 
22
22
  it "wraps code blocks" do
23
23
  Togglate::CLI.start(['create', 'README.md'])
24
- expect($stdout.string).to match(/<!--original\n\s{4}% ruby title\.rb\n-->/)
24
+ expect($stdout.string).to match(/<!--original\n\s{4}% ruby title\.rb\n\n-->/)
25
25
  end
26
26
 
27
27
  it "wraps gfm code blocks" do
@@ -29,10 +29,12 @@ describe Togglate::CLI do
29
29
  expect($stdout.string).to match(/<!--original.*```ruby.*```\n-->/m)
30
30
  end
31
31
 
32
- it "wraps sentences except code blocks" do
33
- Togglate::CLI.start(['create', 'README.md', '--code-block'])
34
- expect($stdout.string).to match(/^\n {4}% ruby title\.rb\n$/)
35
- expect($stdout.string).to match(/^\n```ruby.*```\n$/m)
32
+ context "set code-block option to true" do
33
+ it "not wraps fenced and indented code blocks" do
34
+ Togglate::CLI.start(['create', 'README.md', '--code-block'])
35
+ expect($stdout.string).to match(/^\n {4}% ruby title\.rb\n$/)
36
+ expect($stdout.string).to match(/^\n```ruby.*```\n$/m)
37
+ end
36
38
  end
37
39
  end
38
40
 
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kyoendo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-03 00:00:00.000000000 Z
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor