class Asciidoctor::Table::ParserContext - asciidoctor-1.5.6.2 Documentation

# 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.

### Constants

DELIMITERS

  Public: A Hash mapping the AsciiDoc table formats to default delimiters

FORMATS

  Public: An Array of String keys that represent the table formats in AsciiDoc

### Attributes

buffer[RW]

Public: The String buffer of the currently open cell

colcount[R]

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

delimiter[R]

Public: The cell delimiter for this table.

delimiter_re[R]

Public: The cell delimiter compiled Regexp for this table.

format[RW]

Public: The AsciiDoc table format (psv, dsv, or csv)

table[RW]

Public: The Table currently being parsed

### Public Class Methods

new(reader, table, attributes = {}) click to toggle source

    # File lib/asciidoctor/table.rb, line 352
    def initialize reader, table, attributes = {}
      @reader = reader
      @table = table
      # IMPORTANT if reader.cursor becomes a reference, this assignment would require .dup
      @last_cursor = reader.cursor

      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
          warn %Q(asciidoctor: ERROR: #{reader.prev_line_info}: illegal table format: #{xsv})
          @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_re = DELIMITERS[xsv]
        # QUESTION should we support any other escape codes or multiple tabs?
        elsif sep == '\t'
          @delimiter, @delimiter_re = DELIMITERS['tsv']
        else
          @delimiter, @delimiter_re = sep, /#{::Regexp.escape sep}/
        end
      else
        @delimiter, @delimiter_re = 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

activate_rowspan(rowspan, colspan) click to toggle source

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 598
    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

advance() click to toggle source

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 619
    def advance
      @linenum += 1
    end

buffer_has_unclosed_quotes?(append = nil) click to toggle source

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 433
    def buffer_has_unclosed_quotes? append = nil
      if (record = append ? (buffer + append).strip : buffer.strip).start_with? '"'
        if ((trailing_quote = record.end_with? '"') && (record.end_with? '""')) || (record.start_with? '""')
          ((record = record.gsub '""', '').start_with? '"') && !(record.end_with? '"')
        else
          !trailing_quote
        end
      else
        false
      end
    end

cell_closed?() click to toggle source

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 492
    def cell_closed?
      !@cell_open
    end

cell_open?() click to toggle source

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 485
    def cell_open?
      @cell_open
    end

close_cell(eol = false) click to toggle source

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 513
    def close_cell(eol = false)
      if @format == 'psv'
        strip_text = true
        cell_text = @buffer
        @buffer = ''
        if (cellspec = take_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
        strip_text = false
        cell_text = @buffer.strip
        @buffer = ''
        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

            # collapse escaped quotes
            cell_text = cell_text.squeeze('"')
          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, :cursor => @last_cursor, :strip_text => strip_text)
        @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

close_open_cell(next_cellspec = {}) click to toggle source

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 501
    def close_open_cell(next_cellspec = {})
      push_cellspec next_cellspec
      close_cell(true) if cell_open?
      advance
      nil
    end

close_row() click to toggle source

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 582
    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

effective_column_visits() click to toggle source

Public: Calculate the effective column visits, which consists of the number of cells plus any active rowspans.

    # File lib/asciidoctor/table.rb, line 613
    def effective_column_visits
      @column_visits + @active_rowspans[0]
    end

end_of_row?() click to toggle source

Public: Check whether we've met the number of effective columns for the current row.

    # File lib/asciidoctor/table.rb, line 607
    def end_of_row?
      @colcount == -1 || effective_column_visits == @colcount
    end

keep_cell_open() click to toggle source

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 468
    def keep_cell_open
      @cell_open = true
      nil
    end

mark_cell_closed() click to toggle source

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 477
    def mark_cell_closed
      @cell_open = false
      nil
    end

match_delimiter(line) click to toggle source

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 409
    def match_delimiter(line)
      @delimiter_re.match(line)
    end

push_cellspec(cellspec = {}) click to toggle source

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 458
    def push_cellspec(cellspec = {})
      # this shouldn't be nil, but we check anyway
      @cellspecs << (cellspec || {})
      nil
    end

skip_past_delimiter(match) click to toggle source

Public: Skip past the matched delimiter because it's inside quoted text.

returns the String after the match

    # File lib/asciidoctor/table.rb, line 416
    def skip_past_delimiter(match)
      @buffer = %Q(#{@buffer}#{match.pre_match}#{@delimiter})
      match.post_match
    end

skip_past_escaped_delimiter(match) click to toggle source

Public: Skip past the matched delimiter because it's escaped.

returns the String after the match

    # File lib/asciidoctor/table.rb, line 424
    def skip_past_escaped_delimiter(match)
      @buffer = %Q(#{@buffer}#{match.pre_match.chop}#{@delimiter})
      match.post_match
    end

starts_with_delimiter?(line) click to toggle source

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 401
    def starts_with_delimiter?(line)
      line.start_with? @delimiter
    end

take_cellspec() click to toggle source

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 450
    def take_cellspec
      @cellspecs.shift
    end