module Rack::WebProfiler::View::Helpers::Common

Common helpers.

Public Instance Methods

capture() { || ... } click to toggle source

@yield

# 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(&block) click to toggle source

@yield

# File lib/rack/web_profiler/view.rb, line 202
def capture_later(&block)
  proc { |*| @capture = capture(&block) }
end
content_for(key, content = nil, &block) click to toggle source
# 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) click to toggle source
# File lib/rack/web_profiler/view.rb, line 121
def content_for?(key)
  content_blocks[key.to_sym].any?
end
h(obj) click to toggle source

Escape html.

@param obj

@return [String]

# 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: {}) click to toggle source

Highlight text.

@option code [String] @option mimetype [String, nil] @option language [String, nil] @option formatter_opts [Hash]

@yield code.

@return [String]

# 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) click to toggle source

Render a partial view.

@param path [String] path to partial @option variables [Hash, nil] variables for partial

@return [String]

# 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) click to toggle source
# 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

Private Instance Methods

content_blocks() click to toggle source

@return [Hash]

# File lib/rack/web_profiler/view.rb, line 211
def content_blocks
  @content_blocks ||= Hash.new { |h, k| h[k] = [] }
end