class Asciidoctor::Table::ParserContext
Public: Methods for managing the parsing of an AsciiDoc table. Instances of this class are primarily responsible for tracking the buffer of a cell as the parser moves through the lines of the table using tail recursion. When a cell boundary is located, the previous cell is closed, an instance of Table::Cell
is instantiated, the row is closed if the cell satisfies the column count and, finally, a new buffer is allocated to track the next cell.
Constants
Attributes
Public: The String
buffer of the currently open cell
Public: Get the expected column count for a row
colcount is the number of columns to pull into a row A value of -1 means we use the number of columns found in the first line as the colcount
Public: The cell delimiter for this table.
Public: The cell delimiter compiled Regexp
for this table.
Public: The AsciiDoc table format (psv, dsv, or csv)
Public: The Table
currently being parsed
Public Class Methods
# File lib/asciidoctor/table.rb, line 455 def initialize reader, table, attributes = {} @start_cursor_data = (@reader = reader).mark @table = table if attributes.key? 'format' if FORMATS.include?(xsv = attributes['format']) if xsv == 'tsv' # NOTE tsv is just an alias for csv with a tab separator @format = 'csv' elsif (@format = xsv) == 'psv' && table.document.nested? xsv = '!sv' end else logger.error message_with_context %(illegal table format: #{xsv}), source_location: reader.cursor_at_prev_line @format, xsv = 'psv', (table.document.nested? ? '!sv' : 'psv') end else @format, xsv = 'psv', (table.document.nested? ? '!sv' : 'psv') end if attributes.key? 'separator' if (sep = attributes['separator']).nil_or_empty? @delimiter, @delimiter_rx = DELIMITERS[xsv] # QUESTION should we support any other escape codes or multiple tabs? elsif sep == '\t' @delimiter, @delimiter_rx = DELIMITERS['tsv'] else @delimiter, @delimiter_rx = sep, /#{::Regexp.escape sep}/ end else @delimiter, @delimiter_rx = DELIMITERS[xsv] end @colcount = table.columns.empty? ? -1 : table.columns.size @buffer = '' @cellspecs = [] @cell_open = false @active_rowspans = [0] @column_visits = 0 @current_row = [] @linenum = -1 end
Public Instance Methods
Public: Determines whether the buffer has unclosed quotes. Used for CSV data.
returns true if the buffer has unclosed quotes, false if it doesn’t or it isn’t quoted data
# File lib/asciidoctor/table.rb, line 534 def buffer_has_unclosed_quotes? append = nil, q = '"' if (record = append ? (@buffer + append).strip : @buffer.strip) == q true elsif record.start_with? q qq = q + q if ((trailing_quote = record.end_with? q) && (record.end_with? qq)) || (record.start_with? qq) ((record = record.gsub qq, '').start_with? q) && !(record.end_with? q) else !trailing_quote end else false end end
Public: Checks whether the current cell has been marked as closed
returns true if the cell is marked as closed, false otherwise
# File lib/asciidoctor/table.rb, line 596 def cell_closed? !@cell_open end
Public: Checks whether the current cell is still open
returns true if the cell is marked as open, false otherwise
# File lib/asciidoctor/table.rb, line 589 def cell_open? @cell_open end
Public: Close the current cell, instantiate a new Table::Cell
, add it to the current row and, if the number of expected columns for the current row has been met, close the row and begin a new one.
returns nothing
# File lib/asciidoctor/table.rb, line 617 def close_cell(eol = false) if @format == 'psv' cell_text = @buffer @buffer = '' if (cellspec = take_cellspec) repeat = cellspec.delete('repeatcol') || 1 else logger.error message_with_context 'table missing leading separator; recovering automatically', source_location: Reader::Cursor.new(*@start_cursor_data) cellspec = {} repeat = 1 end else cell_text = @buffer.strip @buffer = '' cellspec = nil repeat = 1 if @format == 'csv' && !cell_text.empty? && (cell_text.include? (q = '"')) # this may not be perfect logic, but it hits the 99% if (cell_text.start_with? q) && (cell_text.end_with? q) # unquote if (cell_text = cell_text.slice(1, cell_text.length - 2)) # trim whitespace and collapse escaped quotes cell_text = cell_text.strip.squeeze q else logger.error message_with_context 'unclosed quote in CSV data; setting cell to empty', source_location: @reader.cursor_at_prev_line cell_text = '' end else # collapse escaped quotes cell_text = cell_text.squeeze q end end end 1.upto(repeat) do |i| # TODO make column resolving an operation if @colcount == -1 @table.columns << (column = Table::Column.new(@table, @table.columns.size + i - 1)) if cellspec && (cellspec.key? 'colspan') && (extra_cols = cellspec['colspan'].to_i - 1) > 0 offset = @table.columns.size extra_cols.times do |j| @table.columns << Table::Column.new(@table, offset + j) end end else # QUESTION is this right for cells that span columns? unless (column = @table.columns[@current_row.size]) logger.error message_with_context 'dropping cell because it exceeds specified number of columns', source_location: @reader.cursor_before_mark return nil end end cell = Table::Cell.new(column, cell_text, cellspec, cursor: @reader.cursor_before_mark) @reader.mark unless !cell.rowspan || cell.rowspan == 1 activate_rowspan(cell.rowspan, (cell.colspan || 1)) end @column_visits += (cell.colspan || 1) @current_row << cell # don't close the row if we're on the first line and the column count has not been set explicitly # TODO perhaps the colcount/linenum logic should be in end_of_row? (or a should_end_row? method) close_row if end_of_row? && (@colcount != -1 || @linenum > 0 || (eol && i == repeat)) end @cell_open = false nil end
Public: If the current cell is open, close it. In additional, push the cell spec captured from the end of this cell onto the stack for use by the next cell.
returns nothing
# File lib/asciidoctor/table.rb, line 605 def close_open_cell(next_cellspec = {}) push_cellspec next_cellspec close_cell(true) if cell_open? advance nil end
Public: Marks that the cell should be kept open. Used when the end of the line is reached and the cell may contain additional text.
returns nothing
# File lib/asciidoctor/table.rb, line 572 def keep_cell_open @cell_open = true nil end
Public: Marks the cell as closed so that the parser knows to instantiate a new cell instance and add it to the current row.
returns nothing
# File lib/asciidoctor/table.rb, line 581 def mark_cell_closed @cell_open = false nil end
Public: Puts a cell spec onto the stack. Cell specs precede the delimiter, so a stack is used to carry over the spec to the next cell.
returns nothing
# File lib/asciidoctor/table.rb, line 562 def push_cellspec(cellspec = {}) # this shouldn't be nil, but we check anyway @cellspecs << (cellspec || {}) nil end
Public: Skip past the matched delimiter because it’s inside quoted text.
Returns nothing
# File lib/asciidoctor/table.rb, line 517 def skip_past_delimiter(pre) @buffer = %(#{@buffer}#{pre}#{@delimiter}) nil end
Public: Skip past the matched delimiter because it’s escaped.
Returns nothing
# File lib/asciidoctor/table.rb, line 525 def skip_past_escaped_delimiter(pre) @buffer = %(#{@buffer}#{pre.chop}#{@delimiter}) nil end
Public: Checks whether the line provided starts with the cell delimiter used by this table.
returns true if the line starts with the delimiter, false otherwise
# File lib/asciidoctor/table.rb, line 502 def starts_with_delimiter?(line) line.start_with? @delimiter end
Public: Takes a cell spec from the stack. Cell specs precede the delimiter, so a stack is used to carry over the spec from the previous cell to the current cell when the cell is being closed.
returns The cell spec Hash
captured from parsing the previous cell
# File lib/asciidoctor/table.rb, line 554 def take_cellspec @cellspecs.shift end