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.

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

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

  # smell 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) 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 103
def assign_column_widths width_base = nil
  pf = 10.0 ** 4 # precision factor (multipler / divisor) for managing precision of calculated result
  total_width = col_pcwidth = 0

  if width_base
    @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 79
def create_columns colspecs
  cols = []
  width_base = 0
  colspecs.each do |colspec|
    width_base += colspec['width']
    cols << (Column.new self, cols.size, colspec)
  end
  unless (@columns = cols).empty?
    @attributes['colcount'] = cols.size
    assign_column_widths(width_base == 0 ? nil : width_base)
  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 72
def header_row?
  @has_header_option && @rows.body.empty?
end