class Asciidoctor::Table::ParserContext
finally, a new buffer is allocated to track the next cell.
instantiated, the row is closed if the cell satisifies the column count and,
is located, the previous cell is closed, an instance of Table::Cell is
moves through the lines of the table using tail recursion. When a cell boundary
class are primarily responsible for tracking the buffer of a cell as the parser
Public: Methods for managing the parsing of an AsciiDoc table. Instances of this
def activate_rowspan(rowspan, colspan)
determining the effective number of cells in the current row.
Public: Activate a rowspan. The rowspan Array is consulted when
def activate_rowspan(rowspan, colspan) 1.upto(rowspan - 1).each {|i| @active_rowspans[i] ||= 0 @active_rowspans[i] += colspan } nil end
def buffer_has_unclosed_quotes?(append = nil)
returns true if the buffer has unclosed quotes, false if it doesn't or it
Public: Determines whether the buffer has unclosed quotes. Used for CSV data.
def buffer_has_unclosed_quotes?(append = nil) record = "#@buffer#{append}".strip record.start_with?('"') && !record.start_with?('""') && !record.end_with?('"') end
def buffer_quoted?
returns true if the buffer starts with a double quote (and not an escaped double quote),
Public: Determines whether the buffer contains quoted data. Used for CSV data.
def buffer_quoted? @buffer.lstrip! @buffer.start_with?('"') && !@buffer.start_with?('""') end
def cell_closed?
Public: Checks whether the current cell has been marked as closed
def cell_closed? !@cell_open end
def cell_open?
Public: Checks whether the current cell is still open
def cell_open? @cell_open end
def close_cell(eol = false)
row has been met, close the row and begin a new one.
the current row and, if the number of expected columns for the current
Public: Close the current cell, instantiate a new Table::Cell, add it to
def close_cell(eol = false) cell_text = @buffer.strip @buffer = '' if format == 'psv' cell_spec = take_cell_spec repeat = cell_spec.fetch('repeatcol', 1) cell_spec.delete('repeatcol') else cell_spec = nil repeat = 1 if format == 'csv' if !cell_text.empty? && cell_text.include?('"') # this may not be perfect logic, but it hits the 99% if cell_text.start_with?('"') && cell_text.end_with?('"') # unquote cell_text = cell_text[1..-2].strip end # collapses escaped quotes cell_text = cell_text.tr_s('"', '"') end end end 1.upto(repeat) {|i| # make column resolving an operation if @col_count == -1 @table.columns << Table::Column.new(@table, @current_row.size + i - 1) column = @table.columns.last else # QUESTION is this right for cells that span columns? column = @table.columns[@current_row.size] end cell = Table::Cell.new(column, cell_text, cell_spec) unless cell.rowspan.nil? || cell.rowspan == 1 activate_rowspan(cell.rowspan, (cell.colspan || 1)) end @col_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 col_count/linenum logic should be in end_of_row? (or a should_end_row? method) close_row if end_of_row? && (@col_count != -1 || @linenum > 0 || (eol && i == repeat)) } @open_cell = false nil end
def close_open_cell(next_cell_spec = {})
by the next cell.
cell spec captured from the end of this cell onto the stack for use
Public: If the current cell is open, close it. In additional, push the
def close_open_cell(next_cell_spec = {}) push_cell_spec next_cell_spec close_cell(true) if cell_open? next_line nil end
def close_row
Array and counter variables.
Public: Close the row by adding it to the Table and resetting the row
def close_row @table.rows.body << @current_row # don't have to account for active rowspans here # since we know this is first row @col_count = @col_visits if @col_count == -1 @col_visits = 0 @current_row = [] @active_rowspans.shift @active_rowspans[0] ||= 0 nil end
def effective_col_visits
Public: Calculate the effective column visits, which consists of the number of
def effective_col_visits @col_visits + @active_rowspans.first end
def end_of_row?
def end_of_row? @col_count == -1 || effective_col_visits == @col_count end
def initialize(table, attributes = {})
def initialize(table, attributes = {}) @table = table if attributes.has_key? 'format' @format = attributes['format'] if !Table::DATA_FORMATS.include? @format raise "Illegal table format: #@format" end else @format = Table::DEFAULT_DATA_FORMAT end if @format == 'psv' && !attributes.has_key?('separator') && table.document.nested? @delimiter = '!' else @delimiter = attributes.fetch('separator', Table::DEFAULT_DELIMITERS[@format]) end @delimiter_re = /#{Regexp.escape @delimiter}/ @col_count = table.columns.empty? ? -1 : table.columns.size @buffer = '' @cell_specs = [] @cell_open = false @active_rowspans = [0] @col_visits = 0 @current_row = [] @linenum = -1 end
def keep_cell_open
reached and the cell may contain additional text.
Public: Marks that the cell should be kept open. Used when the end of the line is
def keep_cell_open @cell_open = true nil end
def mark_cell_closed
instance and add it to the current row.
Public: Marks the cell as closed so that the parser knows to instantiate a new cell
def mark_cell_closed @cell_open = false nil end
def match_delimiter(line)
used by this table.
Public: Checks whether the line provided contains the cell delimiter
def match_delimiter(line) line.match @delimiter_re end
def next_line
Internal: Advance to the next line (which may come after the parser begins processing
def next_line @linenum += 1 end
def push_cell_spec(cell_spec = {})
stack is used to carry over the spec to the next cell.
Public: Puts a cell spec onto the stack. Cell specs precede the delimiter, so a
def push_cell_spec(cell_spec = {}) # this shouldn't be nil, but we check anyway @cell_specs << (cell_spec || {}) nil end
def skip_matched_delimiter(match, escaped = false)
(either because it was escaped or in a quoted context)
Public: Skip beyond the matched delimiter because it was a false positive
def skip_matched_delimiter(match, escaped = false) @buffer << (escaped ? match.pre_match.chop : match.pre_match) << @delimiter match.post_match end
def starts_with_delimiter?(line)
used by this table.
Public: Checks whether the line provided starts with the cell delimiter
def starts_with_delimiter?(line) line.start_with? @delimiter end
def take_cell_spec()
when the cell is being closed.
stack is used to carry over the spec from the previous cell to the current cell
Public: Takes a cell spec from the stack. Cell specs precede the delimiter, so a
def take_cell_spec() @cell_specs.shift end