diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 39c27b965..1928ae417 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -39,6 +39,7 @@ Improvements:: * drop support for the unmaintained payment font (`pf`) for use in font-based icons * refactor formatted text transform to simplify how inner space is collapsed; verify only inner hard breaks are preserved * extract shared `smallcaps` method from `TextTransform#smallcaps_pcdata` to make it easier to override +* assign node to `@node` instance variable on table instance to make it easer to access from Prawn::Table extension (#2471) Bug Fixes:: diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index a9dee091b..179a87afb 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -2278,6 +2278,7 @@ def convert_table node left_padding = right_padding = nil table table_data, table_settings do + instance_variable_set :@node, node # NOTE: cell_style must be applied manually to be compatible with both prawn-table 0.2.2 and prawn-table 0.2.3 cells.style cell_style @column_widths = column_widths unless column_widths.empty? diff --git a/spec/table_spec.rb b/spec/table_spec.rb index f353b222c..1a366c9f2 100644 --- a/spec/table_spec.rb +++ b/spec/table_spec.rb @@ -1764,6 +1764,53 @@ (expect to_file).to visually_match 'table-transparent-header-cell.pdf' end + + it 'should be able to access node in Prawn::Table#add_header' do + backend = nil + create_class (Asciidoctor::Converter.for 'pdf') do + register_for (backend = %(pdf#{object_id}).to_sym) + def init_pdf(*) + super + extend (Module.new do + def table data, options = {}, &block + t = Prawn::Table.new data, self, options, &block + t.extend (Module.new do + def add_header(*) + height = 0 + this_node = (instance_variable_defined? :@node) && @node # rubocop:disable RSpec/InstanceVariable + if this_node && this_node.title? + this_pdf = @pdf # rubocop:disable RSpec/InstanceVariable + title = %(#{this_node.captioned_title} (continued)) + height += (this_pdf.ink_caption title, dry_run: true) + this_pdf.ink_caption title + end + height + super + end + end) + t.draw + t + end + end) + end + end + + input = <<~END + .table title + |=== + |Column + + #{['| cell'] * 40 * ?\n} + |=== + END + + pdf = to_pdf input, backend: backend, analyze: true + (expect pdf.pages).to have_size 2 + title_text = pdf.find_unique_text page_number: 2, string: 'Table 1. table title (continued)' + (expect title_text).not_to be_nil + column_text = pdf.find_unique_text page_number: 2, string: 'Column' + (expect column_text).not_to be_nil + (expect title_text[:y] - column_text[:y]).to be > 20 + end end context 'Foot' do