erb-formatter 0.5.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/erb-formatter.gemspec +3 -0
- data/lib/erb/formatter/command_line.rb +28 -5
- data/lib/erb/formatter/version.rb +1 -1
- data/lib/erb/formatter.rb +22 -9
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a91fb15a3934ccb7a9589ebfefd60bd2b95610acb90259358a26239a34d7a3cb
|
4
|
+
data.tar.gz: 576ac293a9932d8c81d2ef9a62cd98fc3a39a01da9c3b8ac7919438768e18c28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 460305512646f76855effa266fb439083e571f9d14538e2b6417e00fec52859c2311d7ab2e2e44d32164320cb868d8d94e7c3118b2a855fd8270dfbb66303d17
|
7
|
+
data.tar.gz: 9de4c93e2cf39ab24cf0b882b7c23291f812d9685fb6618b8787f425433288c1c3b9d4f3208118abedf54a193d01715734b3edbd523a8e694840a4eecd1230ba
|
data/README.md
CHANGED
@@ -125,6 +125,12 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
125
125
|
|
126
126
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
127
127
|
|
128
|
+
In order to run a specific test, use the following command:
|
129
|
+
|
130
|
+
```bash
|
131
|
+
m test/erb/test_formatter.rb:123
|
132
|
+
```
|
133
|
+
|
128
134
|
## Contributing
|
129
135
|
|
130
136
|
Bug reports and pull requests are welcome on GitHub at https://github.com/nebulab/erb-formatter.
|
data/erb-formatter.gemspec
CHANGED
@@ -3,6 +3,19 @@ require 'erb/formatter'
|
|
3
3
|
require 'optparse'
|
4
4
|
|
5
5
|
class ERB::Formatter::CommandLine
|
6
|
+
def self.tailwindcss_class_sorter(css_path)
|
7
|
+
css = File.read(css_path)
|
8
|
+
|
9
|
+
css = css.tr("\n", " ").gsub(%r{\/\*.*?\*\/},"") # remove comments
|
10
|
+
css = css.gsub(%r<@media.*?\{>, "") # strip media queries
|
11
|
+
css = css.scan(%r<(?:^|\}|\{) *(\S.*?) *\{>).join(" ") # extract selectors
|
12
|
+
classes = css.tr(","," ").split(" ").grep(/\./).uniq.map { _1.split('.').last.gsub("\\", "") }
|
13
|
+
indexed_classes = Hash[classes.zip((0...classes.size).to_a)]
|
14
|
+
|
15
|
+
->(class_name) do
|
16
|
+
indexed_classes[class_name] || classes.index { _1.start_with?(class_name) } || -1
|
17
|
+
end
|
18
|
+
end
|
6
19
|
|
7
20
|
attr_reader :write, :filename, :read_stdin
|
8
21
|
|
@@ -41,6 +54,10 @@ class ERB::Formatter::CommandLine
|
|
41
54
|
@single_class_per_line = value
|
42
55
|
end
|
43
56
|
|
57
|
+
parser.on("--tailwind-output-path PATH", "Set the path to the tailwind output file") do |value|
|
58
|
+
@tailwind_output_path = value
|
59
|
+
end
|
60
|
+
|
44
61
|
parser.on("--[no-]debug", "Enable debug mode") do |value|
|
45
62
|
$DEBUG = value
|
46
63
|
end
|
@@ -61,10 +78,6 @@ class ERB::Formatter::CommandLine
|
|
61
78
|
@ignore_list ||= ERB::Formatter::IgnoreList.new
|
62
79
|
end
|
63
80
|
|
64
|
-
def ignore?(filename)
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
81
|
def run
|
69
82
|
if read_stdin
|
70
83
|
abort "Can't read both stdin and a list of files" unless @argv.empty?
|
@@ -77,11 +90,21 @@ class ERB::Formatter::CommandLine
|
|
77
90
|
end
|
78
91
|
end
|
79
92
|
|
93
|
+
if @tailwind_output_path
|
94
|
+
css_class_sorter = self.class.tailwindcss_class_sorter(@tailwind_output_path)
|
95
|
+
end
|
96
|
+
|
80
97
|
files.each do |(filename, code)|
|
81
98
|
if ignore_list.should_ignore_file? filename
|
82
99
|
print code unless write
|
83
100
|
else
|
84
|
-
html = ERB::Formatter.new(
|
101
|
+
html = ERB::Formatter.new(
|
102
|
+
code,
|
103
|
+
filename: filename,
|
104
|
+
line_width: @width || 80,
|
105
|
+
single_class_per_line: @single_class_per_line,
|
106
|
+
css_class_sorter: css_class_sorter
|
107
|
+
)
|
85
108
|
|
86
109
|
if write
|
87
110
|
File.write(filename, html)
|
data/lib/erb/formatter.rb
CHANGED
@@ -9,6 +9,7 @@ require 'securerandom'
|
|
9
9
|
require 'erb/formatter/version'
|
10
10
|
|
11
11
|
require 'syntax_tree'
|
12
|
+
require 'syntax_tree/plugin/trailing_comma'
|
12
13
|
|
13
14
|
class ERB::Formatter
|
14
15
|
module SyntaxTreeCommandPatch
|
@@ -47,10 +48,17 @@ class ERB::Formatter
|
|
47
48
|
|
48
49
|
SELF_CLOSING_TAG = /\A(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\z/i
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
begin
|
52
|
+
require 'prism' # ruby 3.3
|
53
|
+
RUBY_OPEN_BLOCK = Prism.method(:parse_failure?)
|
54
|
+
rescue LoadError
|
55
|
+
require 'ripper'
|
56
|
+
RUBY_OPEN_BLOCK = ->(code) do
|
57
|
+
# is nil when the parsing is broken, meaning it's an open expression
|
58
|
+
Ripper.sexp(code).nil?
|
59
|
+
end.freeze
|
60
|
+
end
|
61
|
+
|
54
62
|
RUBY_CLOSE_BLOCK = /\Aend\z/
|
55
63
|
RUBY_REOPEN_BLOCK = /\A(else|elsif\b(.*)|when\b(.*))\z/
|
56
64
|
|
@@ -67,7 +75,7 @@ class ERB::Formatter
|
|
67
75
|
new(source, filename: filename).html
|
68
76
|
end
|
69
77
|
|
70
|
-
def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, debug: $DEBUG)
|
78
|
+
def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, css_class_sorter: nil, debug: $DEBUG)
|
71
79
|
@original_source = source
|
72
80
|
@filename = filename || '(erb)'
|
73
81
|
@line_width = line_width
|
@@ -75,6 +83,7 @@ class ERB::Formatter
|
|
75
83
|
@html = +""
|
76
84
|
@debug = debug
|
77
85
|
@single_class_per_line = single_class_per_line
|
86
|
+
@css_class_sorter = css_class_sorter
|
78
87
|
|
79
88
|
html.extend DebugShovel if @debug
|
80
89
|
|
@@ -117,7 +126,9 @@ class ERB::Formatter
|
|
117
126
|
return "" if attrs.strip.empty?
|
118
127
|
|
119
128
|
plain_attrs = attrs.tr("\n", " ").squeeze(" ").gsub(erb_tags_regexp, erb_tags)
|
120
|
-
|
129
|
+
within_line_width = "<#{tag_name} #{plain_attrs}#{tag_closing}".size <= line_width
|
130
|
+
|
131
|
+
return " #{plain_attrs}" if within_line_width && !@css_class_sorter && !plain_attrs.match?(/ class=/)
|
121
132
|
|
122
133
|
attr_html = ""
|
123
134
|
tag_stack_push(['attr='], attrs)
|
@@ -132,8 +143,10 @@ class ERB::Formatter
|
|
132
143
|
end
|
133
144
|
|
134
145
|
value_parts = value[1...-1].strip.split(/\s+/)
|
146
|
+
value_parts.sort_by!(&@css_class_sorter) if name == 'class' && @css_class_sorter
|
135
147
|
|
136
|
-
full_attr =
|
148
|
+
full_attr = "#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}"
|
149
|
+
full_attr = within_line_width ? " #{full_attr}" : indented(full_attr)
|
137
150
|
|
138
151
|
if full_attr.size > line_width && MULTILINE_ATTR_NAMES.include?(name) && attr.match?(QUOTED_ATTR)
|
139
152
|
attr_html << indented("#{name}=#{value[0]}")
|
@@ -157,14 +170,14 @@ class ERB::Formatter
|
|
157
170
|
end
|
158
171
|
|
159
172
|
tag_stack_pop('attr"', value)
|
160
|
-
attr_html << indented(value[-1])
|
173
|
+
attr_html << (within_line_width ? value[-1] : indented(value[-1]))
|
161
174
|
else
|
162
175
|
attr_html << full_attr
|
163
176
|
end
|
164
177
|
end
|
165
178
|
|
166
179
|
tag_stack_pop(['attr='], attrs)
|
167
|
-
attr_html << indented("")
|
180
|
+
attr_html << indented("") unless within_line_width
|
168
181
|
attr_html
|
169
182
|
end
|
170
183
|
|
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
|
+
version: 0.7.0
|
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-12-
|
11
|
+
date: 2023-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: syntax_tree
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tailwindcss-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: m
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
27
55
|
description:
|
28
56
|
email:
|
29
57
|
- elia@schito.me
|
@@ -68,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
96
|
- !ruby/object:Gem::Version
|
69
97
|
version: '0'
|
70
98
|
requirements: []
|
71
|
-
rubygems_version: 3.3
|
99
|
+
rubygems_version: 3.5.3
|
72
100
|
signing_key:
|
73
101
|
specification_version: 4
|
74
102
|
summary: Format ERB files with speed and precision.
|