module ToMd::TableBuilder

Public Class Methods

build(header, items) click to toggle source
# File lib/to_md.rb, line 27
def build header, items
  [
    row(header),
    row(header.map{'---'}),
    *items.map{|item|row item_to_array(header, item)}
  ].join($/)+$/
end
build_with_hash(items) click to toggle source
# File lib/to_md.rb, line 22
def build_with_hash items
  keys = items.map(&:keys).inject(:|)
  build keys, items
end
escape_cell(text) click to toggle source
# File lib/to_md.rb, line 48
def escape_cell text
  text.to_s.gsub('|', '|').gsub($/, '
')
end
item_to_array(header, item) click to toggle source
# File lib/to_md.rb, line 35
def item_to_array header, item
  if Hash === item
    header.map{|key|item[key]}
  else
    item = [*item] unless Array === item
    header.zip(item).map(&:last)
  end
end
row(array) click to toggle source
# File lib/to_md.rb, line 44
def row array
  '| '+array.map{|c|escape_cell c}.join(' | ')+' |'
end