class PseudoHiki::PlainTextFormat::TableNodeFormatter
Constants
- ERROR_MESSAGE
Public Instance Methods
deep_copy_tree(tree)
click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 201 def deep_copy_tree(tree) tree.dup.clear.tap do |new_tree| new_tree.concat tree.map {|node| node.dup } end end
each_cell_index(max_row, max_col, initial_row=0, initial_col=0) { |r, c| ... }
click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 207 def each_cell_index(max_row, max_col, initial_row=0, initial_col=0) initial_row.upto(max_row) do |r| initial_col.upto(max_col) do |c| yield r, c end end end
each_empty_cell_index(max_row, max_col, tree, table) { |r, c, cur_row| ... }
click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 181 def each_empty_cell_index(max_row, max_col, tree, table) rows = deep_copy_tree(tree) cur_row = nil each_cell_index(max_row, max_col) do |r, c| cur_row = rows.shift if c == 0 next if table[r][c] if cur_row.empty? warning_for_malformed_row(table[r]) else yield r, c, cur_row end end end
fill_expand(table, initial_row, initial_col, cur_cell)
click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 215 def fill_expand(table, initial_row, initial_col, cur_cell) row_expand, col_expand = choose_expander_of_col_and_row max_row = initial_row + cur_cell.rowspan - 1 max_col = initial_col + cur_cell.colspan - 1 each_cell_index(max_row, max_col, initial_row, initial_col) do |r, c| unless initial_row == r and initial_col == c table[r][c] = initial_row == r ? col_expand : row_expand end end end
visit(tree, memo)
click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 168 def visit(tree, memo) table = create_self_element(tree) tree.length.times { table.push create_self_element(tree) } max_col = tree.map {|row| row.reduce(0) {|sum, cell| sum + cell.colspan }}.max - 1 max_row = tree.length - 1 each_empty_cell_index(max_row, max_col, tree, table) do |r, c, cur_row| cur_cell = cur_row.shift table[r][c] = visited_result(cur_cell, memo).join.lstrip.chomp fill_expand(table, r, c, cur_cell) end format_table(table, tree) end
warning_for_malformed_row(row)
click to toggle source
# File lib/pseudohiki/plaintextformat.rb, line 195 def warning_for_malformed_row(row) message = sprintf(ERROR_MESSAGE, row.inspect) raise MalFormedTableError.new(message) if @options.strict_mode STDERR.puts message end