Sha256: 2a85acd304428569521d7c991af304ad717d1df5f1d824e0f2da5d4ca1b4c40f

Contents?: true

Size: 1.08 KB

Versions: 13

Compression:

Stored size: 1.08 KB

Contents

require "tempfile"

class DynportTools::AsciiTable
  attr_accessor :headers
  attr_accessor :rows
  
  def initialize(attributes = {})
    self.headers = attributes[:headers] || []
    self.rows = attributes[:rows] || []
  end
  
  def to_tsv
    ([headers] + rows).map { |line| line.join("\t") }.join("\n")
  end
  
  def to_html
    html = "<table border=1 align=center>"
    html << "<tr>" + headers.map { |header| html_table_cell(header, "th") }.join("") if headers.any?
    html << rows.map { |row| "<tr>" + row.map { |r| html_table_cell(r, "td") }.join("") }.join("")
    html + "</table>"
  end
  
  def html_table_cell(text_or_array, node = "td")
    text, options = text_or_array
    "<#{node}#{options ? options.map { |key, value| " #{key}=#{value}" }.join("") : ""}>#{text}"
  end
  
  def to_ascii
    html2ascii(to_html)
  end
  
  def to(format)
    send(:"to_#{format}")
  end
  
  def html2ascii(html)
    tempfile = Tempfile.new("html2ascii")
    tempfile.print(html)
    tempfile.close
    ascii = Kernel.send(:`, "links -dump #{tempfile.path}")
    tempfile.delete
    ascii
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dynport_tools-0.2.22 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.21 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.20 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.19 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.18 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.17 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.16 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.15 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.14 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.13 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.12 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.11 lib/dynport_tools/ascii_table.rb
dynport_tools-0.2.10 lib/dynport_tools/ascii_table.rb