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

LOGO_IMAGE_PATH =
File.expand_path('../../../assets/logo.png', __dir__)

Constants included from PathUtil

PathUtil::HIDDEN_FILE_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TextUtil

pluralize

Methods included from PathUtil

absolute?, glob?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

Constructor Details

#initialize(files, summary) ⇒ ERBContext

Returns a new instance of ERBContext.



69
70
71
72
# File 'lib/rubocop/formatter/html_formatter.rb', line 69

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

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



67
68
69
# File 'lib/rubocop/formatter/html_formatter.rb', line 67

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary.



67
68
69
# File 'lib/rubocop/formatter/html_formatter.rb', line 67

def summary
  @summary
end

Instance Method Details

#base64_encoded_logo_imageObject



116
117
118
119
120
121
122
# File 'lib/rubocop/formatter/html_formatter.rb', line 116

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

  # `Base64.encode64` compatible:
  # https://github.com/ruby/base64/blob/v0.1.1/lib/base64.rb#L27-L40
  [image].pack('m')
end

#bindingObject

Make Kernel#binding public. rubocop:disable Lint/UselessMethodDefinition



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

def binding
  super
end

#decorated_message(offense) ⇒ Object

rubocop:enable Lint/UselessMethodDefinition



81
82
83
# File 'lib/rubocop/formatter/html_formatter.rb', line 81

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

#escape(string) ⇒ Object



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

def escape(string)
  CGI.escapeHTML(string)
end

#highlight_source_tag(offense) ⇒ Object



92
93
94
95
96
# File 'lib/rubocop/formatter/html_formatter.rb', line 92

def highlight_source_tag(offense)
  "<span class=\"highlight #{offense.severity}\">" \
    "#{escape(offense.highlighted_area.source)}" \
    '</span>'
end

#highlighted_source_line(offense) ⇒ Object



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

def highlighted_source_line(offense)
  source_before_highlight(offense) +
    highlight_source_tag(offense) +
    source_after_highlight(offense) +
    possible_ellipses(offense.location)
end

#possible_ellipses(location) ⇒ Object



108
109
110
# File 'lib/rubocop/formatter/html_formatter.rb', line 108

def possible_ellipses(location)
  location.single_line? ? '' : " #{ELLIPSES}"
end

#render_cssObject



124
125
126
127
128
129
130
131
# File 'lib/rubocop/formatter/html_formatter.rb', line 124

def render_css
  context = CSSContext.new
  template = File.read(CSS_PATH, encoding: Encoding::UTF_8)
  erb = ERB.new(template, trim_mode: '-')
  erb.result(context.binding).lines.map do |line|
    line == "\n" ? line : "      #{line}"
  end.join
end

#source_after_highlight(offense) ⇒ Object



103
104
105
106
# File 'lib/rubocop/formatter/html_formatter.rb', line 103

def source_after_highlight(offense)
  source_line = offense.location.source_line
  escape(source_line[offense.highlighted_area.end_pos..])
end

#source_before_highlight(offense) ⇒ Object



98
99
100
101
# File 'lib/rubocop/formatter/html_formatter.rb', line 98

def source_before_highlight(offense)
  source_line = offense.location.source_line
  escape(source_line[0...offense.highlighted_area.begin_pos])
end