erb-formatter 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +25 -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: a21bdecce12179abd027c94e06f28e708323ef5217e1a45c43302e72c9334d9c
|
4
|
+
data.tar.gz: c4f1dc0e38523170c62a2e4c474140da3b988741a0fe8f983b766738730b63eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1377c919438c3db88d2398d9eef2e259c4fd36e9ad0299e1d5e0892a387389468ea8a7ba083a9575ef5b3814330f08bb549f2fe2e36b61266a58a1b63524b1d
|
7
|
+
data.tar.gz: 9976eaea39da84cfca194851430428224c393bc8cd1e39ebe2b8833b7c57832911b6057415201d00bd00bd5956a5be7d6c197464233b80bc83e92482e0d0db2a
|
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
@@ -48,10 +48,17 @@ class ERB::Formatter
|
|
48
48
|
|
49
49
|
SELF_CLOSING_TAG = /\A(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\z/i
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
+
|
55
62
|
RUBY_CLOSE_BLOCK = /\Aend\z/
|
56
63
|
RUBY_REOPEN_BLOCK = /\A(else|elsif\b(.*)|when\b(.*))\z/
|
57
64
|
|
@@ -68,7 +75,7 @@ class ERB::Formatter
|
|
68
75
|
new(source, filename: filename).html
|
69
76
|
end
|
70
77
|
|
71
|
-
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)
|
72
79
|
@original_source = source
|
73
80
|
@filename = filename || '(erb)'
|
74
81
|
@line_width = line_width
|
@@ -76,6 +83,7 @@ class ERB::Formatter
|
|
76
83
|
@html = +""
|
77
84
|
@debug = debug
|
78
85
|
@single_class_per_line = single_class_per_line
|
86
|
+
@css_class_sorter = css_class_sorter
|
79
87
|
|
80
88
|
html.extend DebugShovel if @debug
|
81
89
|
|
@@ -118,7 +126,9 @@ class ERB::Formatter
|
|
118
126
|
return "" if attrs.strip.empty?
|
119
127
|
|
120
128
|
plain_attrs = attrs.tr("\n", " ").squeeze(" ").gsub(erb_tags_regexp, erb_tags)
|
121
|
-
|
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=/)
|
122
132
|
|
123
133
|
attr_html = ""
|
124
134
|
tag_stack_push(['attr='], attrs)
|
@@ -126,6 +136,10 @@ class ERB::Formatter
|
|
126
136
|
attrs.scan(ATTR).flatten.each do |attr|
|
127
137
|
attr.strip!
|
128
138
|
name, value = attr.split('=', 2)
|
139
|
+
if UNQUOTED_ATTR =~ attr
|
140
|
+
attr_html << indented("#{name}=\"#{value}\"")
|
141
|
+
next
|
142
|
+
end
|
129
143
|
|
130
144
|
if value.nil?
|
131
145
|
attr_html << indented("#{name}")
|
@@ -133,8 +147,10 @@ class ERB::Formatter
|
|
133
147
|
end
|
134
148
|
|
135
149
|
value_parts = value[1...-1].strip.split(/\s+/)
|
150
|
+
value_parts.sort_by!(&@css_class_sorter) if name == 'class' && @css_class_sorter
|
136
151
|
|
137
|
-
full_attr =
|
152
|
+
full_attr = "#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}"
|
153
|
+
full_attr = within_line_width ? " #{full_attr}" : indented(full_attr)
|
138
154
|
|
139
155
|
if full_attr.size > line_width && MULTILINE_ATTR_NAMES.include?(name) && attr.match?(QUOTED_ATTR)
|
140
156
|
attr_html << indented("#{name}=#{value[0]}")
|
@@ -158,14 +174,14 @@ class ERB::Formatter
|
|
158
174
|
end
|
159
175
|
|
160
176
|
tag_stack_pop('attr"', value)
|
161
|
-
attr_html << indented(value[-1])
|
177
|
+
attr_html << (within_line_width ? value[-1] : indented(value[-1]))
|
162
178
|
else
|
163
179
|
attr_html << full_attr
|
164
180
|
end
|
165
181
|
end
|
166
182
|
|
167
183
|
tag_stack_pop(['attr='], attrs)
|
168
|
-
attr_html << indented("")
|
184
|
+
attr_html << indented("") unless within_line_width
|
169
185
|
attr_html
|
170
186
|
end
|
171
187
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Elia Schito
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-10 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.
|