lib/erb/formatter.rb in erb-formatter-0.4.3 vs lib/erb/formatter.rb in erb-formatter-0.5.0

- old
+ new

@@ -1,13 +1,13 @@ # frozen_string_literal: false require 'pp' -require "erb" -require "ripper" -require 'securerandom' +require 'erb' +require 'yaml' require 'strscan' require 'stringio' +require 'securerandom' require 'erb/formatter/version' require 'syntax_tree' class ERB::Formatter @@ -54,17 +54,10 @@ RUBY_CLOSE_BLOCK = /\Aend\z/ RUBY_REOPEN_BLOCK = /\A(else|elsif\b(.*)|when\b(.*))\z/ 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 ")}" super end @@ -72,17 +65,18 @@ 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 @tag_stack = [] @pre_pos = 0 @@ -102,14 +96,17 @@ format freeze end def remove_front_matter(source) - source.sub(/\A---\n[\s\S]*?\n---\n/) do |match| - @front_matter = match - match.gsub(/[^\n]/, ' ') - end + return source unless source.start_with?("---\n") + + first_body_line = YAML.parse(source).children.first.end_line + 1 + lines = source.lines + + @front_matter = lines[0...first_body_line].join + lines[first_body_line..].join end attr_accessor \ :source, :html, :tag_stack, :pre_pos, :pre_placeholders, :erb_tags, :erb_tags_regexp, :pre_placeholders_regexp, :tags_regexp, :line_width @@ -122,26 +119,45 @@ plain_attrs = attrs.tr("\n", " ").squeeze(" ").gsub(erb_tags_regexp, erb_tags) return " #{plain_attrs}" if "<#{tag_name} #{plain_attrs}#{tag_closing}".size <= line_width attr_html = "" tag_stack_push(['attr='], attrs) + attrs.scan(ATTR).flatten.each do |attr| attr.strip! full_attr = indented(attr) name, value = attr.split('=', 2) 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 end