Class: Tabula::Writers::MarkdownWriter

Inherits:
Writer
  • Object
show all
Defined in:
lib/tabula/writers/markdown_writer.rb

Overview

Writes tables in Markdown format (GitHub-flavored)

Instance Method Summary collapse

Methods inherited from Writer

to_string, write

Constructor Details

#initialize(alignment: nil, **options) ⇒ MarkdownWriter

Returns a new instance of MarkdownWriter.

Parameters:

  • alignment (Symbol) (defaults to: nil) —

    column alignment (:left, :center, :right, or nil for default)



8
9
10
11
# File 'lib/tabula/writers/markdown_writer.rb', line 8

def initialize(alignment: nil, **options)
  super(**options)
  @alignment = alignment
end

Instance Method Details

#write(tables, io) ⇒ Object

Write tables to an IO object

Parameters:

  • tables (Array<Table>) —

    tables to write

  • io (IO) —

    output destination



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tabula/writers/markdown_writer.rb', line 16

def write(tables, io)
  tables.each_with_index do |table, idx|
    # Add blank line between tables
    io.puts if idx.positive?

    rows = table.to_a
    next if rows.empty?

    col_count = rows.map(&:size).max || 0
    next if col_count.zero?

    # Write header row (first row)
    write_row(io, rows.first, col_count)

    # Write separator row
    write_separator(io, col_count)

    # Write data rows
    rows.drop(1).each do |row|
      write_row(io, row, col_count)
    end
  end
end