class Tag

Attributes

attributes[RW]
children[RW]
name[R]
text[RW]

Public Class Methods

new(name, text="", attributes={}, children=[]) click to toggle source
# File lib/tablinate/tag.rb, line 5
def initialize(name, text="", attributes={}, children=[])
  @name = name.to_sym
  @attributes = attributes || {}
  @children = children || []
  @text = text.to_s
end

Public Instance Methods

to_html() click to toggle source
# File lib/tablinate/tag.rb, line 16
def to_html
  format_html(to_s)
end
to_s(offset=0) click to toggle source
# File lib/tablinate/tag.rb, line 12
def to_s(offset=0)
  build_open_tag(offset) + text + add_sub_tags + build_close_tag
end

Private Instance Methods

add_sub_tags() click to toggle source
# File lib/tablinate/tag.rb, line 25
def add_sub_tags
  children.map.with_index {|tag, i| tag.to_s(i) }.join("")
end
attributes_to_html(offset) click to toggle source
# File lib/tablinate/tag.rb, line 34
def attributes_to_html(offset)
  attributes.map do |key,value| 
    case value
    when String 
      "#{key}='#{value}'"
    when Symbol
      "#{key}='#{value}'"
    when Array
      "#{key}='#{value[offset % value.count]}'"
    end
  end.join(" ")
end
build_close_tag() click to toggle source
# File lib/tablinate/tag.rb, line 21
def build_close_tag
  "</#{name}>"
end
build_open_tag(offset) click to toggle source
# File lib/tablinate/tag.rb, line 29
def build_open_tag offset
  attributes = attributes_to_html(offset)
  "<#{name}#{attributes.empty? ? '' : ' '}" + attributes + ">"
end
format_html(html) click to toggle source
# File lib/tablinate/tag.rb, line 47
def format_html(html)
  #Finds html tags via regex and adds whitespace so
  #that the table doesn't look disgusting in the
  #source code.
  tags = html.scan(%r{</?[^>]+?>}).uniq
  tags.each do |tag|
    if tag.length > 5 || tag.include?("/") || tag.include?("tr>") then
      html.gsub!(tag,"#{tag}\n") 
    else
      html.gsub!(tag,"\s\s#{tag}")
    end
  end
  return html
end