class HTMLTable

Public Class Methods

new() click to toggle source
# File lib/zarchitect/htmltable.rb, line 3
def initialize
  @lines = Array.new
  @starts_at = nil
  @ends_at = nil
  @coln = 0
  @html = "<table>"
  @rows = Array.new
end

Public Instance Methods

add_line(l) click to toggle source
# File lib/zarchitect/htmltable.rb, line 39
def add_line(l)
  @coln = l.count('|') if l.count('|') > @coln
  @lines.push l
end
ends_at(x) click to toggle source
# File lib/zarchitect/htmltable.rb, line 52
def ends_at(x)
  @ends_at = x
end
print() click to toggle source
process() click to toggle source
# File lib/zarchitect/htmltable.rb, line 12
def process
  @coln -= 1 # table syntax expects one more pipes than columns

  @lines.each_with_index do |l,i|
    ar = l.split('|', -1)
    ar.shift
    ar.pop
    ar2 = get_colspans(ar)
    ar.map! { |a| a.strip }
    if ar[0].count("-") == ar[0].length && ar[0].count("-") > 0
      # header previous row is header
      @rows.last.set_header
    else
      # row
      @rows.push HTMLTableRow.new(ar,ar2) 
    end
  end

  @rows.each do |r|
    r.process
    @html << r.html
  end


  @html << "</table>"
end
replace(ar) click to toggle source
# File lib/zarchitect/htmltable.rb, line 56
def replace(ar)
  ar[@starts_at] = @html
  if @ends_at.nil?
    @ends_at = ar.length
    process
  end
  ar.each_with_index do |x,i|
    ar[i] = nil if i > @starts_at && i <= @ends_at
  end
  ar
end
starts_at(x) click to toggle source
# File lib/zarchitect/htmltable.rb, line 48
def starts_at(x)
  @starts_at = x
end

Private Instance Methods

cspancnt(c, ar,i) click to toggle source
# File lib/zarchitect/htmltable.rb, line 82
def cspancnt(c, ar,i)
  if i+1 < ar.length
    if ar[i+1] == ""
      c += 1
      c = cspancnt(c, ar, i+1)
    end
  end
  c
end
get_colspans(ar) click to toggle source
# File lib/zarchitect/htmltable.rb, line 70
def get_colspans(ar)
  cspans = Array.new
  ar.each_with_index do |str,i|
    if str == ""
      cspans.push 0
    else
      cspans.push cspancnt(1, ar,i)
    end
  end
  cspans
end