Class: ERB::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/erb/formatter/version.rb,
lib/erb/formatter.rb

Defined Under Namespace

Modules: DebugShovel, SyntaxTreeCommandPatch Classes: Error

Constant Summary collapse

VERSION =
"0.7.3"
SPACES =
/\s+/m
ATTR_NAME =
%r{[^\r\n\t\f\v= '"<>]*[^\r\n\t\f\v= '"<>/]}
UNQUOTED_VALUE =
%r{[^<>'"\s]+}
UNQUOTED_ATTR =
%r{#{ATTR_NAME}=#{UNQUOTED_VALUE}}
SINGLE_QUOTE_ATTR =
%r{(?:#{ATTR_NAME}='[^']*?')}m
DOUBLE_QUOTE_ATTR =
%r{(?:#{ATTR_NAME}="[^"]*?")}m
BAD_ATTR =
%r{#{ATTR_NAME}=\s+}
QUOTED_ATTR =
Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR)
ATTR =
Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR, UNQUOTED_ATTR, UNQUOTED_VALUE)
MULTILINE_ATTR_NAMES =
%w[class data-action]
ERB_TAG =
%r{(<%(?:==|=|-|))\s*(.*?)\s*(-?%>)}m
ERB_PLACEHOLDER =
%r{erb[a-z0-9]+tag}
TAG_NAME =
/[a-z0-9_:-]+/
TAG_NAME_ONLY =
/\A#{TAG_NAME}\z/
HTML_ATTR =
%r{\s+#{SINGLE_QUOTE_ATTR}|\s+#{DOUBLE_QUOTE_ATTR}|\s+#{UNQUOTED_ATTR}|\s+#{ATTR_NAME}}m
HTML_TAG_OPEN =
%r{<(#{TAG_NAME})((?:#{HTML_ATTR})*)(\s*?)(/>|>)}m
HTML_TAG_CLOSE =
%r{</\s*(#{TAG_NAME})\s*>}
SELF_CLOSING_TAG =
/\A(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\z/i
RUBY_STANDALONE_BLOCK =
/\A(yield|next)\b/
RUBY_CLOSE_BLOCK =
/\Aend\z/
RUBY_REOPEN_BLOCK =
/\A(else|elsif\b(.*)|when\b(.*))\z/
RUBOCOP_STDIN_MARKER =
"===================="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, line_width: 80, single_class_per_line: false, filename: nil, css_class_sorter: nil, debug: $DEBUG) ⇒ Formatter

Returns a new instance of Formatter.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/erb/formatter.rb', line 81

def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, css_class_sorter: 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
  @css_class_sorter = css_class_sorter

  html.extend DebugShovel if @debug

  @tag_stack = []
  @pre_pos = 0

  build_uid = -> { ['erb', SecureRandom.uuid, 'tag'].join.delete('-') }

  @pre_placeholders = {}
  @erb_tags = {}

  @source.gsub!(ERB_PLACEHOLDER) { |tag| build_uid[].tap { |uid| pre_placeholders[uid] = tag } }
  @source.gsub!(ERB_TAG) { |tag| build_uid[].tap { |uid| erb_tags[uid] = tag } }

  @erb_tags_regexp = /(#{Regexp.union(erb_tags.keys)})/
  @pre_placeholders_regexp = /(#{Regexp.union(pre_placeholders.keys)})/
  @tags_regexp = Regexp.union(HTML_TAG_CLOSE, HTML_TAG_OPEN)

  format
  freeze
end

Instance Attribute Details

#erb_tagsObject

Returns the value of attribute erb_tags.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def erb_tags
  @erb_tags
end

#erb_tags_regexpObject

Returns the value of attribute erb_tags_regexp.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def erb_tags_regexp
  @erb_tags_regexp
end

#htmlObject Also known as: to_s

Returns the value of attribute html.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def html
  @html
end

#line_widthObject

Returns the value of attribute line_width.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def line_width
  @line_width
end

#pre_placeholdersObject

Returns the value of attribute pre_placeholders.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def pre_placeholders
  @pre_placeholders
end

#pre_placeholders_regexpObject

Returns the value of attribute pre_placeholders_regexp.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def pre_placeholders_regexp
  @pre_placeholders_regexp
end

#pre_posObject

Returns the value of attribute pre_pos.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def pre_pos
  @pre_pos
end

#sourceObject

Returns the value of attribute source.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def source
  @source
end

#tag_stackObject

Returns the value of attribute tag_stack.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def tag_stack
  @tag_stack
end

#tags_regexpObject

Returns the value of attribute tags_regexp.



122
123
124
# File 'lib/erb/formatter.rb', line 122

def tags_regexp
  @tags_regexp
end

Class Method Details

.format(source, filename: nil) ⇒ Object



77
78
79
# File 'lib/erb/formatter.rb', line 77

def self.format(source, filename: nil)
  new(source, filename: filename).html
end

Instance Method Details

#formatObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/erb/formatter.rb', line 341

def format
  scanner = StringScanner.new(source)

  until scanner.eos?
    if matched = scanner.scan_until(tags_regexp)
      p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
      pre_match = scanner.pre_match[pre_pos..]
      p POS: pre_pos...scanner.pos, advanced: source[pre_pos...scanner.pos] if @debug
      p MATCHED: matched if @debug
      self.pre_pos = scanner.charpos

      # Don't accept `name= "value"` attributes
      raise "Bad attribute, please fix spaces after the equal sign:\n#{pre_match}" if BAD_ATTR.match? pre_match

      format_erb_tags(pre_match) if pre_match

      if matched.match?(HTML_TAG_CLOSE)
        tag_name = scanner.captures.first

        full_tag = "</#{tag_name}>"
        tag_stack_pop(tag_name, full_tag)
        html << (scanner.pre_match.match?(/\s+\z/) ? indented(full_tag) : full_tag)

      elsif matched.match(HTML_TAG_OPEN)
        _, tag_name, tag_attrs, _, tag_closing = *scanner.captures

        raise "Unknown tag #{tag_name.inspect}" unless tag_name.match?(TAG_NAME_ONLY)

        tag_self_closing = tag_closing == '/>' || SELF_CLOSING_TAG.match?(tag_name)
        tag_attrs.strip!
        formatted_tag_name = format_attributes(tag_name, tag_attrs.strip, tag_closing).gsub(erb_tags_regexp, erb_tags)
        full_tag = "<#{tag_name}#{formatted_tag_name}#{tag_closing}"
        html << (scanner.pre_match.match?(/\s+\z/) ? indented(full_tag) : full_tag)

        tag_stack_push(tag_name, full_tag) unless tag_self_closing
      else
        raise "Unrecognized content: #{matched.inspect}"
      end
    else
      p format_rest: scanner.rest if @debug
      format_erb_tags(scanner.rest.to_s)
      scanner.terminate
    end
  end

  html.gsub!(erb_tags_regexp, erb_tags)
  html.gsub!(pre_placeholders_regexp, pre_placeholders)
  html.strip!
  html.prepend @front_matter + "\n" if @front_matter
  html << "\n"
end

#format_attributes(tag_name, attrs, tag_closing) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/erb/formatter.rb', line 128

def format_attributes(tag_name, attrs, tag_closing)
  return "" if attrs.strip.empty?

  plain_attrs = attrs.tr("\n", " ").squeeze(" ").gsub(erb_tags_regexp, erb_tags)
  within_line_width = "<#{tag_name} #{plain_attrs}#{tag_closing}".size <= line_width

  return " #{plain_attrs}" if within_line_width && !@css_class_sorter && !plain_attrs.match?(/ class=/)

  attr_html = ""
  tag_stack_push(['attr='], attrs)

  attrs.scan(ATTR).flatten.each do |attr|
    attr.strip!
    name, value = attr.split('=', 2)

    if value.nil?
      attr_html << indented("#{name}")
      next
    end

    if /\A#{UNQUOTED_VALUE}\z/o.match?(value)
      attr_html << indented("#{name}=\"#{value}\"")
      next
    end

    value_parts = value[1...-1].strip.split(SPACES)
    value_parts.sort_by!(&@css_class_sorter) if name == 'class' && @css_class_sorter

    full_attr = "#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}"
    full_attr = within_line_width ? " #{full_attr}" : indented(full_attr)

    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)

      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 << (within_line_width ? value[-1] : indented(value[-1]))
    else
      attr_html << full_attr
    end
  end

  tag_stack_pop(['attr='], attrs)
  attr_html << indented("") unless within_line_width
  attr_html
end

#format_erb_tags(string) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/erb/formatter.rb', line 286

def format_erb_tags(string)
  p format_erb_tags: string if @debug
  if %w[style script].include?(tag_stack.last&.first)
    html << string.rstrip
    return
  end

  erb_scanner = StringScanner.new(string.to_s)
  erb_pre_pos = 0
  until erb_scanner.eos?
    if erb_scanner.scan_until(erb_tags_regexp)
      p PRE_MATCH: [erb_pre_pos, '..', erb_scanner.pre_match] if @debug
      erb_pre_match = erb_scanner.pre_match
      erb_pre_match = erb_pre_match[erb_pre_pos..].to_s
      erb_pre_pos = erb_scanner.pos

      erb_code = erb_tags[erb_scanner.captures.first]

      format_text(erb_pre_match)

      erb_open, ruby_code, erb_close = ERB_TAG.match(erb_code).captures
      erb_open << ' ' unless ruby_code.start_with?('#')

      case ruby_code
      when RUBY_STANDALONE_BLOCK
        ruby_code = format_ruby(ruby_code, autoclose: false)
        full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
        html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
      when RUBY_CLOSE_BLOCK
        full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
        tag_stack_pop('%erb%', ruby_code)
        html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
      when RUBY_REOPEN_BLOCK
        full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
        tag_stack_pop('%erb%', ruby_code)
        html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
        tag_stack_push('%erb%', ruby_code)
      when RUBY_OPEN_BLOCK
        full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
        html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
        tag_stack_push('%erb%', ruby_code)
      else
        ruby_code = format_ruby(ruby_code, autoclose: false)
        full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
        html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
      end
    else
      p ERB_REST: erb_scanner.rest if @debug
      rest = erb_scanner.rest.to_s
      format_text(rest)
      erb_scanner.terminate
    end
  end
end

#format_ruby(code, autoclose: false) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/erb/formatter.rb', line 263

def format_ruby(code, autoclose: false)
  if autoclose
    code += "\nend" unless RUBY_OPEN_BLOCK["#{code}\nend"]
    code += "\n}" unless RUBY_OPEN_BLOCK["#{code}\n}"]
  end
  p RUBY_IN_: code if @debug

  SyntaxTree::Command.prepend SyntaxTreeCommandPatch

  code = begin
    SyntaxTree.format(code, @line_width)
  rescue SyntaxTree::Parser::ParseError => error
    p RUBY_PARSE_ERROR: error if @debug
    code
  end

  lines = code.strip.lines
  lines = lines[0...-1] if autoclose
  code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join.strip
  p RUBY_OUT: code if @debug
  code
end

#format_text(text) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/erb/formatter.rb', line 227

def format_text(text)
  p format_text: text if @debug
  return unless text

  starting_space = text.match?(/\A\s/)

  final_newlines_count = text.match(/(\s*)\z/m).captures.last.count("\n")
  html << "\n" if final_newlines_count > 1

  return if text.match?(/\A\s*\z/m) # empty

  text = text.gsub(SPACES, ' ').strip

  offset = indented("").size
  # Restore full line width if there are less than 40 columns available
  offset = 0 if (line_width - offset) <= 40
  available_width = line_width - offset

  lines = []

  until text.empty?
    if text.size >= available_width
      last_space_index = text[0..available_width].rindex(' ')
      lines << text.slice!(0..last_space_index)
    else
      lines << text.slice!(0..-1)
    end
    offset = 0
  end
  p lines: lines if @debug
  html << lines.shift.strip unless starting_space
  lines.each do |line|
    html << indented(line)
  end
end

#indented(string, strip: true) ⇒ Object



221
222
223
224
225
# File 'lib/erb/formatter.rb', line 221

def indented(string, strip: true)
  string = string.strip if strip
  indent = "  " * tag_stack.size
  "\n#{indent}#{string}"
end

#raise(message) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/erb/formatter.rb', line 206

def raise(message)
  line = @original_source[0..pre_pos].count("\n")
  location = "#{@filename}:#{line}:in `#{tag_stack.last&.first}'"
  error = RuntimeError.new([
    nil,
    "==> FORMATTED:",
    html,
    "==> STACK:",
    tag_stack.pretty_inspect,
    "==> ERROR: #{message}",
  ].join("\n"))
  error.set_backtrace caller.to_a + [location]
  super error
end

#remove_front_matter(source) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/erb/formatter.rb', line 112

def remove_front_matter(source)
  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

#tag_stack_pop(tag_name, code) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/erb/formatter.rb', line 197

def tag_stack_pop(tag_name, code)
  if tag_name == tag_stack.last&.first
    tag_stack.pop
    p POP_: tag_stack if @debug
  else
    raise "Unmatched close tag, tried with #{[tag_name, code]}, but #{tag_stack.last} was on the stack"
  end
end

#tag_stack_push(tag_name, code) ⇒ Object



192
193
194
195
# File 'lib/erb/formatter.rb', line 192

def tag_stack_push(tag_name, code)
  tag_stack << [tag_name, code]
  p PUSH: tag_stack if @debug
end