Skip to content

Commit

Permalink
backport fix for #2471 assign node to instance variable on table inst…
Browse files Browse the repository at this point in the history
…ance
  • Loading branch information
mojavelinux committed Jan 22, 2024
1 parent 8882718 commit aec4cd1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Enhancements::
Improvements::

* 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::

Expand Down
1 change: 1 addition & 0 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,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?
Expand Down
47 changes: 47 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<~EOS
.table title
|===
|Column
#{['| cell'] * 40 * ?\n}
|===
EOS

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
Expand Down

0 comments on commit aec4cd1

Please sign in to comment.