Class: Coradoc::Markdown::Serializer::Serializers::Table

Inherits:
ElementSerializer show all
Defined in:
lib/coradoc/markdown/serializer/serializers/table.rb

Instance Method Summary collapse

Methods inherited from ElementSerializer

call, handles?, #handles?, handles_type, #handles_type

Instance Method Details

#call(element, _ctx) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/coradoc/markdown/serializer/serializers/table.rb', line 12

def call(element, _ctx)
  cols = [element.headers.size, *element.rows.map { |r| row_cells(r).size }].max
  return '' if cols.zero?

  headers = element.headers.empty? ? Array.new(cols, '') : element.headers
  header_row = "| #{headers.join(' | ')} |"
  separator = "| #{headers.map { '---' }.join(' | ')} |"
  rows = element.rows.map do |row|
    cells = row_cells(row).fill('', row_cells(row).size...cols)
    "| #{cells.join(' | ')} |"
  end

  [header_row, separator, *rows].join("\n")
end