togglate 0.1.5 → 0.1.6

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: 65547c6e81808aac70c1c4d2e5090f9d8f5922b6
4
- data.tar.gz: ab0e68cce467df0c9ddb5cb80bbe68bd186b9832
3
+ metadata.gz: 57da012eeca551cd3f241a7da1ce2b9c6876f183
4
+ data.tar.gz: 767fd04b88c9d744b131f177da7492c969f86173
5
5
  SHA512:
6
- metadata.gz: 196facfe16d6a389e7768f621fc32a604d92395daf3c9549364a7d4d374134e2f8483a12ddf923e0388dcb8bfb15e2d08201511054beb9e11eda922d009d4134
7
- data.tar.gz: 18fac88443c51e12acc9e85eee4c9d2631df1788bc7047bcdf4f7b6801322be669af499664718f3f45964e691b3e8c4345427f39f39079cb1fce24ea63bacf80
6
+ metadata.gz: 8b1d2f7f6e7eac949163256db714b2352190d4b484f17da6269f5d2d14289fc6b8a3f6fbd7c4e025be06ce1b07fdb9b7597512e5ad8993346949af33bd979484
7
+ data.tar.gz: 0745a5419ff582ed151f354814209eb90e3ca9c87ee07441d632ae25f213d529e9f0b1de9a05009b326081f55c7682a8e8f11bcded872cee75bebdbdf936dc65
@@ -16,8 +16,7 @@ class Togglate::BlockWrapper
16
16
  @indent_re = /^\s{4,}\S/
17
17
  @block_tags = {
18
18
  fenced: /^```/,
19
- liquid: /^{%/,
20
- html: /^<(?!img|br|hr|!--).+>\s*$/
19
+ liquid: /^{%/
21
20
  }
22
21
  @wrap_exceptions = wrap_exceptions.map { |key| @block_tags[key] }
23
22
  end
@@ -28,25 +27,46 @@ class Togglate::BlockWrapper
28
27
 
29
28
  private
30
29
  def build_chunks
31
- in_block = false
32
- in_indent = false
33
- @text.each_line.chunk do |line|
34
- in_block = in_block?(line, in_block)
35
- prev_indent = in_indent
36
- in_indent = in_indented_block?(line, in_indent, in_block)
37
-
38
- if true_to_false?(prev_indent, in_indent)
39
- if blank_line?(line)
40
- true
30
+ pre_wrap_for(:html) do |text|
31
+ in_block = false
32
+ in_indent = false
33
+ chunks = text.each_line.chunk do |line|
34
+ in_block = in_block?(line, in_block)
35
+ prev_indent = in_indent
36
+ in_indent = in_indented_block?(line, in_indent, in_block)
37
+
38
+ if true_to_false?(prev_indent, in_indent)
39
+ if blank_line?(line)
40
+ true
41
+ else
42
+ :_alone # line just after 4 indent block marked :_alone
43
+ end
41
44
  else
42
- :_alone # line just after 4 indent block marked :_alone
45
+ blank_line?(line) && !in_block && !in_indent
43
46
  end
44
- else
45
- blank_line?(line) && !in_block && !in_indent
46
47
  end
47
48
  end
48
49
  end
49
50
 
51
+ def pre_wrap_for(*targets, tag:"```__TEMP-WRAPPER-TAG__\n")
52
+ # Wrap targets blocks with a pair of fenced blocks once before
53
+ # chunking text, then remove the pair after the chunking.
54
+ #
55
+ # NOTE: I have tried to include 'four indented blocks' here,
56
+ # but abandoned. Because it was complicated to handle
57
+ # indented lines inside of html tag blocks.
58
+ target_re = { html: /^<(\w+)\s*.*?>\n.*?^<\/\1>\n/m }
59
+ text =
60
+ targets.inject(@text) do |txt, target|
61
+ txt.gsub(target_re[target]) { "#{tag}#{$&}#{tag}" }
62
+ end
63
+ chunks = yield(text)
64
+ chunks.map do |k, lines|
65
+ lines = lines[1..-2] if lines.first.match(/#{tag}/)
66
+ [k, lines]
67
+ end
68
+ end
69
+
50
70
  def blank_line?(line)
51
71
  !line.match(@blank_line_re).nil?
52
72
  end
@@ -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 = %i(fenced liquid html)
15
+ blocks = %i(fenced liquid)
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.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -3,10 +3,12 @@ require 'togglate'
3
3
  require "stringio"
4
4
  require "fakeweb"
5
5
 
6
- class String
7
- def ~
8
- margin = scan(/^ +/).map(&:size).min
9
- gsub(/^ {#{margin}}/, '')
6
+ module StringExt
7
+ refine String do
8
+ def ~
9
+ margin = scan(/^ +/).map(&:size).min
10
+ gsub(/^ {#{margin}}/, '')
11
+ end
10
12
  end
11
13
  end
12
14
 
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ using StringExt
2
3
 
3
4
  describe Togglate::BlockWrapper do
4
5
  before do
@@ -16,18 +17,17 @@ describe Togglate::BlockWrapper do
16
17
 
17
18
  context "with liquid tags" do
18
19
  it "wraps liquid tag blocks as target blocks" do
19
- text =<<-EOS
20
- #title
20
+ text = ~<<-EOS
21
+ #title
21
22
 
22
- text
23
+ text
23
24
 
24
- {% highlight bash %}
25
- bash code
26
-
27
- here
28
- {% endhighlight %}
29
- EOS
25
+ {% highlight bash %}
26
+ bash code
30
27
 
28
+ here
29
+ {% endhighlight %}
30
+ EOS
31
31
  wrapper = Togglate::BlockWrapper.new(text)
32
32
  exp = [[false, ["#title\n"]],
33
33
  [true, ["\n"]],
@@ -44,17 +44,17 @@ EOS
44
44
 
45
45
  context "with fenced code blocks" do
46
46
  it "wraps fenced code blocks as target blocks" do
47
- text = <<-EOS
48
- # title
47
+ text = ~<<-EOS
48
+ # title
49
49
 
50
- text
50
+ text
51
51
 
52
- ``` ruby
53
- puts 'Hello'
52
+ ``` ruby
53
+ puts 'Hello'
54
54
 
55
- p :World
56
- ```
57
- EOS
55
+ p :World
56
+ ```
57
+ EOS
58
58
  wrapper = Togglate::BlockWrapper.new(text)
59
59
  exp = [
60
60
  [false, ["# title\n"]],
@@ -69,18 +69,17 @@ EOS
69
69
 
70
70
  context "with 4 indented code blocks" do
71
71
  it "wraps them as target blocks" do
72
- text =<<-EOS
73
- #title
74
-
75
- tell application 'Foo'
72
+ text = ~<<-EOS
73
+ #title
76
74
 
77
- beep
75
+ tell application 'Foo'
78
76
 
79
- end tell
77
+ beep
80
78
 
81
- line
82
- EOS
79
+ end tell
83
80
 
81
+ line
82
+ EOS
84
83
  wrapper = Togglate::BlockWrapper.new(text)
85
84
  exp = [[false, ["#title\n"]],
86
85
  [true, ["\n"]],
@@ -97,16 +96,15 @@ EOS
97
96
 
98
97
  context "with html tag blocks" do
99
98
  it "wraps them as target blocks" do
100
- text =<<-EOS
101
- #title
102
-
103
- <table>
104
- <tr><th>Header</th></tr>
99
+ text = ~<<-EOS
100
+ #title
105
101
 
106
- <tr><td>Data</td></tr>
107
- </table>
108
- EOS
102
+ <table>
103
+ <tr><th>Header</th></tr>
109
104
 
105
+ <tr><td>Data</td></tr>
106
+ </table>
107
+ EOS
110
108
  wrapper = Togglate::BlockWrapper.new(text)
111
109
  exp = [[false, ["#title\n"]],
112
110
  [true, ["\n"]],
@@ -118,19 +116,64 @@ EOS
118
116
  expect(wrapper.send(:build_chunks).to_a).to eq exp
119
117
  end
120
118
 
121
- it "wraps self-closing tags as target blocks" do
122
- text =<<-EOS
123
- #title
119
+ it "wraps irregularly indented tags" do
120
+ text = ~<<-EOS
121
+ #title
122
+
123
+ <table>
124
+ <tr><th>Header</th></tr>
125
+
126
+ <tr><td>Data</td></tr>
127
+ </table>
128
+
129
+ sentence
130
+ EOS
131
+ wrapper = Togglate::BlockWrapper.new(text)
132
+ exp = [[false, ["#title\n"]],
133
+ [true, ["\n"]],
134
+ [false, ["<table>\n",
135
+ "<tr><th>Header</th></tr>\n",
136
+ "\n",
137
+ "<tr><td>Data</td></tr>\n",
138
+ "</table>\n"]],
139
+ [true, ["\n"]],
140
+ [false, ["sentence\n"]]]
141
+ expect(wrapper.send(:build_chunks).to_a).to eq exp
142
+ end
143
+
144
+ it "wraps html block which includes 4 or more indented parts" do
145
+ text = ~<<-EOS
146
+ <table>
147
+ <tr>
148
+ <th>
149
+ Header
150
+ </th>
151
+ </tr>
152
+ </table>
153
+ EOS
154
+ wrapper = Togglate::BlockWrapper.new(text)
155
+ exp = [[false, ["<table>\n",
156
+ " <tr>\n",
157
+ " <th>\n",
158
+ " Header\n",
159
+ " </th>\n",
160
+ " </tr>\n",
161
+ "</table>\n"]]]
162
+ expect(wrapper.send(:build_chunks).to_a).to eq exp
163
+ end
124
164
 
125
- <!-- comment -->
165
+ it "wraps self-closing tags as target blocks" do
166
+ text = ~<<-EOS
167
+ #title
126
168
 
127
- <br />
169
+ <!-- comment -->
128
170
 
129
- <img src="img.png" />
171
+ <br />
130
172
 
131
- <hr />
132
- EOS
173
+ <img src="img.png" />
133
174
 
175
+ <hr />
176
+ EOS
134
177
  wrapper = Togglate::BlockWrapper.new(text)
135
178
  exp = [[false, ["#title\n"]],
136
179
  [true, ["\n"]],
@@ -148,29 +191,29 @@ EOS
148
191
 
149
192
  describe "#wrap_chunks" do
150
193
  before do
151
- @text =<<-EOS
152
- #title
194
+ @text = ~<<-EOS
195
+ #title
153
196
 
154
- text
155
- EOS
197
+ text
198
+ EOS
156
199
  end
157
200
 
158
201
  it "returns wrapped text" do
159
202
  wrapper = Togglate::BlockWrapper.new(@text)
160
203
  chunks = wrapper.send(:build_chunks)
161
- exp =<<-EOS
162
- [translation here]
204
+ exp = ~<<-EOS
205
+ [translation here]
163
206
 
164
- <!--original
165
- #title
166
- -->
207
+ <!--original
208
+ #title
209
+ -->
167
210
 
168
- [translation here]
211
+ [translation here]
169
212
 
170
- <!--original
171
- text
172
- -->
173
- EOS
213
+ <!--original
214
+ text
215
+ -->
216
+ EOS
174
217
  expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
175
218
  end
176
219
 
@@ -185,78 +228,78 @@ EOS
185
228
 
186
229
  context "text has 4 indented code blocks" do
187
230
  it "wraps sentences after the code blocks correctly" do
188
- text =<<-EOS
189
- #title
231
+ text = ~<<-EOS
232
+ #title
190
233
 
191
- % echo hello
234
+ % echo hello
192
235
 
193
- line
194
- line2
195
- EOS
236
+ line
237
+ line2
238
+ EOS
196
239
  wrapper = Togglate::BlockWrapper.new(text)
197
240
  chunks = wrapper.send(:build_chunks)
198
- exp =<<-EOS
199
- [translation here]
241
+ exp = ~<<-EOS
242
+ [translation here]
200
243
 
201
- <!--original
202
- #title
203
- -->
244
+ <!--original
245
+ #title
246
+ -->
204
247
 
205
- [translation here]
248
+ [translation here]
206
249
 
207
- <!--original
208
- % echo hello
250
+ <!--original
251
+ % echo hello
209
252
 
210
- -->
253
+ -->
211
254
 
212
- [translation here]
255
+ [translation here]
213
256
 
214
- <!--original
215
- line
216
- line2
217
- -->
218
- EOS
257
+ <!--original
258
+ line
259
+ line2
260
+ -->
261
+ EOS
219
262
  expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
220
263
  end
221
264
 
222
265
  it "wraps sentences after the code blocks correctly 2" do
223
- text =<<-EOS
224
- #title
266
+ text = ~<<-EOS
267
+ #title
225
268
 
226
- % echo hello
269
+ % echo hello
227
270
 
228
- line
271
+ line
229
272
 
230
- line2
231
- EOS
273
+ line2
274
+ EOS
232
275
  wrapper = Togglate::BlockWrapper.new(text)
233
276
  chunks = wrapper.send(:build_chunks)
234
- exp =<<-EOS
235
- [translation here]
277
+ exp = ~<<-EOS
278
+ [translation here]
236
279
 
237
- <!--original
238
- #title
239
- -->
280
+ <!--original
281
+ #title
282
+ -->
240
283
 
241
- [translation here]
284
+ [translation here]
242
285
 
243
- <!--original
244
- % echo hello
286
+ <!--original
287
+ % echo hello
245
288
 
246
- -->
289
+ -->
247
290
 
248
- [translation here]
291
+ [translation here]
249
292
 
250
- <!--original
251
- line
252
- -->
293
+ <!--original
294
+ line
295
+ -->
253
296
 
254
- [translation here]
297
+ [translation here]
255
298
 
256
- <!--original
257
- line2
258
- -->
259
- EOS
299
+ <!--original
300
+ line2
301
+ -->
302
+ EOS
260
303
  expect(wrapper.send(:wrap_chunks, chunks)).to eq exp
261
304
  end
262
305
  end
@@ -36,9 +36,9 @@ describe Togglate::CLI do
36
36
  expect($stdout.string).to match(/\n\n```ruby.*```\n\n/m)
37
37
  end
38
38
 
39
- it "not wraps html tags" do
39
+ it "wraps html tags" do
40
40
  Togglate::CLI.start(['create', 'README.md', '--code-block'])
41
- expect($stdout.string).to match(/\n\n<table>.*<\/table>\n\n/m)
41
+ expect($stdout.string).to match(/<!--original.*<table>.*<\/table>\n-->/m)
42
42
  end
43
43
  end
44
44
  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.1.5
4
+ version: 0.1.6
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-09 00:00:00.000000000 Z
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor