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("&", "&").gsub("<", "<").gsub(">", ">") end
issue_link_for(i, opts={})
click to toggle source
# File lib/html.rb, line 73 def issue_link_for i, opts={} link = link_to i, "#{i.title}" link = "<span class=\"inline-issue-link\">" + link + "</span>" if opts[:inline] link = link + " " + issue_status_img_for(i, :class => "inline-status-image") if opts[:status_image] link 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
link_issue_names(project, s, opts={})
click to toggle source
# File lib/html.rb, line 80 def link_issue_names project, s, opts={} project.issues.inject(s) do |s, i| s.gsub(/\b#{i.name}\b/, issue_link_for(i, {:inline => true, :status_image => true}.merge(opts))) end end
link_to(o, name)
click to toggle source
# File lib/html.rb, line 45 def link_to o, name dest = @links[o] dest = o if dest.nil? && o.is_a?(String) raise ArgumentError, "no link for #{o.inspect}" unless dest "<a href=\"#{dest}\">#{name}</a>" 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'>" + (" " * done) + "</span><span class='progress-meter-undone'>" + (" " * undone) + "</span></span>" end
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