Class: RedQuilt::Table::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/red_quilt/table.rb

Overview

Cached collaborator for BlockParser. A single instance is created in BlockParser#initialize and reused; per-call state lives in method locals so reentrant calls are safe.

Instance Method Summary collapse

Constructor Details

#initialize(block_parser) ⇒ Parser

Returns a new instance of Parser.



45
46
47
# File 'lib/red_quilt/table.rb', line 45

def initialize(block_parser)
  @arena = block_parser.arena
end

Instance Method Details

#parse(parent_id, lines, index) ⇒ Object

Parses the table starting at lines (already confirmed by Table.start?). Returns the index past the table.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/red_quilt/table.rb', line 51

def parse(parent_id, lines, index)
  start_index = index
  header_cells = Table.split_row(lines[index].content)
  row_lines = [lines[index]]
  index += 2
  while index < lines.length
    break if lines[index].blank
    break unless Table.row?(lines[index].content)

    row_lines << lines[index]
    index += 1
  end

  table_id = @arena.add_node(NodeType::TABLE,
                             source_start: lines[start_index].start_byte,
                             source_len: row_lines.last.end_byte - lines[start_index].start_byte)
  @arena.append_child(parent_id, table_id)

  append_row(table_id, lines[start_index], header_cells, true)
  row_lines.drop(1).each do |row_line|
    append_row(table_id, row_line, Table.split_row(row_line.content), false)
  end

  index
end