class Ditz::ErbHtml

pass through any variables needed for template generation, and add a bunch of HTML formatting utility methods.

Public Class Methods

new(template_dir, links, binding={}) click to toggle source
# File lib/html.rb, line 8
def initialize template_dir, links, binding={}
  @template_dir = template_dir
  @links = links
  @binding = binding
end

Public Instance Methods

clone_for_binding(extra_binding={}) click to toggle source

return an ErbHtml object that has the current binding plus extra_binding merged in

# File lib/html.rb, line 15
def clone_for_binding extra_binding={}
  extra_binding.empty? ? self : ErbHtml.new(@template_dir, @links, @binding.merge(extra_binding))
end
h(o;) click to toggle source

the following methods are meant to be called from the ERB itself

# File lib/html.rb, line 41
def h o; o.to_s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;") end
issue_status_img_for(i, opts={}) click to toggle source
# File lib/html.rb, line 52
def issue_status_img_for i, opts={}
  fn, title = if i.closed?
    case i.disposition
    when :fixed; ["green-check.png", "fixed"]
    when :wontfix; ["red-check.png", "won't fix"]
    when :reorg; ["blue-check.png", "reorganized"]
    end
  elsif i.in_progress?
    ["green-bar.png", "in progress"]
  elsif i.paused?
    ["yellow-bar.png", "paused"]
  end

  return "" unless fn

  args = {:src => fn, :alt => title, :title => title}
  args[:class] = opts[:class] if opts[:class]

  "<img " + args.map { |k, v| "#{k}=#{v.inspect}" }.join(" ") + "/>"
end
method_missing(meth, *a) click to toggle source
Calls superclass method
# File lib/html.rb, line 99
def method_missing meth, *a
  @binding.member?(meth) ? @binding[meth] : super
end
obscured_email(e;) click to toggle source
# File lib/html.rb, line 44
def obscured_email e; h e.gsub(/@.*?(>|$)/, "@...\\1") end
p(o;) click to toggle source
# File lib/html.rb, line 43
def p o; "<p>" + h(o.to_s).gsub("\n\n", "</p><p>") + "</p>" end
progress_meter(p, size=50) click to toggle source
# File lib/html.rb, line 86
def progress_meter p, size=50
  done = (p * size).to_i
  undone = [size - done, 0].max
  "<span class='progress-meter'><span class='progress-meter-done'>" +
    ("&nbsp;" * done) +
    "</span><span class='progress-meter-undone'>" +
    ("&nbsp;" * undone) +
    "</span></span>"
end
render(template_name, extra_binding={})

render a nested ERB

Alias for: render_template
render_string(s, extra_binding={}) click to toggle source
# File lib/html.rb, line 29
def render_string s, extra_binding={}
  if extra_binding.empty?
    ERB.new(s).result binding
  else
    clone_for_binding(extra_binding).render_string s
  end
end
render_template(template_name, extra_binding={}) click to toggle source
# File lib/html.rb, line 19
def render_template template_name, extra_binding={}
  if extra_binding.empty?
    @@erbs ||= {}
    @@erbs[template_name] ||= ERB.new IO.read(File.join(@template_dir, "#{template_name}.rhtml"))
    @@erbs[template_name].result binding
  else
    clone_for_binding(extra_binding).render_template template_name
  end
end
Also aliased as: render
t(o;) click to toggle source
# File lib/html.rb, line 42
def t o; o.strftime "%Y-%m-%d %H:%M %Z" end