Module: RedQuilt::Table

Defined in:
lib/red_quilt/table.rb

Overview

GFM table detection (spec 4.10). Pure functions over line text: whether a line could be a table row and whether a header+delimiter pair starts a table. Cell splitting lives here too so the recognition rules and the splitting rules they depend on stay together. Node construction stays in BlockParser.

Defined Under Namespace

Classes: Parser

Class Method Summary collapse

Class Method Details

.row?(text) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/red_quilt/table.rb', line 30

def row?(text)
  text.include?("|")
end

.split_row(text) ⇒ Object



34
35
36
37
38
39
# File 'lib/red_quilt/table.rb', line 34

def split_row(text)
  body = text.strip
  body = body[1..] if body.start_with?("|")
  body = body[0...-1] if body.end_with?("|")
  body.split("|", -1)
end

.start?(lines, index) ⇒ Boolean

True when lines / lines form a header + delimiter pair that starts a GFM table.

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/red_quilt/table.rb', line 14

def start?(lines, index)
  return false if index + 1 >= lines.length
  return false unless row?(lines[index].content)

  header_cells = split_row(lines[index].content)
  separators = split_row(lines[index + 1].content)
  return false if separators.empty?

  # GFM spec: separator row must have valid delimiters AND match header column count.
  # "The header row must match the delimiter row in the number of cells.
  #  If not, a table will not be recognized."
  return false unless header_cells.length == separators.length

  separators.all? { |cell| cell.strip.match?(/\A:?-+:?\z/) }
end