erbse 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
  SHA256:
3
- metadata.gz: a7fbd1dec8733c0729e2deca42168eb95696704b85c0b0d9c37a1d14e64587ba
4
- data.tar.gz: 0fbe06409ad39912f9b4e81183426c54a7f05be522ccc16f395640f304611280
3
+ metadata.gz: 40d1d10b1c63ad9100e2112b150ef69e0d3a08f44767a34ee47b5ffe5483b940
4
+ data.tar.gz: e06b5f57fa13d926932949213076c01f576f5aebafb6e99b10ad9a56b97c7237
5
5
  SHA512:
6
- metadata.gz: 512cfadfb79da8590ed0f19cd1f0f69eaf157cbef7004b6e0b6053c14df5cb95cd3980157e2cc98b4d51a60e47452fc838330da997eaa1c7e3308ba65f8aec0e
7
- data.tar.gz: f8bfa636d35d4cbff380b7c9926a8b2ae520ac6441f61b156379c90dc29904e993d97738b113ab5dde68aa5652723e482935e2c66d9d898c9a98dd4ed8df1bc0
6
+ metadata.gz: ec08209d87ebb3235f2f8fdc87d1299037af405454c62ce1165d0d2d132191247d32d82259f1c288a26105390db2f54099f7c1f7462782600d0f30850e0761c1
7
+ data.tar.gz: 55307e0004e6f6de876394840e0c23f07b077911e388789f0d1d433a63ce129b4b103e0663efaaf7cf979f9bdca3d1869788d3b1df2af98673e9517eff0ccb93
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.4
2
+
3
+ * Newlines are now properly reflected in the compiled code.
4
+
1
5
  # 0.1.3
2
6
 
3
7
  * Do not trim whitespace between ERB tags.
@@ -1,7 +1,7 @@
1
1
  module Erbse
2
2
  class Parser
3
- # ERB_EXPR = /<%(=|\#)?(.*?)%>(\n)*/m # this is the desired pattern.
4
- ERB_EXPR = /<%(=+|-|\#|@\s|%)?(.*?)[-=]?%>(\n)*/m # this is for backward-compatibility.
3
+ # ERB_EXPR = /(\n)|<%(=|\#)?(.*?)%>(\n)*/m # this is the desired pattern.
4
+ ERB_EXPR = /(\n)|<=|<%(=+|-|\#|@\s|%)?(.*?)[-=]?%>/m # this is for backward-compatibility.
5
5
  # BLOCK_EXPR = /\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/
6
6
  BLOCK_EXPR = /\sdo\s*\z|\sdo\s+\|[^|]*\|\s*\z/
7
7
  BLOCK_EXEC = /\A\s*(if|unless)\b|#{BLOCK_EXPR}/
@@ -21,7 +21,7 @@ module Erbse
21
21
  buffers << result
22
22
  match = nil
23
23
 
24
- str.scan(ERB_EXPR) do |indicator, code, newlines|
24
+ str.scan(ERB_EXPR) do |newline, indicator, code|
25
25
  match = Regexp.last_match
26
26
  len = match.begin(0) - pos
27
27
 
@@ -29,6 +29,11 @@ module Erbse
29
29
  pos = match.end(0)
30
30
  ch = indicator ? indicator[0] : nil
31
31
 
32
+ if newline
33
+ buffers.last << [:static, "#{text}\n"] << [:newline]
34
+ next
35
+ end
36
+
32
37
  if text and !text.empty? # text
33
38
  buffers.last << [:static, text]
34
39
  end
@@ -56,10 +61,6 @@ module Erbse
56
61
  buffers.last << [:code, code]
57
62
  end
58
63
  end
59
-
60
- # FIXME: only adds one newline.
61
- # TODO: does that influence speed?
62
- buffers.last << [:newline] if newlines
63
64
  end
64
65
 
65
66
  # add text after last/none ERB tag.
@@ -1,3 +1,3 @@
1
1
  module Erbse
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -16,22 +16,28 @@ Text
16
16
 
17
17
  it "what" do
18
18
  Erbse::Parser.new.(str).must_equal [:multi,
19
- [:static, "\n"],
20
- [:dynamic, " true "], [:newline],
21
- [:static, "Text\n"],
19
+ [:static, "\n"], [:newline],
20
+ [:dynamic, " true "],
21
+ [:static, "\n"], [:newline],
22
+ [:static, "Text\n"], [:newline],
22
23
  [:erb, :block, " form_for do ", [:multi,
23
24
  [:dynamic, " 1 "],
24
- [:code, " 2 "], [:newline],
25
+ [:code, " 2 "],
26
+ [:static, "\n"], [:newline],
25
27
  [:static, " "],
26
- [:erb, :block, " nested do ", [:multi, [:newline],
28
+ [:erb, :block, " nested do ", [:multi,
29
+ [:static, "\n"], [:newline],
27
30
  [:static, " "],
28
- [:dynamic, " this "], [:newline],
29
- [:static, " <a/>\n "],
30
- ]], [:newline]]]]
31
+ [:dynamic, " this "],
32
+ [:static, "\n"], [:newline],
33
+ [:static, " <a/>\n"], [:newline],
34
+ [:static, " "],
35
+ ]],
36
+ [:static, "\n"], [:newline]]]]
31
37
  end
32
38
 
33
39
  it "generates ruby" do
34
- code = %{_buf = []; _buf << ("@@".freeze); _buf << ( true ); @; _buf << ("Text@@".freeze); _erbse_blockfilter1 = form_for do ; _erbse_blockfilter2 = ''; _erbse_blockfilter2 << (( 1 ).to_s); 2 ; @; _erbse_blockfilter2 << (" ".freeze); _erbse_blockfilter3 = nested do ; _erbse_blockfilter4 = ''; @; _erbse_blockfilter4 << (" ".freeze); _erbse_blockfilter4 << (( this ).to_s); @; _erbse_blockfilter4 << (" <a/>@@ ".freeze); _erbse_blockfilter4; end; _erbse_blockfilter2 << ((_erbse_blockfilter3).to_s); @; _erbse_blockfilter2; end; _buf << (_erbse_blockfilter1); _buf = _buf.join("".freeze)}
40
+ code = %{_buf = []; _buf << (\"@@\".freeze); @; _buf << ( true ); _buf << (\"@@\".freeze); @; _buf << (\"Text@@\".freeze); @; _erbse_blockfilter1 = form_for do ; _erbse_blockfilter2 = ''; _erbse_blockfilter2 << (( 1 ).to_s); 2 ; _erbse_blockfilter2 << (\"@@\".freeze); @; _erbse_blockfilter2 << (\" \".freeze); _erbse_blockfilter3 = nested do ; _erbse_blockfilter4 = ''; _erbse_blockfilter4 << (\"@@\".freeze); @; _erbse_blockfilter4 << (\" \".freeze); _erbse_blockfilter4 << (( this ).to_s); _erbse_blockfilter4 << (\"@@\".freeze); @; _erbse_blockfilter4 << (\" <a/>@@\".freeze); @; _erbse_blockfilter4 << (\" \".freeze); _erbse_blockfilter4; end; _erbse_blockfilter2 << ((_erbse_blockfilter3).to_s); _erbse_blockfilter2 << (\"@@\".freeze); @; _erbse_blockfilter2; end; _buf << (_erbse_blockfilter1); _buf = _buf.join(\"\".freeze)}
35
41
  ruby = Erbse::Engine.new.(str).gsub("\n", "@").gsub('\n', "@@")
36
42
  # puts ruby
37
43
  ruby.must_equal code
@@ -51,26 +57,33 @@ Text
51
57
  }
52
58
  it "what" do
53
59
  Erbse::Parser.new.(str).must_equal [:multi,
54
- [:static, "\n"],
60
+ [:static, "\n"], [:newline],
55
61
  [:block, " 2.times do |i| ", [:multi,
56
- [:newline],
62
+ [:static, "\n"], [:newline],
57
63
  [:static, " "],
58
- [:dynamic, " i+1 "], [:newline], [:static, " "],
59
- [:code, " puts "], [:newline]]], [:newline],
60
- [:block, " if 1 == 1 ", [:multi, [:newline],
61
- [:static, " Hello\n"]]], [:newline]]
64
+ [:dynamic, " i+1 "],
65
+ [:static, "\n"], [:newline], [:static, " "],
66
+ [:code, " puts "],
67
+ [:static, "\n"], [:newline]
68
+ ]],
69
+ [:static, "\n"], [:newline],
70
+ [:static, "\n"], [:newline],
71
+ [:block, " if 1 == 1 ", [:multi,
72
+ [:static, "\n"], [:newline],
73
+ [:static, " Hello\n"], [:newline]]],
74
+ [:static, "\n"], [:newline]]
62
75
  end
63
76
 
64
77
  it do
65
78
  ruby = Erbse::Engine.new.(str)
66
79
  ruby = ruby.gsub("\n", "@")
67
80
  # ruby.must_equal %{_buf = []; self ; 2.times do |i| ; _buf << ( i+1 ); puts ; end; _buf = _buf.join(\"\".freeze)}
68
- ruby.must_equal '_buf = []; _buf << ("\n".freeze); 2.times do |i| ; @; _buf << (" ".freeze); _buf << ( i+1 ); @; _buf << (" ".freeze); puts ; @; end; @; if 1 == 1 ; @; _buf << (" Hello\n".freeze); end; @; _buf = _buf.join("".freeze)'
81
+ ruby.must_equal '_buf = []; _buf << ("\n".freeze); @; 2.times do |i| ; _buf << ("\n".freeze); @; _buf << (" ".freeze); _buf << ( i+1 ); _buf << ("\n".freeze); @; _buf << (" ".freeze); puts ; _buf << ("\n".freeze); @; end; _buf << ("\n".freeze); @; _buf << ("\n".freeze); @; if 1 == 1 ; _buf << ("\n".freeze); @; _buf << (" Hello\n".freeze); @; end; _buf << ("\n".freeze); @; _buf = _buf.join("".freeze)'
69
82
  end
70
83
 
71
84
  it do
72
85
  ruby = Erbse::Engine.new.(str)
73
- eval(ruby).must_equal "\n 1 2 Hello\n"
86
+ eval(ruby).must_equal "\n\n 1\n \n\n 2\n \n\n\n\n Hello\n\n"
74
87
  end
75
88
  end
76
89
 
@@ -78,7 +91,7 @@ Text
78
91
  let (:str) { %{Holla
79
92
  Hi}
80
93
  }
81
- it { Erbse::Parser.new.(str).must_equal [:multi, [:static, "Holla\nHi"]] }
94
+ it { Erbse::Parser.new.(str).must_equal [:multi, [:static, "Holla\n"], [:newline], [:static, "Hi"]] }
82
95
  end
83
96
 
84
97
  # comments
@@ -93,12 +106,12 @@ Hi
93
106
  } }
94
107
 
95
108
  it do
96
- Erbse::Parser.new.(str).must_equal [:multi, [:static, "Hello\n"], [:newline], [:static, "Hola\n"], [:newline], [:newline], [:static, "Hi\n"], [:code, " # this "], [:newline]]
109
+ Erbse::Parser.new.(str).must_equal [:multi, [:static, "Hello\n"], [:newline], [:static, "\n"], [:newline], [:static, "Hola\n"], [:newline], [:newline], [:static, "\n"], [:newline], [:static, "Hi\n"], [:newline], [:code, " # this "], [:static, "\n"], [:newline]]
97
110
  end
98
111
 
99
112
  it do
100
113
  ruby = Erbse::Engine.new.(str).gsub("\n", "@").gsub('\n', "@@")
101
- code = %{_buf = []; _buf << ("Hello@@".freeze); @; _buf << ("Hola@@".freeze); @; @; _buf << ("Hi@@".freeze); # this ; @; _buf = _buf.join("".freeze)}
114
+ code = %{_buf = []; _buf << (\"Hello@@\".freeze); @; _buf << (\"@@\".freeze); @; _buf << (\"Hola@@\".freeze); @; @; _buf << (\"@@\".freeze); @; _buf << (\"Hi@@\".freeze); @; # this ; _buf << (\"@@\".freeze); @; _buf = _buf.join(\"\".freeze)}
102
115
  ruby.must_equal code
103
116
  end
104
117
 
@@ -106,14 +119,14 @@ Hi
106
119
  it { Erbse::Parser.new.(%{Yo
107
120
  <%# bla do %>
108
121
  <%# end %>
109
- 1}).must_equal [:multi, [:static, "Yo\n"], [:newline], [:newline], [:static, "1"]] }
122
+ 1}).must_equal [:multi, [:static, "Yo\n"], [:newline], [:static, "\n"], [:newline], [:static, "\n"], [:newline], [:static, "1"]] }
110
123
  end
111
124
 
112
125
  describe "content after last ERB tag" do
113
126
  let (:str) { %{<b><%= 1 %>bla
114
127
  blubb</b>} }
115
128
 
116
- it { Erbse::Parser.new.(str).must_equal [:multi, [:static, "<b>"], [:dynamic, " 1 "], [:static, "bla\nblubb</b>"]] }
129
+ it { Erbse::Parser.new.(str).must_equal [:multi, [:static, "<b>"], [:dynamic, " 1 "], [:static, "bla\n"], [:newline], [:static, "blubb</b>"]] }
117
130
  end
118
131
 
119
132
  describe "<%* %>" do
@@ -177,16 +190,42 @@ blubb</b>} }
177
190
 
178
191
  it "parses quoted conditionals correctly" do
179
192
  Erbse::Parser.new.(str2).must_equal [:multi,
180
- [:static, "\n "],
193
+ [:static, "\n"],
194
+ [:newline],
195
+ [:static, " "],
181
196
  [:erb, :block, " form_for do ", [:multi,
197
+ [:static, "\n"],
182
198
  [:newline],
183
199
  [:static, " "],
184
200
  [:dynamic, " link_to \"string\", path, \"v-if\" => \"trigger\" "],
201
+ [:static, "\n"],
185
202
  [:newline],
186
203
  [:static, " "]
187
204
  ]]]
188
205
  end
189
206
  end
207
+
208
+ describe "newlines" do
209
+ let (:str2) { %{
210
+ abc
211
+ <%= 'foo' %>
212
+ def}
213
+ }
214
+
215
+ it "puts newlines after code" do
216
+ Erbse::Parser.new.(str2).must_equal [:multi,
217
+ [:static, "\n"],
218
+ [:newline],
219
+ [:static, " abc\n"],
220
+ [:newline],
221
+ [:static, " "],
222
+ [:dynamic, " 'foo' "],
223
+ [:static, "\n"],
224
+ [:newline],
225
+ [:static, ' def'],
226
+ ]
227
+ end
228
+ end
190
229
 
191
230
  describe "<%@ %>" do
192
231
  let (:str) { %{<%@ content = capture do %>
@@ -196,7 +235,7 @@ blubb</b>} }
196
235
 
197
236
  it do
198
237
  ruby = Erbse::Engine.new.(str).gsub("\n", "@").gsub('\n', "@@")
199
- code = %{_buf = []; content = capture do ; _erbse_blockfilter1 = ''; @; _erbse_blockfilter1 << (\" Yo!@@ \".freeze); _erbse_blockfilter1 << (( 1 ).to_s); @; _erbse_blockfilter1; end; _buf = _buf.join(\"\".freeze)}
238
+ code = %{_buf = []; content = capture do ; _erbse_blockfilter1 = ''; _erbse_blockfilter1 << (\"@@\".freeze); @; _erbse_blockfilter1 << (\" Yo!@@\".freeze); @; _erbse_blockfilter1 << (\" \".freeze); _erbse_blockfilter1 << (( 1 ).to_s); _erbse_blockfilter1 << (\"@@\".freeze); @; _erbse_blockfilter1; end; _buf = _buf.join(\"\".freeze)}
200
239
  ruby.must_equal code
201
240
  end
202
241
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erbse
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
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-20 00:00:00.000000000 Z
11
+ date: 2020-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: temple