Module: Rack::WebProfiler::View::Helpers::Common

Included in:
Collector::View::Context, Context
Defined in:
lib/rack/web_profiler/view.rb

Overview

Common helpers.

Instance Method Summary collapse

Instance Method Details

#capture { ... } ⇒ Object

Yields:



188
189
190
191
192
193
194
195
196
197
# File 'lib/rack/web_profiler/view.rb', line 188

def capture(&block)
  @capture = nil
  buf_was  =  @_erbout
  @_erbout = ""

  result = yield

  @_erbout = buf_was
  result.strip.empty? && @capture ? @capture : result
end

#capture_later { ... } ⇒ Object

Yields:



202
203
204
# File 'lib/rack/web_profiler/view.rb', line 202

def capture_later(&block)
  proc { |*| @capture = capture(&block) }
end

#content_for(key, content = nil, &block) ⇒ Object



116
117
118
119
# File 'lib/rack/web_profiler/view.rb', line 116

def content_for(key, content = nil, &block)
  block ||= proc { |*| content }
  content_blocks[key.to_sym] << capture_later(&block)
end

#content_for?(key) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rack/web_profiler/view.rb', line 121

def content_for?(key)
  content_blocks[key.to_sym].any?
end

#h(obj) ⇒ String

Escape html.

Parameters:

  • obj

Returns:

  • (String)


151
152
153
154
155
156
157
158
# File 'lib/rack/web_profiler/view.rb', line 151

def h(obj)
  case obj
  when String
    ::ERB::Util.html_escape(obj)
  else
    ::ERB::Util.html_escape(obj.inspect)
  end
end

#highlight(code: "", mimetype: nil, language: nil, formatter_opts: {}) { ... } ⇒ String

Highlight text.

Parameters:

  • code (Hash) (defaults to: "")

    a customizable set of options

  • mimetype (Hash) (defaults to: nil)

    a customizable set of options

  • language (Hash) (defaults to: nil)

    a customizable set of options

  • formatter_opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (code:):

  • (String)

Options Hash (mimetype:):

  • (String, nil)

Options Hash (language:):

  • (String, nil)

Options Hash (formatter_opts:):

  • (Hash)

Yields:

  • code.

Returns:

  • (String)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rack/web_profiler/view.rb', line 170

def highlight(code: "", mimetype: nil, language: nil, formatter_opts: {})
  language = language.to_s if language.is_a? Symbol

  lexer = ::Rouge::Lexer.guess(mimetype: mimetype) if mimetype.is_a? String
  lexer = ::Rouge::Lexer.find_fancy(language)      if language.is_a? String
  lexer ||= ::Rouge::Lexers::PlainText.new

  code = capture(&Proc.new) if block_given?
  code ||= ""

  formatter = WebProfiler::Rouge::HTMLFormatter.new(formatter_opts)

  "<div class=\"highlight\">#{formatter.format(lexer.lex(code))}</div>"
end

#partial(path, variables: nil) ⇒ String

Render a partial view.

Parameters:

  • path (String)

    path to partial

  • variables (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (variables:):

  • variables (Hash, nil)

    for partial

Returns:

  • (String)


136
137
138
139
140
141
142
143
144
# File 'lib/rack/web_profiler/view.rb', line 136

def partial(path, variables: nil)
  return "" if path.nil?

  variables ||= binding if variables.nil?

  capture do
    WebProfiler::View.new(path, context: self).result(variables)
  end
end

#yield_content(key, default = nil) ⇒ Object



125
126
127
128
# File 'lib/rack/web_profiler/view.rb', line 125

def yield_content(key, default = nil)
  return default if content_blocks[key.to_sym].empty?
  content_blocks[key.to_sym].map { |b| capture(&b) }.join
end