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_FACTOR

multipler / divisor for tuning precision of calculated result

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 45
def initialize parent, attributes
  super parent, :table
  @rows = Rows.new
  @columns = []

  @has_header_option = attributes.key? 'header-option'

  # 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.key? 'pagewidth'
    # FIXME calculate more accurately (only used in DocBook output)
    @attributes['tableabswidth'] ||=
        ((@attributes['tablepcwidth'].to_f / 100) * @document.attributes['pagewidth']).round
  end

  attributes['orientation'] = 'landscape' if attributes.key? '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 112
def assign_column_widths width_base = nil, autowidth_cols = nil
  pf = DEFAULT_PRECISION_FACTOR
  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 * pf).to_i / pf
        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, pf) }
  else
    col_pcwidth = ((100 * pf / @columns.size).to_i) / pf
    # or...
    #col_pcwidth = (100.0 / @columns.size).truncate 4
    col_pcwidth = col_pcwidth.to_i if col_pcwidth.to_i == col_pcwidth
    @columns.each {|col| total_width += col.assign_width col_pcwidth }
  end

  # donate balance, if any, to final column (using half up rounding)
  unless total_width == 100
    @columns[-1].assign_width(((100 - total_width + col_pcwidth) * pf).round / pf)
    # or (manual half up rounding)...
    #numerator = (raw_numerator = (100 - total_width + col_pcwidth) * pf).to_i
    #numerator += 1 if raw_numerator >= numerator + 0.5
    #@columns[-1].assign_width numerator / pf
    # or...
    #@columns[-1].assign_width((100 - total_width + col_pcwidth).round 4)
  end

  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 81
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 whether the current row being processed is the header row

# File lib/asciidoctor/table.rb, line 74
def header_row?
  @has_header_option && @rows.body.empty?
end