class Asciidoctor::Table

Public: Methods and constants for managing AsciiDoc table content in a document. It supports all three of AsciiDoc’s table formats: psv, dsv and csv.

Constants

DEFAULT_PRECISION

precision of column widths

Attributes

caption[R]

Public: Get the caption for this table

columns[RW]

Public: Get/Set the columns for this table

has_header_option[RW]

Public: Boolean specifies whether this table has a header row

rows[RW]

Public: Get/Set the Rows struct for this table (encapsulates head, foot and body rows)

Public Class Methods

new(parent, attributes) click to toggle source
Calls superclass method Asciidoctor::AbstractBlock::new
# File lib/asciidoctor/table.rb, line 56
def initialize parent, attributes
  super parent, :table
  @rows = Rows.new
  @columns = []

  @has_header_option = false

  # smells like we need a utility method here
  # to resolve an integer width from potential bogus input
  if (pcwidth = attributes['width'])
    if (pcwidth_intval = pcwidth.to_i) > 100 || pcwidth_intval < 1
      pcwidth_intval = 100 unless pcwidth_intval == 0 && (pcwidth == '0' || pcwidth == '0%')
    end
  else
    pcwidth_intval = 100
  end
  @attributes['tablepcwidth'] = pcwidth_intval

  if @document.attributes['pagewidth']
    @attributes['tableabswidth'] = (abswidth_val = (((pcwidth_intval / 100.0) * @document.attributes['pagewidth'].to_f).truncate DEFAULT_PRECISION)) == abswidth_val.to_i ? abswidth_val.to_i : abswidth_val
  end

  @attributes['orientation'] = 'landscape' if attributes['rotate-option']
end

Public Instance Methods

assign_column_widths(width_base = nil, autowidth_cols = nil) click to toggle source

Internal: Assign column widths to columns

This method rounds the percentage width values to 4 decimal places and donates the balance to the final column.

This method assumes there’s at least one column in the columns array.

width_base - the total of the relative column values used for calculating percentage widths (default: nil)

returns nothing

# File lib/asciidoctor/table.rb, line 121
def assign_column_widths width_base = nil, autowidth_cols = nil
  precision = DEFAULT_PRECISION
  total_width = col_pcwidth = 0

  if width_base
    if autowidth_cols
      if width_base > 100
        autowidth = 0
        logger.warn %(total column width must not exceed 100% when using autowidth columns; got #{width_base}%)
      else
        autowidth = ((100.0 - width_base) / autowidth_cols.size).truncate precision
        autowidth = autowidth.to_i if autowidth.to_i == autowidth
        width_base = 100
      end
      autowidth_attrs = { 'width' => autowidth, 'autowidth-option' => '' }
      autowidth_cols.each {|col| col.update_attributes autowidth_attrs }
    end
    @columns.each {|col| total_width += (col_pcwidth = col.assign_width nil, width_base, precision) }
  else
    col_pcwidth = (100.0 / @columns.size).truncate precision
    col_pcwidth = col_pcwidth.to_i if col_pcwidth.to_i == col_pcwidth
    @columns.each {|col| total_width += col.assign_width col_pcwidth, nil, precision }
  end

  # donate balance, if any, to final column (using half up rounding)
  @columns[-1].assign_width(((100 - total_width + col_pcwidth).round precision), nil, precision) unless total_width == 100

  nil
end
create_columns(colspecs) click to toggle source

Internal: Creates the Column objects from the column spec

returns nothing

# File lib/asciidoctor/table.rb, line 90
def create_columns colspecs
  cols = []
  autowidth_cols = nil
  width_base = 0
  colspecs.each do |colspec|
    colwidth = colspec['width']
    cols << (Column.new self, cols.size, colspec)
    if colwidth < 0
      (autowidth_cols ||= []) << cols[-1]
    else
      width_base += colwidth
    end
  end
  if (num_cols = (@columns = cols).size) > 0
    @attributes['colcount'] = num_cols
    width_base = nil unless width_base > 0 || autowidth_cols
    assign_column_widths width_base, autowidth_cols
  end
  nil
end
header_row?() click to toggle source

Internal: Returns the current state of the header option (true or :implicit) if the row being processed is (or is assumed to be) the header row, otherwise nil

# File lib/asciidoctor/table.rb, line 83
def header_row?
  (val = @has_header_option) && @rows.body.empty? ? val : nil
end