erb-formatter 0.7.1 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/lib/erb/formatter/version.rb +1 -1
- data/lib/erb/formatter.rb +15 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 360fdcbb367ec4e87072a757436ffceae053d2020bcf9b0f3b3d72d497f1687d
|
4
|
+
data.tar.gz: 78d63c4817533bc6d651dda6e48773e599907c9a7321ddbe6a718bd310fc4264
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ee0269dee3ad8e5c1240af3730bc0b166faf7581b801e771c81d7c69d121b0aed6abca7176e8cb746ac67a290ddd626484730dbb762c723ddad8a0180101f5a
|
7
|
+
data.tar.gz: 2ed97281f686c7d33f40f604003857e5c182a89a984a4bb23cd16ec7c65b3bfc9d8876f43fdaceac5012a0c3dec9b9508fb36cba1c8bc9b04b01926d36cf712d
|
data/README.md
CHANGED
@@ -119,6 +119,23 @@ let g:ale_fixers = {
|
|
119
119
|
\}
|
120
120
|
```
|
121
121
|
|
122
|
+
### With [Zed](https://zed.dev/) editor
|
123
|
+
|
124
|
+
With the gem installed, configure `settings.json` to use the formatter as an external command
|
125
|
+
|
126
|
+
```json
|
127
|
+
"language_overrides": {
|
128
|
+
"ERB": {
|
129
|
+
"formatter": {
|
130
|
+
"external": {
|
131
|
+
"command": "erb-format",
|
132
|
+
"arguments": ["--stdin", "--print-width", "80"]
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
137
|
+
```
|
138
|
+
|
122
139
|
## Development
|
123
140
|
|
124
141
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/erb/formatter.rb
CHANGED
@@ -26,9 +26,11 @@ class ERB::Formatter
|
|
26
26
|
|
27
27
|
class Error < StandardError; end
|
28
28
|
|
29
|
+
SPACES = /\s+/m
|
30
|
+
|
29
31
|
# https://stackoverflow.com/a/317081
|
30
32
|
ATTR_NAME = %r{[^\r\n\t\f\v= '"<>]*[^\r\n\t\f\v= '"<>/]} # not ending with a slash
|
31
|
-
UNQUOTED_VALUE =
|
33
|
+
UNQUOTED_VALUE = %r{[^<>'"\s]+}
|
32
34
|
UNQUOTED_ATTR = %r{#{ATTR_NAME}=#{UNQUOTED_VALUE}}
|
33
35
|
SINGLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}='[^']*?')}m
|
34
36
|
DOUBLE_QUOTE_ATTR = %r{(?:#{ATTR_NAME}="[^"]*?")}m
|
@@ -59,6 +61,7 @@ class ERB::Formatter
|
|
59
61
|
end.freeze
|
60
62
|
end
|
61
63
|
|
64
|
+
RUBY_STANDALONE_BLOCK = /\A(yield|next)\b/
|
62
65
|
RUBY_CLOSE_BLOCK = /\Aend\z/
|
63
66
|
RUBY_REOPEN_BLOCK = /\A(else|elsif\b(.*)|when\b(.*))\z/
|
64
67
|
|
@@ -136,17 +139,18 @@ class ERB::Formatter
|
|
136
139
|
attrs.scan(ATTR).flatten.each do |attr|
|
137
140
|
attr.strip!
|
138
141
|
name, value = attr.split('=', 2)
|
139
|
-
if UNQUOTED_ATTR =~ attr
|
140
|
-
attr_html << indented("#{name}=\"#{value}\"")
|
141
|
-
next
|
142
|
-
end
|
143
142
|
|
144
143
|
if value.nil?
|
145
144
|
attr_html << indented("#{name}")
|
146
145
|
next
|
147
146
|
end
|
148
147
|
|
149
|
-
|
148
|
+
if /\A#{UNQUOTED_VALUE}\z/o.match?(value)
|
149
|
+
attr_html << indented("#{name}=\"#{value}\"")
|
150
|
+
next
|
151
|
+
end
|
152
|
+
|
153
|
+
value_parts = value[1...-1].strip.split(SPACES)
|
150
154
|
value_parts.sort_by!(&@css_class_sorter) if name == 'class' && @css_class_sorter
|
151
155
|
|
152
156
|
full_attr = "#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}"
|
@@ -231,7 +235,7 @@ class ERB::Formatter
|
|
231
235
|
|
232
236
|
return if text.match?(/\A\s*\z/m) # empty
|
233
237
|
|
234
|
-
text = text.gsub(
|
238
|
+
text = text.gsub(SPACES, ' ').strip
|
235
239
|
|
236
240
|
offset = indented("").size
|
237
241
|
# Restore full line width if there are less than 40 columns available
|
@@ -303,6 +307,10 @@ class ERB::Formatter
|
|
303
307
|
erb_open << ' ' unless ruby_code.start_with?('#')
|
304
308
|
|
305
309
|
case ruby_code
|
310
|
+
when RUBY_STANDALONE_BLOCK
|
311
|
+
ruby_code = format_ruby(ruby_code, autoclose: false)
|
312
|
+
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
|
313
|
+
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
306
314
|
when RUBY_CLOSE_BLOCK
|
307
315
|
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
|
308
316
|
tag_stack_pop('%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.7.
|
4
|
+
version: 0.7.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: 2024-
|
11
|
+
date: 2024-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: syntax_tree
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
|
-
rubygems_version: 3.5.
|
99
|
+
rubygems_version: 3.5.4
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: Format ERB files with speed and precision.
|