class Ruport::WikiTableFormatter::Markdown

This class produces Markdown table output from Ruport::Table data.

Rendering Options

:alignment: Default alignment for all columns. Allowed values are :left, :center and :right. Default is :left.

:column_alignments: Alignments for specific columns. You can configure alignments by using Hash (key: column name, value: alignment)

Public Instance Methods

apply_template() click to toggle source

Hook for setting available options using a template.

# File lib/ruport/wiki_table_formatter/markdown.rb, line 18
def apply_template
  apply_table_format_template(template.table)
end
build_table_body() click to toggle source

Generates body of Markdown table data. Following characters will be replaced as escape.

  • | -> |

  • newline code(\n) -> <br>

# File lib/ruport/wiki_table_formatter/markdown.rb, line 37
def build_table_body
  body =
    if data.column_names && !data.column_names.empty?
      data
    else
      data[1..-1]
    end
  body.each { |row| build_md_row(output, row) }
end
build_table_header() click to toggle source

Uses the column names from the given Data::Table to generate a table header. If no column names are given, first row will be treated as table header.

# File lib/ruport/wiki_table_formatter/markdown.rb, line 26
def build_table_header
  names = column_names(data)
  build_md_row(output, names)
  build_md_row(output, alignment_strings(names))
end

Private Instance Methods

alignment_string(column_name) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 73
def alignment_string(column_name)
  case column_alignment(column_name)
  when :right
    '--:'
  when :center
    ':-:'
  else
    ':--'
  end
end
alignment_strings(column_names) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 69
def alignment_strings(column_names)
  column_names.map(&method(:alignment_string))
end
apply_table_format_template(template) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 94
def apply_table_format_template(template)
  template = (template || {}).merge(options.table_format || {})
  options.alignment ||= template[:alignment]
  options.column_alignments =
    merget_column_alignments(options, template)
end
build_md_row(output, row) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 57
def build_md_row(output, row)
  output << '|'
  output << row.to_a.map { |cell| escape(+cell.to_s) }.join('|')
  output << "|\n"
end
column_alignment(column_name) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 84
def column_alignment(column_name)
  if options.column_alignments&.key?(column_name)
    options.column_alignments[column_name]
  elsif options.alignment
    options.alignment
  else
    :left
  end
end
column_names(data) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 49
def column_names(data)
  if data.column_names && !data.column_names.empty?
    data.column_names
  else
    data[0]
  end
end
escape(cell) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 63
def escape(cell)
  cell.gsub!('|', '&#124;')
  cell.gsub!("\n", '<br>')
  cell
end
merget_column_alignments(options, template) click to toggle source
# File lib/ruport/wiki_table_formatter/markdown.rb, line 101
def merget_column_alignments(options, template)
  (template[:column_alignments] || {})
    .merge(options.column_alignments || {})
end