lib/asciidoctor/table.rb in asciidoctor-2.0.10 vs lib/asciidoctor/table.rb in asciidoctor-2.0.11
- old
+ new
@@ -56,11 +56,11 @@
def initialize parent, attributes
super parent, :table
@rows = Rows.new
@columns = []
- @has_header_option = attributes['header-option'] ? true : false
+ @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
@@ -152,26 +152,23 @@
# by the options on the table
#
# returns nothing
def partition_header_footer(attrs)
# set rowcount before splitting up body rows
- @attributes['rowcount'] = @rows.body.size
+ num_body_rows = @attributes['rowcount'] = (body = @rows.body).size
- num_body_rows = @rows.body.size
- if num_body_rows > 0 && @has_header_option
- head = @rows.body.shift
- num_body_rows -= 1
- # styles aren't applied to header row
- head.each {|c| c.style = nil }
- # QUESTION why does AsciiDoc use an array for head? is it
- # possible to have more than one based on the syntax?
- @rows.head = [head]
+ if num_body_rows > 0
+ if @has_header_option
+ @rows.head = [body.shift.map {|cell| cell.reinitialize true }]
+ num_body_rows -= 1
+ elsif @has_header_option.nil?
+ @has_header_option = false
+ body.unshift(body.shift.map {|cell| cell.reinitialize false })
+ end
end
- if num_body_rows > 0 && attrs['footer-option']
- @rows.foot = [@rows.body.pop]
- end
+ @rows.foot = [body.pop] if num_body_rows > 0 && attrs['footer-option']
nil
end
end
@@ -230,18 +227,27 @@
attr_accessor :rowspan
# Public: An alias to the parent block (which is always a Column)
alias column parent
- # Internal: Returns the nested Document in an AsciiDoc table cell (only set when style is :asciidoc)
+ # Public: Returns the nested Document in an AsciiDoc table cell (only set when style is :asciidoc)
attr_reader :inner_document
def initialize column, cell_text, attributes = {}, opts = {}
super column, :table_cell
+ @cursor = @reinitialize_args = nil
@source_location = opts[:cursor].dup if @document.sourcemap
+ # NOTE: column is always set when parsing; may not be set when building table from the API
if column
- cell_style = column.attributes['style'] unless (in_header_row = column.table.header_row?)
+ if (in_header_row = column.table.header_row?)
+ if (cell_style = column.style || attributes&.[]('style')) == :asciidoc || cell_style == :literal
+ @reinitialize_args = [column, cell_text, attributes&.merge, opts]
+ end
+ cell_style = nil
+ else
+ cell_style = column.style
+ end
# REVIEW feels hacky to inherit all attributes from column
update_attributes column.attributes
end
# NOTE if attributes is defined, we know this is a psv cell; implies text needs to be stripped
if attributes
@@ -304,20 +310,43 @@
@subs = nil
elsif literal
@content_model = :verbatim
@subs = BASIC_SUBS
else
- if normal_psv && (cell_text.start_with? '[[') && LeadingInlineAnchorRx =~ cell_text
- Parser.catalog_inline_anchor $1, $2, self, opts[:cursor], @document
+ if normal_psv
+ if in_header_row
+ @cursor = opts[:cursor] # used in deferred catalog_inline_anchor call
+ else
+ catalog_inline_anchor cell_text, opts[:cursor]
+ end
end
@content_model = :simple
@subs = NORMAL_SUBS
end
@text = cell_text
@style = cell_style
end
+ def reinitialize has_header
+ if has_header
+ @reinitialize_args = nil
+ elsif @reinitialize_args
+ return Table::Cell.new(*@reinitialize_args)
+ else
+ @style = @attributes['style']
+ end
+ catalog_inline_anchor if @cursor
+ self
+ end
+
+ def catalog_inline_anchor cell_text = @text, cursor = nil
+ cursor, @cursor = @cursor, nil unless cursor
+ if (cell_text.start_with? '[[') && LeadingInlineAnchorRx =~ cell_text
+ Parser.catalog_inline_anchor $1, $2, self, cursor, @document
+ end
+ end
+
# Public: Get the String text of this cell with substitutions applied.
#
# Used for cells in the head row as well as text-only (non-AsciiDoc) cells in
# the foot row and body.
#
@@ -337,10 +366,10 @@
@text = val
end
# Public: Handles the body data (tbody, tfoot), applying styles and partitioning into paragraphs
#
- # This method should not be used for cells in the head row or that have the literal or verse style.
+ # This method should not be used for cells in the head row or that have the literal style.
#
# Returns the converted String for this Cell
def content
if (cell_style = @style) == :asciidoc
@inner_document.convert