def self.next_table(table_reader, parent, attributes)
table = Table.new(parent, attributes)
table.title = attributes.delete('title') if attributes.has_key?('title')
table.assign_caption attributes.delete('caption')
if attributes.has_key? 'cols'
table.create_columns(parse_col_specs(attributes['cols']))
explicit_col_specs = true
else
explicit_col_specs = false
end
table_reader.skip_blank_lines
parser_ctx = Table::ParserContext.new(table, attributes)
while table_reader.has_more_lines?
line = table_reader.get_line
if parser_ctx.format == 'psv'
if parser_ctx.starts_with_delimiter? line
line = line[1..-1]
parser_ctx.close_open_cell
else
next_cell_spec, line = parse_cell_spec(line, :start)
if !next_cell_spec.nil?
parser_ctx.close_open_cell next_cell_spec
else
end
end
end
while !line.empty?
if m = parser_ctx.match_delimiter(line)
if parser_ctx.format == 'csv'
if parser_ctx.buffer_has_unclosed_quotes?(m.pre_match)
line = parser_ctx.skip_matched_delimiter(m)
next
end
else
if m.pre_match.end_with? '\\'
line = parser_ctx.skip_matched_delimiter(m, true)
next
end
end
if parser_ctx.format == 'psv'
next_cell_spec, cell_text = parse_cell_spec(m.pre_match, :end)
parser_ctx.push_cell_spec next_cell_spec
parser_ctx.buffer = %(#{parser_ctx.buffer}#{cell_text})
else
parser_ctx.buffer = %(#{parser_ctx.buffer}#{m.pre_match})
end
line = m.post_match
parser_ctx.close_cell
else
parser_ctx.buffer = %(#{parser_ctx.buffer}#{line})
if parser_ctx.format == 'csv'
parser_ctx.buffer = %(#{parser_ctx.buffer.rstrip} )
end
line = ''
if parser_ctx.format == 'psv' || (parser_ctx.format == 'csv' &&
parser_ctx.buffer_has_unclosed_quotes?)
parser_ctx.keep_cell_open
else
parser_ctx.close_cell true
end
end
end
table_reader.skip_blank_lines unless parser_ctx.cell_open?
if !table_reader.has_more_lines?
parser_ctx.close_cell true
end
end
table.attributes['colcount'] ||= parser_ctx.col_count
if !explicit_col_specs
even_width = (100.0 / parser_ctx.col_count).floor
table.columns.each {|c| c.assign_width(0, even_width) }
end
table.partition_header_footer attributes
table
end