summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-03-04 23:54:07 -0700
committerDan Allen <dan.j.allen@gmail.com>2022-03-05 02:24:19 -0700
commita36e5afaef6bd200cdb1bfaea55ae670ad5a2d7c (patch)
treeb32e6453bc0df4ddce096dfb61a10e1874ea4d99
parent1bec943dfad7260555b1c6079fe3d5fac4574af6 (diff)
add helper to retrieve metadata for destination
-rw-r--r--spec/admonition_spec.rb14
-rw-r--r--spec/dest_spec.rb121
-rw-r--r--spec/footnote_spec.rb7
-rw-r--r--spec/image_spec.rb24
-rw-r--r--spec/index_spec.rb10
-rw-r--r--spec/listing_spec.rb24
-rw-r--r--spec/section_spec.rb6
-rw-r--r--spec/spec_helper.rb6
8 files changed, 80 insertions, 132 deletions
diff --git a/spec/admonition_spec.rb b/spec/admonition_spec.rb
index 970b42fb..d547b565 100644
--- a/spec/admonition_spec.rb
+++ b/spec/admonition_spec.rb
@@ -30,10 +30,8 @@ describe 'Asciidoctor::PDF::Converter - Admonition' do
pdf_theme = { block_margin_top: 10 }
lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
pdf = to_pdf input, pdf_theme: pdf_theme
- names = get_names pdf
- (expect names).to have_key 'admon-1'
- dest = pdf.objects[names['admon-1']]
- (expect dest[3]).to eql lines[0][:from][:y] + 10
+ (expect (dest = get_dest pdf, 'admon-1')).not_to be_nil
+ (expect dest[:y]).to eql lines[0][:from][:y] + 10
end
it 'should keep anchor with block if block is advanced to next page' do
@@ -49,11 +47,9 @@ describe 'Asciidoctor::PDF::Converter - Admonition' do
pdf_theme = { block_margin_top: 10 }
lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
pdf = to_pdf input, pdf_theme: pdf_theme
- names = get_names pdf
- (expect names).to have_key 'admon-1'
- dest = pdf.objects[names['admon-1']]
- (expect get_page_number pdf, dest[0]).to be 2
- (expect dest[3]).to eql lines[0][:from][:y]
+ (expect (dest = get_dest pdf, 'admon-1')).not_to be_nil
+ (expect dest[:page_number]).to be 2
+ (expect dest[:y]).to eql lines[0][:from][:y]
end
it 'should vertically center label on first page if block is split across pages' do
diff --git a/spec/dest_spec.rb b/spec/dest_spec.rb
index 94607d68..a35bb534 100644
--- a/spec/dest_spec.rb
+++ b/spec/dest_spec.rb
@@ -8,8 +8,7 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
= Document Title
:doctype: book
EOS
- names = get_names pdf
- (expect names).to be_empty
+ (expect get_names pdf).to be_empty
end
it 'should define a dest named __anchor-top at top of the first body page' do
@@ -25,14 +24,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
content
EOS
- names = get_names pdf
- (expect names).to have_key '__anchor-top'
- top_dest = pdf.objects[names['__anchor-top']]
- top_page_num = get_page_number pdf, top_dest[0]
- top_y = top_dest[3]
- (expect top_page_num).to be 3
- _, page_height = get_page_size pdf, top_page_num
- (expect top_y).to eql page_height
+ (expect (top_dest = get_dest pdf, '__anchor-top')).not_to be_nil
+ (expect (top_dest[:page_number])).to be 3
+ _, page_height = get_page_size pdf, top_dest[:page_number]
+ (expect top_dest[:y]).to eql page_height
end
it 'should define a dest named toc at the top of the first toc page' do
@@ -44,14 +39,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
== Chapter
EOS
- names = get_names pdf
- (expect names).to have_key 'toc'
- toc_dest = pdf.objects[names['toc']]
- toc_page_num = get_page_number pdf, toc_dest[0]
- toc_y = toc_dest[3]
- (expect toc_page_num).to be 2
- _, page_height = get_page_size pdf, toc_page_num
- (expect toc_y).to eql page_height
+ (expect (toc_dest = get_dest pdf, 'toc')).not_to be_nil
+ (expect toc_dest[:page_number]).to be 2
+ _, page_height = get_page_size pdf, toc_dest[:page_number]
+ (expect toc_dest[:y]).to eql page_height
end
it 'should define a dest named toc at the location where the macro toc starts' do
@@ -68,14 +59,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
== Another Chapter
EOS
- names = get_names pdf
- (expect names).to have_key 'toc'
- toc_dest = pdf.objects[names['toc']]
- toc_page_num = get_page_number pdf, toc_dest[0]
- toc_y = toc_dest[3]
- (expect toc_page_num).to be 1
- _, page_height = get_page_size pdf, toc_page_num
- (expect toc_y).to be < page_height
+ (expect (toc_dest = get_dest pdf, 'toc')).not_to be_nil
+ (expect (toc_dest[:page_number])).to be 1
+ _, page_height = get_page_size pdf, toc_dest[:page_number]
+ (expect toc_dest[:y]).to be < page_height
end
it 'should use the toc macro ID as the name of the dest for the macro toc' do
@@ -93,8 +80,7 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
== Another Chapter
EOS
- names = get_names pdf
- (expect names).to have_key 'macro-toc'
+ (expect get_names pdf).to have_key 'macro-toc'
end
it 'should define a dest at the top of a chapter page' do
@@ -105,14 +91,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
== Chapter
EOS
- names = get_names pdf
- (expect names).to have_key '_chapter'
- chapter_dest = pdf.objects[names['_chapter']]
- chapter_page_num = get_page_number pdf, chapter_dest[0]
- chapter_y = chapter_dest[3]
- (expect chapter_page_num).to be 2
- _, page_height = get_page_size pdf, chapter_page_num
- (expect chapter_y).to eql page_height
+ (expect (chapter_dest = get_dest pdf, '_chapter')).not_to be_nil
+ (expect (chapter_dest[:page_number])).to be 2
+ _, page_height = get_page_size pdf, chapter_dest[:page_number]
+ (expect chapter_dest[:y]).to eql page_height
end
it 'should define a dest at the top of a part page' do
@@ -127,14 +109,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
content
EOS
- names = get_names pdf
- (expect names).to have_key '_part_1'
- part_dest = pdf.objects[names['_part_1']]
- part_page_num = get_page_number pdf, part_dest[0]
- part_y = part_dest[3]
- (expect part_page_num).to be 2
- _, page_height = get_page_size pdf, part_page_num
- (expect part_y).to eql page_height
+ (expect (part_dest = get_dest pdf, '_part_1')).not_to be_nil
+ (expect (part_dest[:page_number])).to be 2
+ _, page_height = get_page_size pdf, part_dest[:page_number]
+ (expect part_dest[:y]).to eql page_height
end
it 'should define a dest at the top of page for section if section is at top of page' do
@@ -150,14 +128,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
content
EOS
- names = get_names pdf
- (expect names).to have_key '_section'
- sect_dest = pdf.objects[names['_section']]
- sect_page_num = get_page_number pdf, sect_dest[0]
- sect_y = sect_dest[3]
- (expect sect_page_num).to be 2
- _, page_height = get_page_size pdf, sect_page_num
- (expect sect_y).to eql page_height
+ (expect (sect_dest = get_dest pdf, '_section')).not_to be_nil
+ (expect (sect_dest[:page_number])).to be 2
+ _, page_height = get_page_size pdf, sect_dest[:page_number]
+ (expect sect_dest[:y]).to eql page_height
end
it 'should define a dest at the top of content area if page does not start with a section' do
@@ -168,14 +142,10 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
content
EOS
- names = get_names pdf
- (expect names).to have_key 'p1'
- p1_dest = pdf.objects[names['p1']]
- p1_page_num = get_page_number pdf, p1_dest[0]
- p1_y = p1_dest[3]
- (expect p1_page_num).to be 1
- _, page_height = get_page_size pdf, p1_page_num
- (expect p1_y).to eql page_height - 15
+ (expect (para_dest = get_dest pdf, 'p1')).not_to be_nil
+ (expect (para_dest[:page_number])).to be 1
+ _, page_height = get_page_size pdf, para_dest[:page_number]
+ (expect para_dest[:y]).to eql page_height - 15
end
it 'should register dest for every block that has an ID' do
@@ -185,8 +155,7 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
All views expressed are my own.
EOS
- names = get_names pdf
- (expect names).to have_key 'disclaimer'
+ (expect get_names pdf).to have_key 'disclaimer'
end
end
@@ -200,8 +169,7 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
|===
EOS
- names = get_names pdf
- (expect names).to have_key 'props'
+ (expect get_names pdf).to have_key 'props'
end
it 'should register dest for media macro that has an ID' do
@@ -216,8 +184,7 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
#{macro_name == :svg ? 'image' : macro_name.to_s}::#{target}[]
EOS
- names = get_names pdf
- (expect names).to have_key 'media'
+ (expect get_names pdf).to have_key 'media'
end
end
@@ -373,10 +340,8 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
https://asciidoctor.org[Asciidoctor,id=link]
EOS
- dests = get_names pdf
- (expect dests).to have_key 'link'
- link_dest_page = pdf.objects[dests['link']][0]
- (expect get_page_number pdf, link_dest_page).to eql 2
+ (expect (link_dest = get_dest pdf, 'link')).not_to be_nil
+ (expect link_dest[:page_number]).to be 2
end
it 'should hex encode name for ID that contains non-ASCII characters' do
@@ -398,11 +363,8 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
Here are all the #{details}.
EOS
- names = get_names pdf
- (expect names).to have_key 'details'
- details_dest = pdf.objects[names['details']]
- details_dest_pagenum = get_page_number pdf, details_dest[0]
- (expect details_dest_pagenum).to be 2
+ (expect (phrase_dest = get_dest pdf, 'details')).not_to be_nil
+ (expect phrase_dest[:page_number]).to be 2
end
end
@@ -415,12 +377,9 @@ describe 'Asciidoctor::PDF::Converter - Dest' do
#{(['paragraph'] * 16).join ' '} [#anchor]#supercalifragilisticexpialidocious#
EOS
- names = get_names pdf
- (expect names).to have_key 'anchor'
- anchor_dest = pdf.objects[names['anchor']]
- anchor_dest_pagenum = get_page_number pdf, anchor_dest[0]
- (expect anchor_dest_pagenum).to be 2
- (expect (pdf.page 2).text).to eql 'supercalifragilisticexpialidocious'
+ (expect (phrase_dest = get_dest pdf, 'anchor')).not_to be_nil
+ (expect phrase_dest[:page_number]).to be 2
+ (expect (pdf.page phrase_dest[:page_number]).text).to eql 'supercalifragilisticexpialidocious'
end
it 'should not allocate space for anchor if font is missing glyph for null character' do
diff --git a/spec/footnote_spec.rb b/spec/footnote_spec.rb
index 40227346..f9c9ae73 100644
--- a/spec/footnote_spec.rb
+++ b/spec/footnote_spec.rb
@@ -311,9 +311,10 @@ describe 'Asciidoctor::PDF::Converter - Footnote' do
(expect annotations).to have_size 2
footnote_label_y = annotations[0][:Rect][3]
footnote_item_y = annotations[1][:Rect][3]
- names = get_names pdf
- (expect footnote_label_y - pdf.objects[names['_footnoteref_1']][3]).to be < 1
- (expect pdf.objects[names['_footnotedef_1']][3]).to eql footnote_item_y
+ (expect (footnoteref_dest = get_dest pdf, '_footnoteref_1')).not_to be_nil
+ (expect footnote_label_y - footnoteref_dest[:y]).to be < 1
+ (expect (footnotedef_dest = get_dest pdf, '_footnotedef_1')).not_to be_nil
+ (expect footnotedef_dest[:y]).to eql footnote_item_y
end
it 'should render footnotes in table cell that are directly adjacent to text' do
diff --git a/spec/image_spec.rb b/spec/image_spec.rb
index a1ad8350..4944b3ad 100644
--- a/spec/image_spec.rb
+++ b/spec/image_spec.rb
@@ -25,10 +25,8 @@ describe 'Asciidoctor::PDF::Converter - Image' do
images = (to_pdf input, pdf_theme: pdf_theme, analyze: :image).images
pdf = to_pdf input, pdf_theme: pdf_theme
- names = get_names pdf
- (expect names).to have_key 'tux'
- dest = pdf.objects[names['tux']]
- (expect dest[3]).to eql images[0][:y] + 10
+ (expect (image_dest = get_dest pdf, 'tux')).not_to be_nil
+ (expect image_dest[:y]).to eql images[0][:y] + 10
end
it 'should place anchor at top of block image if advanced to next page' do
@@ -43,11 +41,9 @@ describe 'Asciidoctor::PDF::Converter - Image' do
images = (to_pdf input, pdf_theme: pdf_theme, analyze: :image).images
pdf = to_pdf input, pdf_theme: pdf_theme
- names = get_names pdf
- (expect names).to have_key 'tall-diagram'
- dest = pdf.objects[names['tall-diagram']]
- (expect get_page_number pdf, dest[0]).to be 2
- (expect dest[3]).to eql images[0][:y]
+ (expect (image_dest = get_dest pdf, 'tall-diagram')).not_to be_nil
+ (expect image_dest[:page_number]).to be 2
+ (expect image_dest[:y]).to eql images[0][:y]
end
context 'imagesdir' do
@@ -1084,12 +1080,10 @@ describe 'Asciidoctor::PDF::Converter - Image' do
names = get_names pdf
(expect names).to have_size 3
(expect names.keys).to eql %w(__anchor-top _section last)
- top_dest = pdf.objects[names['__anchor-top']]
- top_page_num = get_page_number pdf, top_dest[0]
- (expect top_page_num).to be 1
- last_dest = pdf.objects[names['last']]
- last_page_num = get_page_number pdf, last_dest[0]
- (expect last_page_num).to be 4
+ (expect (top_dest = get_dest pdf, '__anchor-top')).not_to be_nil
+ (expect top_dest[:page_number]).to be 1
+ (expect (last_dest = get_dest pdf, 'last')).not_to be_nil
+ (expect last_dest[:page_number]).to be 4
annotations = get_annotations pdf
(expect annotations).to have_size 3
(expect annotations.map {|it| it[:Dest] }).to eql %w(_section __anchor-top last)
diff --git a/spec/index_spec.rb b/spec/index_spec.rb
index a67205ee..cf6d9028 100644
--- a/spec/index_spec.rb
+++ b/spec/index_spec.rb
@@ -158,12 +158,10 @@ describe 'Asciidoctor::PDF::Converter - Index' do
pdf = to_pdf input
annotations = get_annotations pdf, 3
(expect annotations).to have_size 1
- dest = annotations[0][:Dest]
- names = get_names pdf
- (expect names).to have_key dest
- (expect pdf.objects[names[dest]][2]).to eql dogs_text[:x]
- term_pgnum = get_page_number pdf, pdf.objects[pdf.objects[names[dest]][0]]
- (expect term_pgnum).to be 2
+ dest_name = annotations[0][:Dest]
+ (expect (dest = get_dest pdf, dest_name)).not_to be_nil
+ (expect dest[:x]).to eql dogs_text[:x]
+ (expect dest[:page_number]).to be 2
end
it 'should target first occurance of index term, not in xreftext' do
diff --git a/spec/listing_spec.rb b/spec/listing_spec.rb
index 623db3bb..00ec11d8 100644
--- a/spec/listing_spec.rb
+++ b/spec/listing_spec.rb
@@ -45,12 +45,9 @@ describe 'Asciidoctor::PDF::Converter - Listing' do
(expect (pdf.page 1).text).not_to include 'listing'
(expect (pdf.page 2).text).to include 'listing'
- names = get_names pdf
- (expect names).to have_key 'listing-1'
- dest = pdf.objects[names['listing-1']]
- dest_page_num = get_page_number pdf, dest[0]
- (expect dest_page_num).to be 2
- (expect dest[3]).to eql 805.89
+ (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
+ (expect dest[:page_number]).to be 2
+ (expect dest[:y]).to eql 805.89
end
it 'should place anchor above top margin of block' do
@@ -67,10 +64,9 @@ describe 'Asciidoctor::PDF::Converter - Listing' do
lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
pdf = to_pdf input, pdf_theme: pdf_theme
- names = get_names pdf
- (expect names).to have_key 'listing-1'
- dest = pdf.objects[names['listing-1']]
- (expect dest[3]).to eql lines[0][:from][:y] + 10
+ (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
+ (expect dest[:page_number]).to be 1
+ (expect dest[:y]).to eql lines[0][:from][:y] + 10
end
it 'should place anchor at top of block if advanced to next page' do
@@ -87,11 +83,9 @@ describe 'Asciidoctor::PDF::Converter - Listing' do
lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines
pdf = to_pdf input, pdf_theme: pdf_theme
- names = get_names pdf
- (expect names).to have_key 'listing-1'
- dest = pdf.objects[names['listing-1']]
- (expect get_page_number pdf, dest[0]).to be 2
- (expect dest[3]).to eql lines[0][:from][:y]
+ (expect (dest = get_dest pdf, 'listing-1')).not_to be_nil
+ (expect dest[:page_number]).to be 2
+ (expect dest[:y]).to eql lines[0][:from][:y]
end
it 'should split block if it cannot fit on a whole page' do
diff --git a/spec/section_spec.rb b/spec/section_spec.rb
index 53ccb9dd..aff9c5ab 100644
--- a/spec/section_spec.rb
+++ b/spec/section_spec.rb
@@ -806,9 +806,9 @@ describe 'Asciidoctor::PDF::Converter - Section' do
EOS
pdf = to_pdf input
- names = get_names pdf
- (expect names).to have_key '_prelude'
- (expect pdf.objects[names['_prelude']][3]).to eql (get_page_size pdf, 2)[1]
+ (expect (preface_dest = get_dest pdf, '_prelude')).not_to be_nil
+ _, page_height = get_page_size pdf, preface_dest[:page_number]
+ (expect preface_dest[:y]).to eql page_height
text = (to_pdf input, analyze: true).text
(expect text[1][:string]).to eql 'Prelude'
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index b51b96c1..81029fa5 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -485,6 +485,12 @@ RSpec.configure do |config|
end
end
+ def get_dest pdf, name
+ if (name_ref = (get_names pdf)[name]) && (dest = pdf.objects[name_ref])
+ { page: pdf.objects[(page_ref = dest[0])], page_number: (get_page_number pdf, page_ref), x: dest[2], y: dest[3] }
+ end
+ end
+
def get_page_labels pdf
objects = pdf.objects
Hash[*objects[pdf.catalog[:PageLabels]][:Nums]].each_with_object([]) {|(idx, val), accum| accum[idx] = val[:P] }