Class: Jekyll::DatabaseTables::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-database-tables/converter.rb

Overview

Scans document content and replaces Markdown pipe tables with database-style terminal table HTML.

Lines inside fenced blocks (+“‘+ or ~~~) are never treated as tables, regardless of whether they contain pipe characters.

Constant Summary collapse

FENCE_PATTERN =
/^\s*(`{3,}|~{3,})/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter: 'psql') ⇒ Converter

Returns a new instance of Converter.



22
23
24
# File 'lib/jekyll-database-tables/converter.rb', line 22

def initialize(formatter: 'psql')
  @formatter = formatter
end

Class Method Details

.call(content, formatter: 'psql') ⇒ String

Convenience method - equivalent to Converter.new(formatter:).call(content).

Parameters:

  • content (String)

    the raw document content

  • formatter (String) (defaults to: 'psql')

    the formatter to use (default: ‘psql’)

Returns:

  • (String)

    the content with any Markdown tables converted



18
19
20
# File 'lib/jekyll-database-tables/converter.rb', line 18

def self.call(content, formatter: 'psql')
  new(formatter:).call(content)
end

Instance Method Details

#call(content) ⇒ String

Scans content line by line, buffering pipe-containing lines and flushing the buffer through Parser and Formatter whenever a non-pipe line (or end of input) is reached.

Parameters:

  • content (String)

    the raw document content

Returns:

  • (String)

    the content with any Markdown tables converted



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-database-tables/converter.rb', line 32

def call(content)
  lines = content.lines
  result = +''
  buffer = []
  fenced = false

  lines.each do |line|
    if fenced || !line.include?('|')
      flush_buffer(result, buffer, line)
      fenced = !fenced if fence_line?(line)
    else
      buffer << line
    end
  end

  flush_buffer(result, buffer)
  result
end