def table node
result = []
if node.title?
result << %(.sp
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.B #{manify node.captioned_title})
end
result << '.TS
allbox tab(:);'
row_header = []
row_text = []
row_index = 0
[:head, :body, :foot].each do |tsec|
node.rows[tsec].each do |row|
row_header[row_index] ||= []
row_text[row_index] ||= []
remaining_cells = row.size
row.each_with_index do |cell, cell_index|
remaining_cells -= 1
row_header[row_index][cell_index] ||= []
if row_header[row_index][cell_index] == ['^t']
row_text[row_index] << %(T{#{LF}.sp#{LF}T}:)
end
row_text[row_index] << %(T{#{LF}.sp#{LF})
cell_halign = (cell.attr 'halign', 'left')[0..0]
if tsec == :head
if row_header[row_index].empty? ||
row_header[row_index][cell_index].empty?
row_header[row_index][cell_index] << %(#{cell_halign}tB)
else
row_header[row_index][cell_index + 1] ||= []
row_header[row_index][cell_index + 1] << %(#{cell_halign}tB)
end
row_text[row_index] << %(#{cell.text}#{LF})
elsif tsec == :body
if row_header[row_index].empty? ||
row_header[row_index][cell_index].empty?
row_header[row_index][cell_index] << %(#{cell_halign}t)
else
row_header[row_index][cell_index + 1] ||= []
row_header[row_index][cell_index + 1] << %(#{cell_halign}t)
end
case cell.style
when :asciidoc
cell_content = cell.content
when :verse, :literal
cell_content = cell.text
else
cell_content = cell.content.join
end
row_text[row_index] << %(#{cell_content}#{LF})
elsif tsec == :foot
if row_header[row_index].empty? ||
row_header[row_index][cell_index].empty?
row_header[row_index][cell_index] << %(#{cell_halign}tB)
else
row_header[row_index][cell_index + 1] ||= []
row_header[row_index][cell_index + 1] << %(#{cell_halign}tB)
end
row_text[row_index] << %(#{cell.text}#{LF})
end
if cell.colspan && cell.colspan > 1
(cell.colspan - 1).times do |i|
if row_header[row_index].empty? ||
row_header[row_index][cell_index].empty?
row_header[row_index][cell_index + i] << 'st'
else
row_header[row_index][cell_index + 1 + i] ||= []
row_header[row_index][cell_index + 1 + i] << 'st'
end
end
end
if cell.rowspan && cell.rowspan > 1
(cell.rowspan - 1).times do |i|
row_header[row_index + 1 + i] ||= []
if row_header[row_index + 1 + i].empty? ||
row_header[row_index + 1 + i][cell_index].empty?
row_header[row_index + 1 + i][cell_index] ||= []
row_header[row_index + 1 + i][cell_index] << '^t'
else
row_header[row_index + 1 + i][cell_index + 1] ||= []
row_header[row_index + 1 + i][cell_index + 1] << '^t'
end
end
end
if remaining_cells >= 1
row_text[row_index] << 'T}:'
else
row_text[row_index] << %(T}#{LF})
end
end
row_index += 1
end
end
result << LF
result << row_header.first.map {|r| 'lt'}.join(' ')
result << %(.#{LF})
row_text.each do |row|
result << row.join
end
result << %(.TE#{LF}.sp)
result.join
end