Class: Asciidoctor::Table::ParserContext

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/asciidoctor/table.rb

Overview

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.

Constant Summary collapse

FORMATS =

An Array of String keys that represent the table formats in AsciiDoc – QUESTION should we recognize !sv as a valid format value?

['psv', 'csv', 'dsv', 'tsv'].to_set
DELIMITERS =

A Hash mapping the AsciiDoc table formats to default delimiters

{
  'psv' => ['|', /\|/],
  'csv' => [',', /,/],
  'dsv' => [':', /:/],
  'tsv' => [?\t, /\t/],
  '!sv' => ['!', /!/],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, #message_with_context

Constructor Details

#initialize(reader, table, attributes = {}) ⇒ ParserContext

Returns a new instance of ParserContext.



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/asciidoctor/table.rb', line 458

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

Instance Attribute Details

#bufferObject

The String buffer of the currently open cell



450
451
452
# File 'lib/asciidoctor/table.rb', line 450

def buffer
  @buffer
end

#colcountObject (readonly)

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



447
448
449
# File 'lib/asciidoctor/table.rb', line 447

def colcount
  @colcount
end

#delimiterObject (readonly)

The cell delimiter for this table.



453
454
455
# File 'lib/asciidoctor/table.rb', line 453

def delimiter
  @delimiter
end

#delimiter_reObject (readonly)

The cell delimiter compiled Regexp for this table.



456
457
458
# File 'lib/asciidoctor/table.rb', line 456

def delimiter_re
  @delimiter_re
end

#formatObject

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



440
441
442
# File 'lib/asciidoctor/table.rb', line 440

def format
  @format
end

#tableObject

The Table currently being parsed



437
438
439
# File 'lib/asciidoctor/table.rb', line 437

def table
  @table
end

Instance Method Details

#buffer_has_unclosed_quotes?(append = nil) ⇒ Boolean

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

Returns:

  • (Boolean)


537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/asciidoctor/table.rb', line 537

def buffer_has_unclosed_quotes? append = nil
  if (record = append ? (@buffer + append).strip : @buffer.strip) == '"'
    true
  elsif record.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?Boolean

Checks whether the current cell has been marked as closed

returns true if the cell is marked as closed, false otherwise

Returns:

  • (Boolean)


598
599
600
# File 'lib/asciidoctor/table.rb', line 598

def cell_closed?
  !@cell_open
end

#cell_open?Boolean

Checks whether the current cell is still open

returns true if the cell is marked as open, false otherwise

Returns:

  • (Boolean)


591
592
593
# File 'lib/asciidoctor/table.rb', line 591

def cell_open?
  @cell_open
end

#close_cell(eol = false) ⇒ Object

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



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/asciidoctor/table.rb', line 619

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?('"')
      # this may not be perfect logic, but it hits the 99%
      if cell_text.start_with?('"') && cell_text.end_with?('"')
        # unquote
        if (cell_text = cell_text.slice(1, cell_text.length - 2))
          # trim whitespace and collapse escaped quotes
          cell_text = cell_text.strip.squeeze('"')
        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('"')
      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
      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

#close_open_cell(next_cellspec = {}) ⇒ Object

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



607
608
609
610
611
612
# File 'lib/asciidoctor/table.rb', line 607

def close_open_cell(next_cellspec = {})
  push_cellspec next_cellspec
  close_cell(true) if cell_open?
  advance
  nil
end

#keep_cell_openObject

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



574
575
576
577
# File 'lib/asciidoctor/table.rb', line 574

def keep_cell_open
  @cell_open = true
  nil
end

#mark_cell_closedObject

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



583
584
585
586
# File 'lib/asciidoctor/table.rb', line 583

def mark_cell_closed
  @cell_open = false
  nil
end

#match_delimiter(line) ⇒ Object

Checks whether the line provided contains the cell delimiter used by this table.

returns Regexp MatchData if the line contains the delimiter, false otherwise



513
514
515
# File 'lib/asciidoctor/table.rb', line 513

def match_delimiter(line)
  @delimiter_rx.match(line)
end

#push_cellspec(cellspec = {}) ⇒ Object

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



564
565
566
567
568
# File 'lib/asciidoctor/table.rb', line 564

def push_cellspec(cellspec = {})
  # this shouldn't be nil, but we check anyway
  @cellspecs << (cellspec || {})
  nil
end

#skip_past_delimiter(pre) ⇒ void

This method returns an undefined value.

Skip past the matched delimiter because it’s inside quoted text.



520
521
522
523
# File 'lib/asciidoctor/table.rb', line 520

def skip_past_delimiter(pre)
  @buffer = %(#{@buffer}#{pre}#{@delimiter})
  nil
end

#skip_past_escaped_delimiter(pre) ⇒ void

This method returns an undefined value.

Skip past the matched delimiter because it’s escaped.



528
529
530
531
# File 'lib/asciidoctor/table.rb', line 528

def skip_past_escaped_delimiter(pre)
  @buffer = %(#{@buffer}#{pre.chop}#{@delimiter})
  nil
end

#starts_with_delimiter?(line) ⇒ Boolean

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

Returns:

  • (Boolean)


505
506
507
# File 'lib/asciidoctor/table.rb', line 505

def starts_with_delimiter?(line)
  line.start_with? @delimiter
end

#take_cellspecObject

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



556
557
558
# File 'lib/asciidoctor/table.rb', line 556

def take_cellspec
  @cellspecs.shift
end