Skip to content

Commit 4edff99

Browse files
authored
Merge pull request #33 from nebulab/elia+azeem/single-class-per-line
Break class attributes at line width instead of using one class per line
2 parents abb40fc + ca0c16e commit 4edff99

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

lib/erb/formatter.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ class Error < StandardError; end
5656

5757
RUBOCOP_STDIN_MARKER = "===================="
5858

59-
# Override the max line length to account from already indented ERB
60-
module RubocopForcedMaxLineLength
61-
def max
62-
Thread.current['RuboCop::Cop::Layout::LineLength#max'] || super
63-
end
64-
end
65-
6659
module DebugShovel
6760
def <<(string)
6861
puts "ADDING: #{string.inspect} FROM:\n #{caller(1, 5).join("\n ")}"
@@ -74,13 +67,14 @@ def self.format(source, filename: nil)
7467
new(source, filename: filename).html
7568
end
7669

77-
def initialize(source, line_width: 80, filename: nil, debug: $DEBUG)
70+
def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, debug: $DEBUG)
7871
@original_source = source
7972
@filename = filename || '(erb)'
8073
@line_width = line_width
8174
@source = remove_front_matter source.dup
8275
@html = +""
8376
@debug = debug
77+
@single_class_per_line = single_class_per_line
8478

8579
html.extend DebugShovel if @debug
8680

@@ -127,6 +121,7 @@ def format_attributes(tag_name, attrs, tag_closing)
127121

128122
attr_html = ""
129123
tag_stack_push(['attr='], attrs)
124+
130125
attrs.scan(ATTR).flatten.each do |attr|
131126
attr.strip!
132127
full_attr = indented(attr)
@@ -135,15 +130,33 @@ def format_attributes(tag_name, attrs, tag_closing)
135130
if full_attr.size > line_width && MULTILINE_ATTR_NAMES.include?(name) && attr.match?(QUOTED_ATTR)
136131
attr_html << indented("#{name}=#{value[0]}")
137132
tag_stack_push('attr"', value)
138-
value[1...-1].strip.split(/\s+/).each do |value_part|
139-
attr_html << indented(value_part)
133+
134+
value_parts = value[1...-1].strip.split(/\s+/)
135+
136+
if !@single_class_per_line && name == 'class'
137+
line = value_parts.shift
138+
value_parts.each do |value_part|
139+
if (line.size + value_part.size + 1) <= line_width
140+
line << " #{value_part}"
141+
else
142+
attr_html << indented(line)
143+
line = value_part
144+
end
145+
end
146+
attr_html << indented(line) if line
147+
else
148+
value_parts.each do |value_part|
149+
attr_html << indented(value_part)
150+
end
140151
end
152+
141153
tag_stack_pop('attr"', value)
142154
attr_html << indented(value[-1])
143155
else
144156
attr_html << full_attr
145157
end
146158
end
159+
147160
tag_stack_pop(['attr='], attrs)
148161
attr_html << indented("")
149162
attr_html

lib/erb/formatter/command_line.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(argv, stdin: $stdin)
1010
@argv = argv.dup
1111
@stdin = stdin
1212

13-
@write, @filename, @read_stdin, @code = nil
13+
@write, @filename, @read_stdin, @code, @single_class_per_line = nil
1414

1515
OptionParser.new do |parser|
1616
parser.banner = "Usage: #{$0} FILENAME... --write"
@@ -37,6 +37,10 @@ def initialize(argv, stdin: $stdin)
3737
@width = value
3838
end
3939

40+
parser.on("--single-class-per-line", "Print each class on a separate line") do |value|
41+
@single_class_per_line = value
42+
end
43+
4044
parser.on("--[no-]debug", "Enable debug mode") do |value|
4145
$DEBUG = value
4246
end
@@ -77,7 +81,7 @@ def run
7781
if ignore_list.should_ignore_file? filename
7882
print code unless write
7983
else
80-
html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80)
84+
html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80, single_class_per_line: @single_class_per_line)
8185

8286
if write
8387
File.write(filename, html)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<button class="text-gray-700 bg-transparent hover:bg-gray-50 active:bg-gray-100 focus:bg-gray-50 focus:ring-gray-300 focus:ring-2
2+
disabled:text-gray-300 disabled:bg-transparent disabled:border-gray-300 disabled:cursor-not-allowed
3+
aria-disabled:text-gray-300 aria-disabled:bg-transparent aria-disabled:border-gray-300 aria-disabled:cursor-not-allowed">Button</button>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<button
2+
class="
3+
text-gray-700 bg-transparent hover:bg-gray-50 active:bg-gray-100
4+
focus:bg-gray-50 focus:ring-gray-300 focus:ring-2 disabled:text-gray-300
5+
disabled:bg-transparent disabled:border-gray-300 disabled:cursor-not-allowed
6+
aria-disabled:text-gray-300 aria-disabled:bg-transparent
7+
aria-disabled:border-gray-300 aria-disabled:cursor-not-allowed
8+
"
9+
>Button</button>

0 commit comments

Comments
 (0)