erb-formatter 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/erb-formatter.gemspec +1 -1
- data/lib/erb/formatter/version.rb +1 -1
- data/lib/erb/formatter.rb +8 -47
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a957537254444c222e81398e138e7d3bd22512b782cd3a67302ad9b1de8c12bc
|
4
|
+
data.tar.gz: 23eee273e38701317b779ec455353fa801212b80b78c786d5a01b61fc54942c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05176b2dca77e2500ed5d00ce531e395cf4a11a35d38132fb7539f516d98e007480a32b690f53715a6ca79f967aa773d9698856464e24145e329c8389adad54c
|
7
|
+
data.tar.gz: 2d088aa641242b2a30b8d4a21abe739cf365838950ea9c8aa9d39b395aaf3bda966f3159ee66bb03ad486e337fd6536694b064bdd834d7d138eb59518db44cfe
|
data/erb-formatter.gemspec
CHANGED
data/lib/erb/formatter.rb
CHANGED
@@ -38,8 +38,6 @@ class ERB::Formatter
|
|
38
38
|
|
39
39
|
ERB_TAG = %r{(<%(?:==|=|-|))\s*(.*?)\s*(-?%>)}m
|
40
40
|
ERB_PLACEHOLDER = %r{erb[a-z0-9]+tag}
|
41
|
-
ERB_END = %r{(<%-?)\s*(end)\s*(-?%>)}
|
42
|
-
ERB_ELSE = %r{(<%-?)\s*(else|elsif\b.*)\s*(-?%>)}
|
43
41
|
|
44
42
|
TAG_NAME = /[a-z0-9_:-]+/
|
45
43
|
TAG_NAME_ONLY = /\A#{TAG_NAME}\z/
|
@@ -49,10 +47,12 @@ class ERB::Formatter
|
|
49
47
|
|
50
48
|
SELF_CLOSING_TAG = /\A(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\z/i
|
51
49
|
|
52
|
-
|
50
|
+
RUBY_OPEN_BLOCK = ->(code) do
|
53
51
|
# is nil when the parsing is broken, meaning it's an open expression
|
54
52
|
Ripper.sexp(code).nil?
|
55
53
|
end.freeze
|
54
|
+
RUBY_CLOSE_BLOCK = /\Aend\z/
|
55
|
+
RUBY_REOPEN_BLOCK = /\A(else|elsif\b(.*)|when\b(.*))\z/
|
56
56
|
|
57
57
|
RUBOCOP_STDIN_MARKER = "===================="
|
58
58
|
|
@@ -146,45 +146,6 @@ class ERB::Formatter
|
|
146
146
|
attr_html
|
147
147
|
end
|
148
148
|
|
149
|
-
def format_erb_attributes(string)
|
150
|
-
erb_scanner = StringScanner.new(string.to_s)
|
151
|
-
erb_pre_pos = 0
|
152
|
-
until erb_scanner.eos?
|
153
|
-
if erb_scanner.scan_until(erb_tags_regexp)
|
154
|
-
erb_pre_match = erb_scanner.pre_match
|
155
|
-
erb_pre_match = erb_pre_match[erb_pre_pos..]
|
156
|
-
erb_pre_pos = erb_scanner.pos
|
157
|
-
|
158
|
-
erb_code = erb_tags[erb_scanner.captures.first]
|
159
|
-
|
160
|
-
format_attributes(erb_pre_match)
|
161
|
-
|
162
|
-
erb_open, ruby_code, erb_close = ERB_TAG.match(erb_code).captures
|
163
|
-
full_erb_tag = "#{erb_open} #{ruby_code} #{erb_close}"
|
164
|
-
|
165
|
-
case ruby_code
|
166
|
-
when /\Aend\z/
|
167
|
-
tag_stack_pop('%erb%', ruby_code)
|
168
|
-
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
169
|
-
when /\A(else|elsif\b(.*))\z/
|
170
|
-
tag_stack_pop('%erb%', ruby_code)
|
171
|
-
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
172
|
-
tag_stack_push('%erb%', ruby_code)
|
173
|
-
when ERB_OPEN_BLOCK
|
174
|
-
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
175
|
-
tag_stack_push('%erb%', ruby_code)
|
176
|
-
else
|
177
|
-
ruby_code = format_ruby(ruby_code, autoclose: false)
|
178
|
-
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
179
|
-
end
|
180
|
-
else
|
181
|
-
rest = erb_scanner.rest.to_s
|
182
|
-
format_erb_attributes(rest)
|
183
|
-
erb_scanner.terminate
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
149
|
def tag_stack_push(tag_name, code)
|
189
150
|
tag_stack << [tag_name, code]
|
190
151
|
p PUSH: tag_stack if @debug
|
@@ -258,8 +219,8 @@ class ERB::Formatter
|
|
258
219
|
|
259
220
|
def format_ruby(code, autoclose: false)
|
260
221
|
if autoclose
|
261
|
-
code += "\nend" unless
|
262
|
-
code += "\n}" unless
|
222
|
+
code += "\nend" unless RUBY_OPEN_BLOCK["#{code}\nend"]
|
223
|
+
code += "\n}" unless RUBY_OPEN_BLOCK["#{code}\n}"]
|
263
224
|
end
|
264
225
|
p RUBY_IN_: code if @debug
|
265
226
|
|
@@ -303,16 +264,16 @@ class ERB::Formatter
|
|
303
264
|
erb_open << ' ' unless ruby_code.start_with?('#')
|
304
265
|
|
305
266
|
case ruby_code
|
306
|
-
when
|
267
|
+
when RUBY_CLOSE_BLOCK
|
307
268
|
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
|
308
269
|
tag_stack_pop('%erb%', ruby_code)
|
309
270
|
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
310
|
-
when
|
271
|
+
when RUBY_REOPEN_BLOCK
|
311
272
|
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
|
312
273
|
tag_stack_pop('%erb%', ruby_code)
|
313
274
|
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
314
275
|
tag_stack_push('%erb%', ruby_code)
|
315
|
-
when
|
276
|
+
when RUBY_OPEN_BLOCK
|
316
277
|
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
|
317
278
|
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
318
279
|
tag_stack_push('%erb%', ruby_code)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erb-formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Elia Schito
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: syntax_tree
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
description:
|
28
28
|
email:
|
29
29
|
- elia@schito.me
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
71
|
+
rubygems_version: 3.3.7
|
72
72
|
signing_key:
|
73
73
|
specification_version: 4
|
74
74
|
summary: Format ERB files with speed and precision.
|