Method: Dita#table

Defined in:
lib/ruby-dita/table.rb

#table(column_info, rows = []) ⇒ Element

Returns valid Dita <table>.

Parameters:

  • column_info (Array, Hash)

    if Array of Strings, column headings; if Array of Elements, colspecs; if Hash, keys are strings for column headings; values are <colspec> elements

  • rows (Array) (defaults to: [])

    array of rows with which to populate table; can either be XML <row> elements or Strings representing values

Returns:

  • (Element)

    valid Dita <table>



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby-dita/table.rb', line 24

def table(column_info, rows=[])
  t = Element.new('table')
  headings = []
  tgroup = Element.new('tgroup')
  num_cols = column_info.size.to_s
  case column_info
    when Array
      headings = column_info if column_info.first.is_a?(String)
      colspecs = column_info if column_info.first.is_a?(Element) and column_info.first.name == 'colspec'
    when Hash
      headings = column_info.keys
      colspecs = column_info.values
    else
      # TODO raise error?

  end
  if colspecs
    colspecs.each_with_index do |c, index| c[:colname] = index.to_s end
    tgroup << colspecs
  end
  if headings.any?
    tgroup << Element.new('thead', [Element.new('row')])
    headings.each do |h|
      tgroup.thead.nodes.first << Element.new('entry', [h])
    end
  end

  tgroup[:cols] = num_cols
  tgroup << Element.new('tbody')
  tgroup.tbody << rows.collect do |r| row r end
  t << tgroup
end