diff --git a/lib/erb/formatter.rb b/lib/erb/formatter.rb index be01425..9d766cc 100644 --- a/lib/erb/formatter.rb +++ b/lib/erb/formatter.rb @@ -56,13 +56,6 @@ class Error < StandardError; end RUBOCOP_STDIN_MARKER = "====================" - # Override the max line length to account from already indented ERB - module RubocopForcedMaxLineLength - def max - Thread.current['RuboCop::Cop::Layout::LineLength#max'] || super - end - end - module DebugShovel def <<(string) puts "ADDING: #{string.inspect} FROM:\n #{caller(1, 5).join("\n ")}" @@ -74,13 +67,14 @@ def self.format(source, filename: nil) new(source, filename: filename).html end - def initialize(source, line_width: 80, filename: nil, debug: $DEBUG) + def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, debug: $DEBUG) @original_source = source @filename = filename || '(erb)' @line_width = line_width @source = remove_front_matter source.dup @html = +"" @debug = debug + @single_class_per_line = single_class_per_line html.extend DebugShovel if @debug @@ -127,6 +121,7 @@ def format_attributes(tag_name, attrs, tag_closing) attr_html = "" tag_stack_push(['attr='], attrs) + attrs.scan(ATTR).flatten.each do |attr| attr.strip! full_attr = indented(attr) @@ -135,15 +130,33 @@ def format_attributes(tag_name, attrs, tag_closing) if full_attr.size > line_width && MULTILINE_ATTR_NAMES.include?(name) && attr.match?(QUOTED_ATTR) attr_html << indented("#{name}=#{value[0]}") tag_stack_push('attr"', value) - value[1...-1].strip.split(/\s+/).each do |value_part| - attr_html << indented(value_part) + + value_parts = value[1...-1].strip.split(/\s+/) + + if !@single_class_per_line && name == 'class' + line = value_parts.shift + value_parts.each do |value_part| + if (line.size + value_part.size + 1) <= line_width + line << " #{value_part}" + else + attr_html << indented(line) + line = value_part + end + end + attr_html << indented(line) if line + else + value_parts.each do |value_part| + attr_html << indented(value_part) + end end + tag_stack_pop('attr"', value) attr_html << indented(value[-1]) else attr_html << full_attr end end + tag_stack_pop(['attr='], attrs) attr_html << indented("") attr_html diff --git a/lib/erb/formatter/command_line.rb b/lib/erb/formatter/command_line.rb index 51842d8..e6bace7 100644 --- a/lib/erb/formatter/command_line.rb +++ b/lib/erb/formatter/command_line.rb @@ -10,7 +10,7 @@ def initialize(argv, stdin: $stdin) @argv = argv.dup @stdin = stdin - @write, @filename, @read_stdin, @code = nil + @write, @filename, @read_stdin, @code, @single_class_per_line = nil OptionParser.new do |parser| parser.banner = "Usage: #{$0} FILENAME... --write" @@ -37,6 +37,10 @@ def initialize(argv, stdin: $stdin) @width = value end + parser.on("--single-class-per-line", "Print each class on a separate line") do |value| + @single_class_per_line = value + end + parser.on("--[no-]debug", "Enable debug mode") do |value| $DEBUG = value end @@ -77,7 +81,7 @@ def run if ignore_list.should_ignore_file? filename print code unless write else - html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80) + html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80, single_class_per_line: @single_class_per_line) if write File.write(filename, html) diff --git a/test/fixtures/multiline_attributes.html.erb b/test/fixtures/multiline_attributes.html.erb new file mode 100644 index 0000000..2056283 --- /dev/null +++ b/test/fixtures/multiline_attributes.html.erb @@ -0,0 +1,3 @@ + diff --git a/test/fixtures/multiline_attributes.html.expected.erb b/test/fixtures/multiline_attributes.html.expected.erb new file mode 100644 index 0000000..594fc8f --- /dev/null +++ b/test/fixtures/multiline_attributes.html.expected.erb @@ -0,0 +1,9 @@ +