summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2020-10-30 18:40:32 -0600
committerGitHub <noreply@github.com>2020-10-30 18:40:32 -0600
commitca2ca428aacfcde709db739bc3cf94beb21b7692 (patch)
treeb454716be790f3750bb250bc47ab791cdbd6c951 /lib
parentb765ec86a6367d871d8ac4e223ecb4dced18d975 (diff)
resolves #3760 apply text formatting to table cells in header row when column has a or l style (PR #3790)
Diffstat (limited to 'lib')
-rw-r--r--lib/asciidoctor/parser.rb20
-rw-r--r--lib/asciidoctor/table.rb51
2 files changed, 50 insertions, 21 deletions
diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb
index 29bfa06c..efc5557f 100644
--- a/lib/asciidoctor/parser.rb
+++ b/lib/asciidoctor/parser.rb
@@ -2281,7 +2281,8 @@ class Parser
if attributes['header-option']
table.has_header_option = true
elsif skipped == 0 && !attributes['noheader-option']
- implicit_header = true
+ # NOTE: assume table has header until we know otherwise; if it doesn't (nil), cells in first row get reprocessed
+ table.has_header_option = implicit_header = true
end
parser_ctx = Table::ParserContext.new table_reader, table, attributes
format, loop_idx, implicit_header_boundary = parser_ctx.format, -1, nil
@@ -2304,7 +2305,7 @@ class Parser
implicit_header_boundary = nil if implicit_header_boundary
# otherwise, the cell continues from previous line
elsif implicit_header_boundary && implicit_header_boundary == loop_idx
- implicit_header, implicit_header_boundary = false, nil
+ table.has_header_option = implicit_header = implicit_header_boundary = nil
end
end
end
@@ -2316,7 +2317,7 @@ class Parser
if table_reader.has_more_lines? && table_reader.peek_line.empty?
implicit_header_boundary = 1
else
- implicit_header = false
+ table.has_header_option = implicit_header = nil
end
end
end
@@ -2367,7 +2368,7 @@ class Parser
case format
when 'csv'
if parser_ctx.buffer_has_unclosed_quotes?
- implicit_header, implicit_header_boundary = false, nil if implicit_header_boundary && loop_idx == 0
+ table.has_header_option = implicit_header = implicit_header_boundary = nil if implicit_header_boundary && loop_idx == 0
parser_ctx.keep_cell_open
else
parser_ctx.close_cell true
@@ -2389,15 +2390,8 @@ class Parser
end
end
- unless (table.attributes['colcount'] ||= table.columns.size) == 0 || explicit_colspecs
- table.assign_column_widths
- end
-
- if implicit_header
- table.has_header_option = true
- attributes['header-option'] = ''
- end
-
+ table.assign_column_widths unless (table.attributes['colcount'] ||= table.columns.size) == 0 || explicit_colspecs
+ attributes['header-option'] = '' if implicit_header
table.partition_header_footer attributes
table
diff --git a/lib/asciidoctor/table.rb b/lib/asciidoctor/table.rb
index c8cce9bd..e7f202b1 100644
--- a/lib/asciidoctor/table.rb
+++ b/lib/asciidoctor/table.rb
@@ -156,11 +156,14 @@ class Table < AbstractBlock
# set rowcount before splitting up body rows
num_body_rows = @attributes['rowcount'] = (body = @rows.body).size
- if num_body_rows > 0 && @has_header_option
- @rows.head = [(head = body.shift)]
- # styles aren't applied to header row
- head.each {|c| c.style = nil }
- num_body_rows -= 1
+ 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
@rows.foot = [body.pop] if num_body_rows > 0 && attrs['footer-option']
@@ -231,9 +234,18 @@ class Table::Cell < AbstractBlock
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.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
@@ -300,8 +312,12 @@ class Table::Cell < AbstractBlock
@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
@@ -310,6 +326,25 @@ class Table::Cell < AbstractBlock
@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