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 satisifies the column count and, finally, a new buffer is allocated to track the next cell.
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 321 def initialize(reader, table, attributes = {}) @reader = reader @table = table # TODO if reader.cursor becomes a reference, this would require .dup @last_cursor = reader.cursor if (@format = attributes['format']) unless Table::DATA_FORMATS.include? @format raise %Q(Illegal table format: #{@format}) end else @format = Table::DEFAULT_DATA_FORMAT end @delimiter = if @format == 'psv' && !(attributes.key? 'separator') && table.document.nested? '!' else attributes['separator'] || Table::DEFAULT_DELIMITERS[@format] end @delimiter_re = /#{Regexp.escape @delimiter}/ @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: Activate a rowspan. The rowspan Array is consulted when determining the effective number of cells in the current row.
returns nothing
# File lib/asciidoctor/table.rb, line 543 def activate_rowspan(rowspan, colspan) 1.upto(rowspan - 1).each {|i| # longhand assignment used for Opal compatibility @active_rowspans[i] = (@active_rowspans[i] || 0) + colspan } nil end
Internal: Advance to the next line (which may come after the parser begins processing the next line if the last cell had wrapped content).
# File lib/asciidoctor/table.rb, line 564 def advance @linenum += 1 end
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 379 def buffer_has_unclosed_quotes?(append = nil) record = %Q(#{@buffer}#{append}).strip record.start_with?('"') && !record.start_with?('""') && !record.end_with?('"') end
Public: Determines whether the buffer contains quoted data. Used for CSV data.
returns true if the buffer starts with a double quote (and not an escaped double quote), false otherwise
# File lib/asciidoctor/table.rb, line 388 def buffer_quoted? @buffer = @buffer.lstrip @buffer.start_with?('"') && !@buffer.start_with?('""') 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 440 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 433 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 461 def close_cell(eol = false) cell_text = @buffer.strip @buffer = '' if @format == 'psv' cellspec = take_cellspec if cellspec repeat = cellspec.delete('repeatcol') || 1 else warn %Q(asciidoctor: ERROR: #{@last_cursor.line_info}: table missing leading separator, recovering automatically) cellspec = {} repeat = 1 end else cellspec = 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...-1].strip end # collapses escaped quotes cell_text = cell_text.tr_s('"', '"') 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]) warn %Q(asciidoctor: ERROR: #{@last_cursor.line_info}: dropping cell because it exceeds specified number of columns) return end end cell = Table::Cell.new(column, cell_text, cellspec, @last_cursor) @last_cursor = @reader.cursor 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 449 def close_open_cell(next_cellspec = {}) push_cellspec next_cellspec close_cell(true) if cell_open? advance nil end
Public: Close the row by adding it to the Table and resetting the row Array and counter variables.
returns nothing
# File lib/asciidoctor/table.rb, line 527 def close_row @table.rows.body << @current_row # don't have to account for active rowspans here # since we know this is first row @colcount = @column_visits if @colcount == -1 @column_visits = 0 @current_row = [] @active_rowspans.shift @active_rowspans[0] ||= 0 nil end
Public: Calculate the effective column visits, which consists of the number of cells plus any active rowspans.
# File lib/asciidoctor/table.rb, line 558 def effective_column_visits @column_visits + @active_rowspans[0] end
Public: Check whether we've met the number of effective columns for the current row.
# File lib/asciidoctor/table.rb, line 552 def end_of_row? @colcount == -1 || effective_column_visits == @colcount 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 416 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 425 def mark_cell_closed @cell_open = false nil end
Public: Checks whether the line provided contains the cell delimiter used by this table.
returns Regexp MatchData if the line contains the delimiter, false otherwise
# File lib/asciidoctor/table.rb, line 362 def match_delimiter(line) @delimiter_re.match(line) 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 406 def push_cellspec(cellspec = {}) # this shouldn't be nil, but we check anyway @cellspecs << (cellspec || {}) nil end
Public: Skip beyond the matched delimiter because it was a false positive (either because it was escaped or in a quoted context)
returns the String after the match
# File lib/asciidoctor/table.rb, line 370 def skip_matched_delimiter(match, escaped = false) @buffer = %Q(#{@buffer}#{escaped ? match.pre_match.chop : match.pre_match}#{@delimiter}) match.post_match 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 354 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 398 def take_cellspec @cellspecs.shift end