class PseudoHiki::MarkDownFormat::TableNodeFormatter

Public Instance Methods

calculate_cell_width(table) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 355
def calculate_cell_width(table)
  cell_width = Array.new(table.first.length, 0)
  table.each do |row|
    row.each_with_index do |cell, i|
      cell_width[i] = cell.length if cell_width[i] < cell.length
    end
  end
  cell_width
end
check_conformance_with_gfm_style(rows) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 365
def check_conformance_with_gfm_style(rows)
  rows.each_with_index do |row, i|
    row.each do |cell|
      return false if cell.rowspan > 1 or cell.colspan > 1
      # A table head row should be at the beginning and composed of <th>
      # elements, and other rows should not include <th> elements
      return false unless (i == 0) == (cell.cell_type == "th")
    end
  end
  true
end
choose_expander_of_col_and_row() click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 309
def choose_expander_of_col_and_row
  ["", ""]
end
format_gfm_table(table) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 313
def format_gfm_table(table)
  cell_width = calculate_cell_width(table)
  header_delimiter = cell_width.map {|width| "-" * width }
  cell_formats = cell_width.map {|width| "%-#{width}s" }
  table[1, 0] = [header_delimiter]
  table.map do |row|
    formatted_row = row.zip(cell_formats).map do |cell, format_str|
      sprintf(format_str, cell)
    end
    "|#{formatted_row.join("|") }|#{$/}"
  end.join + $/
end
format_html_table(tree) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 326
def format_html_table(tree)
  table = HtmlElement.create("table").tap do |element|
    element.push HtmlFormat.format(tree)
  end.to_s
  @formatter[PlainNode].remove_trailing_newlines_in_html_element(table)
end
format_table(table, tree) click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 333
def format_table(table, tree)
  return format_html_table(tree) unless @options.gfm_style
  return format_gfm_table(table) if @options.gfm_conformant

  if @options.gfm_style == :force
    warning_for_non_comformant_style
    format_gfm_table(table)
  else
    format_html_table(tree)
  end
end
visit(tree, memo) click to toggle source
Calls superclass method
# File lib/pseudohiki/markdownformat.rb, line 304
def visit(tree, memo)
  @options.gfm_conformant = check_conformance_with_gfm_style(tree)
  super(tree, memo)
end
warning_for_non_comformant_style() click to toggle source
# File lib/pseudohiki/markdownformat.rb, line 345
      def warning_for_non_comformant_style
        warning_message = <<ERROR
The table is not conformant to GFM style.
The first row will be treated as a header row.
ERROR
        raise NotConformantStyleError.new(warning_message)
      rescue
        STDERR.puts warning_message
      end