class Paru::PandocFilter::Table
A Table
node represents a table with an inline caption, column definition, widths, headers, and rows.
@!attribute caption
@return Caption
@!attribute attr
@return Attr
@!attribute colspec
@return ColSpec[]
@!attribute head
@return TableHead[]
@!attribute foot
@return TableHead[]
Attributes
Public Class Methods
Source
# File lib/paru/filter/table.rb, line 227 def self.create_attr() ["", [], []] end
Source
# File lib/paru/filter/table.rb, line 194 def self.create_body(data) [[ create_attr, 0, [], data.map {|r| create_row(r)} ]] end
Source
# File lib/paru/filter/table.rb, line 187 def self.create_caption(contents) [ nil, [Node.from_markdown(contents).to_ast] ] end
Source
# File lib/paru/filter/table.rb, line 217 def self.create_cell(contents) [ create_attr, {"t" => "AlignDefault", "c" => nil}, 1, 1, [Node.from_markdown(contents).to_ast] ] end
Source
# File lib/paru/filter/table.rb, line 203 def self.create_endrow(data) [ create_attr, if data.empty? then [] else [create_row(data)] end ] end
Source
# File lib/paru/filter/table.rb, line 210 def self.create_row(data) [ create_attr, data.map {|c| create_cell(c)} ] end
Source
# File lib/paru/filter/table.rb, line 133 def self.from_array(data, config = {}) table_attribute = create_attr caption = [] if config.has_key? :caption caption = create_caption config[:caption] end col_spec = data[0].map {|c| ColSpec.new.to_ast } head = create_endrow [] if config.has_key? :headers and config[:headers] head = create_endrow data.first data = data[1..-1] end foot = create_endrow [] if config.has_key? :footers and config[:footers] foot = create_endrow data.last data = data[0...-1] end body = create_body data table = [ table_attribute, caption, col_spec, head, body, foot ] Table.new table end
Create a new Table
from an 2D array and an optional configuration
@param data [String[]] an array of markdown strings @param config [Hash] configuration of the list.
properties: :headers [Boolean] True if data includes headers on first row. Defailts to false. :caption [String] The table's caption :footers [Boolean] True if data includes footers on last row, default to false.
@return [Table]
Source
# File lib/paru/filter/table.rb, line 176 def self.from_file(filename, config = {}) data = [] CSV.foreach(filename) do |row| data << row end return self.from_array(data, config) end
Create a new Table
from a CSV file.
@param filename [String] filename to read CSV data from @param config [Hash] See from_file for details
@return [Table]
Source
# File lib/paru/filter/table.rb, line 55 def initialize(contents) @attr = Attr.new contents[0] @caption = Caption.new contents[1] @colspec = contents[2].map {|p| ColSpec.new p} @head = TableHead.new contents[3] super [] contents[4].each do |table_body| @children.push TableBody.new table_body end @foot = TableFoot.new contents[5] end
Create a new Table
based on the contents
@param contents [Array]
Public Instance Methods
Source
# File lib/paru/filter/table.rb, line 70 def ast_contents() [ @attr.to_ast, @caption.to_ast, @colspec.map {|c| c.to_ast}, @head.to_ast, @children.map {|c| c.to_ast}, @foot.to_ast, ] end
The AST contents of this Table
node
@return [Array]
Source
# File lib/paru/filter/table.rb, line 89 def to_array(config = {}) headers = if config.has_key? :headers then config[:headers] else false end footers = if config.has_key? :footers then config[:footers] else false end data = [] if headers then data.concat @head.to_array end @children.each do |row| data.concat row.to_array end if footers then data.concat @foot.to_array end data end
Convert this table to a 2D table of markdown strings for each cell
@param config [Hash] configuraton of the table output
Config can contain properties :headers
@return [String[]] This Table
as a 2D array of cells represented by their markdown strings.
Source
# File lib/paru/filter/table.rb, line 114 def to_file(filename, config = {}) CSV.open(filename, "wb") do |csv| to_array(**config).each {|row| csv << row} end end
Convert this Table
to a CSV file. See to_array
for the config options
@param filename [String] filename to write to @param config [Hash] See to_array
for config options