Class: RuboCop::Formatter::HTMLFormatter::ERBContext

Inherits:
Object
  • Object
show all
Includes:
TextUtil, PathUtil
Defined in:
lib/rubocop/formatter/html_formatter.rb

Overview

This class provides helper methods used in the ERB template.

Constant Summary collapse

SEVERITY_COLORS =
{
  refactor:   Color.new(0xED, 0x9C, 0x28, 1.0),
  convention: Color.new(0xED, 0x9C, 0x28, 1.0),
  warning:    Color.new(0x96, 0x28, 0xEF, 1.0),
  error:      Color.new(0xD2, 0x32, 0x2D, 1.0),
  fatal:      Color.new(0xD2, 0x32, 0x2D, 1.0)
}
LOGO_IMAGE_PATH =
File.expand_path('../../../../assets/logo.png', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PathUtil

issue_deprecation_warning, match_path?, relative_path

Methods included from TextUtil

pluralize

Constructor Details

#initialize(files, summary) ⇒ ERBContext

Returns a new instance of ERBContext.



77
78
79
80
# File 'lib/rubocop/formatter/html_formatter.rb', line 77

def initialize(files, summary)
  @files = files.sort_by(&:path)
  @summary = summary
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



75
76
77
# File 'lib/rubocop/formatter/html_formatter.rb', line 75

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary.



75
76
77
# File 'lib/rubocop/formatter/html_formatter.rb', line 75

def summary
  @summary
end

Instance Method Details

#base64_encoded_logo_imageObject



111
112
113
114
# File 'lib/rubocop/formatter/html_formatter.rb', line 111

def base64_encoded_logo_image
  image = File.read(LOGO_IMAGE_PATH, binmode: true)
  Base64.encode64(image)
end

#bindingObject

Make Kernel#binding public.



83
84
85
# File 'lib/rubocop/formatter/html_formatter.rb', line 83

def binding
  super
end

#decorated_message(offense) ⇒ Object



87
88
89
90
91
# File 'lib/rubocop/formatter/html_formatter.rb', line 87

def decorated_message(offense)
  offense.message.gsub(/`(.+?)`/) do
    "<code>#{Regexp.last_match(1)}</code>"
  end
end

#highlighted_source_line(offense) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubocop/formatter/html_formatter.rb', line 93

def highlighted_source_line(offense)
  location = offense.location

  column_range = if location.begin.line == location.end.line
                   location.column_range
                 else
                   location.column...location.source_line.length
                 end

  source_line = location.source_line

  source_line[0...column_range.begin] +
    "<span class=\"highlight #{offense.severity}\">" +
    source_line[column_range] +
    '</span>' +
    source_line[column_range.end..-1]
end