class RgGen::Markdown::Utility::TableFormatter

Public Instance Methods

format(labels, table) click to toggle source
# File lib/rggen/markdown/utility/table_formatter.rb, line 9
def format(labels, table)
  code_block do |code|
    build_md_lines(labels, table).each_with_index do |line, i|
      code << nl if i.positive?
      code << line
    end
  end
end

Private Instance Methods

alignment_marks(size) click to toggle source
# File lib/rggen/markdown/utility/table_formatter.rb, line 44
def alignment_marks(size)
  Array.new(size, ':--')
end
build_md_line(lines, row) click to toggle source
# File lib/rggen/markdown/utility/table_formatter.rb, line 26
def build_md_line(lines, row)
  lines <<
    ['', *row]
      .map(&method(:escape))
      .zip(separators(row.size + 1))
      .flatten
end
build_md_lines(labels, table) click to toggle source
# File lib/rggen/markdown/utility/table_formatter.rb, line 20
def build_md_lines(labels, table)
  [
    labels, alignment_marks(labels.size), *table
  ].each_with_object([]) { |row, lines| build_md_line(lines, row) }
end
escape(cell) click to toggle source
# File lib/rggen/markdown/utility/table_formatter.rb, line 34
def escape(cell)
  cell.to_s
    .gsub('|', '&#124;')
    .gsub("\n", '<br>')
end
separators(size) click to toggle source
# File lib/rggen/markdown/utility/table_formatter.rb, line 40
def separators(size)
  Array.new(size, '|')
end