Module: Vanity::Render

Included in:
Commands
Defined in:
lib/vanity/commands/report.rb

Overview

Render method available to templates (when used by Vanity command line, outside Rails).

Instance Method Summary collapse

Instance Method Details

#render(path, locals = {}) ⇒ Object

Render the named template. Used for reporting and the dashboard.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vanity/commands/report.rb', line 11

def render(path, locals = {})
  locals[:playground] = self
  keys = locals.keys
  struct = Struct.new(*keys)
  struct.send :include, Render
  locals = struct.new(*locals.values_at(*keys))
  dir, base = File.split(path)
  path = File.join(dir, "_#{base}")
  erb = ERB.new(File.read(path), nil, '<>')
  erb.filename = path
  erb.result(locals.instance_eval { binding })
end

#vanity_h(html) ⇒ Object

Escape HTML.



25
26
27
# File 'lib/vanity/commands/report.rb', line 25

def vanity_h(html)
  CGI.escapeHTML(html.to_s)
end

#vanity_html_safe(text) ⇒ Object



29
30
31
# File 'lib/vanity/commands/report.rb', line 29

def vanity_html_safe(text)
  text
end

#vanity_simple_format(text, options = {}) ⇒ Object

Dumbed down from Rails’ simple_format.



34
35
36
37
38
39
40
# File 'lib/vanity/commands/report.rb', line 34

def vanity_simple_format(text, options={})
  open = "<p #{options.map { |k,v| "#{k}=\"#{CGI.escapeHTML v}\"" }.join(" ")}>"
  text = open + text.gsub(/\r\n?/, "\n").   # \r\n and \r -> \n
    gsub(/\n\n+/, "</p>\n\n#{open}").       # 2+ newline  -> paragraph
    gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') + # 1 newline   -> br
    "</p>"
end