Class: RuboCop::Formatter::HTMLFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/rubocop/formatter/html_formatter.rb

Overview

This formatter saves the output as an html file.

Defined Under Namespace

Classes: CSSContext, Color, ERBContext, FileOffenses, Summary

Constant Summary collapse

ELLIPSES =
'<span class="extra-code">...</span>'
TEMPLATE_PATH =
File.expand_path('../../../assets/output.html.erb', __dir__)
CSS_PATH =
File.expand_path('../../../assets/output.css.erb', __dir__)

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#options, #output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started

Constructor Details

#initialize(output, options = {}) ⇒ HTMLFormatter

Returns a new instance of HTMLFormatter.



29
30
31
32
33
# File 'lib/rubocop/formatter/html_formatter.rb', line 29

def initialize(output, options = {})
  super
  @files = []
  @summary = Summary.new(offense_count: 0)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



27
28
29
# File 'lib/rubocop/formatter/html_formatter.rb', line 27

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary.



27
28
29
# File 'lib/rubocop/formatter/html_formatter.rb', line 27

def summary
  @summary
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



39
40
41
42
# File 'lib/rubocop/formatter/html_formatter.rb', line 39

def file_finished(file, offenses)
  files << FileOffenses.new(path: file, offenses: offenses)
  summary.offense_count += offenses.count
end

#finished(inspected_files) ⇒ Object



44
45
46
47
48
# File 'lib/rubocop/formatter/html_formatter.rb', line 44

def finished(inspected_files)
  summary.inspected_files = inspected_files

  render_html
end

#render_htmlObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/formatter/html_formatter.rb', line 50

def render_html
  context = ERBContext.new(files, summary)

  template = File.read(TEMPLATE_PATH, encoding: Encoding::UTF_8)
  erb = ERB.new(template)
  html = erb.result(context.binding).lines.map do |line|
    line.match?(/\A\s*\z/) ? "\n" : line
  end.join

  output.write html
end

#started(target_files) ⇒ Object



35
36
37
# File 'lib/rubocop/formatter/html_formatter.rb', line 35

def started(target_files)
  summary.target_files = target_files
end