Module: Jekyll::DatabaseTables::Parser

Defined in:
lib/jekyll-database-tables/parser.rb

Overview

Parsers an array of Markdown table lines into a Table.

Accepts both fenced (‘| col |`) and unfenced (`col |`) pipe styles. Returns nil for any input that does not constitute a valid table.

Constant Summary collapse

SEPARATOR_PATTERN =

Matches a GFM table separator line.

/^\|?[\s:-]+\|/

Class Method Summary collapse

Class Method Details

.parse(lines) ⇒ Table?

Parses a buffer of lines into a Table.

Expects at least two lines where the second matches SEPARATOR_PATTERN. Any additional lines are treated as data rows.

Parameters:

  • lines (Array<String>)

    raw lines from the document buffer

Returns:

  • (Table, nil)

    the parsed table, or nil if the input isn’t valid



22
23
24
25
26
27
28
29
30
# File 'lib/jekyll-database-tables/parser.rb', line 22

def parse(lines)
  return if lines.length < 2
  return unless lines[1].match?(SEPARATOR_PATTERN)

  headers = split_cells(lines[0])
  rows    = lines.drop(2).map { |line| Row.new(split_cells(line)) }

  Table.new(headers:, rows:)
end

.split_cells(line) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/jekyll-database-tables/parser.rb', line 32

def split_cells(line)
  line
    .strip
    .delete_prefix('|')
    .delete_suffix('|')
    .split('|')
    .map(&:strip)
end