1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
# frozen_string_literal: true
require_relative 'spec_helper'
describe 'Asciidoctor::PDF::Converter - Xref' do
context 'internal' do
it 'should create reference to a section by title' do
pdf = to_pdf <<~'EOS'
= Document Title
:doctype: book
== Chapter A
You can find details in <<Chapter B>>.
== Chapter B
Here are the details you're looking for.
EOS
names = get_names pdf
(expect names).to have_key '_chapter_a'
(expect names).to have_key '_chapter_b'
annotations = get_annotations pdf, 2
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql '_chapter_b'
(expect (pdf.page 2).text).to include 'Chapter B'
end
it 'should create reference to a section by implicit ID' do
pdf = to_pdf <<~'EOS'
= Document Title
:doctype: book
== Chapter A
You can find details in <<_chapter_b>>.
== Chapter B
Here are the details you're looking for.
EOS
names = get_names pdf
(expect names).to have_key '_chapter_a'
(expect names).to have_key '_chapter_b'
annotations = get_annotations pdf, 2
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql '_chapter_b'
(expect (pdf.page 2).text).to include 'Chapter B'
end
it 'should create reference to a section by explicit ID' do
pdf = to_pdf <<~'EOS'
= Document Title
:doctype: book
[#a]
== Chapter A
You can find details in <<b>>.
[#b]
== Chapter B
Here are the details you're looking for.
EOS
names = get_names pdf
(expect names).to have_key 'a'
(expect names).to have_key 'b'
annotations = get_annotations pdf, 2
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'b'
(expect (pdf.page 2).text).to include 'Chapter B'
end
it 'should short-circuit circular reference in section title' do
pdf = to_pdf <<~'EOS', analyze: true
[#a]
== A <<b>>
[#b]
== B <<a>>
EOS
(expect pdf.lines).to eql ['A B [a]', 'B [a]']
lines = pdf.text.map {|it| it[:y] }.uniq
(expect pdf.find_unique_text 'B [a]', font_color: '428BCA', y: lines[0]).not_to be_nil
(expect pdf.find_unique_text '[a]', font_color: '428BCA', y: lines[1]).not_to be_nil
end
it 'should reference section with ID that contains non-ASCII characters' do
pdf = to_pdf <<~'EOS'
== Über Étudier
See <<_über_étudier>>.
EOS
hex_encoded_id = %(0x#{('_über_étudier'.unpack 'H*')[0]})
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql hex_encoded_id
(expect (pdf.page 1).text).to include 'See Über Étudier.'
end
it 'should create reference to a block by explicit ID' do
pdf = to_pdf <<~'EOS'
= Document Title
:doctype: book
== Summary
You can find the observed values in the <<observed-values,table>>.
== Data
[#observed-values]
|===
| Subject | Count
| foo
| 2
| bar
| 1
|===
EOS
names = get_names pdf
(expect names).to have_key 'observed-values'
annotations = get_annotations pdf, 2
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'observed-values'
(expect (pdf.page 2).text).to include 'table'
end
it 'should create reference to an anchor in a paragraph' do
pdf = to_pdf <<~'EOS'
Jump to the <<explanation>>.
<<<
[[explanation,explanation]]This is the explanation.
EOS
names = get_names pdf
(expect names).to have_key 'explanation'
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'explanation'
(expect (pdf.page 1).text).to include 'explanation'
end
it 'should create reference to a list item with an anchor' do
pdf = to_pdf <<~'EOS'
Jump to the <<first-item>>.
<<<
* [[first-item,first item]]list item
EOS
names = get_names pdf
(expect names).to have_key 'first-item'
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'first-item'
(expect (pdf.page 1).text).to include 'first item'
end
it 'should create reference to a table cell with an anchor' do
pdf = to_pdf <<~'EOS'
Jump to the <<first-cell>>.
<<<
|===
|[[first-cell,first cell]]table cell
|===
EOS
names = get_names pdf
(expect names).to have_key 'first-cell'
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'first-cell'
(expect (pdf.page 1).text).to include 'first cell'
end
it 'should show ID enclosed in square brackets if reference cannot be resolved' do
pdf = to_pdf <<~'EOS'
Road to <<nowhere>>.
EOS
(expect (pdf.page 1).text).to eql 'Road to [nowhere].'
names = get_names pdf
(expect names).not_to have_key 'nowhere'
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'nowhere'
end
end
context 'interdocument' do
it 'should convert interdocument xref to PDF link' do
input_file = Pathname.new fixture_file 'reference-to-sibling.adoc'
pdf = to_pdf input_file
p2_annotations = get_annotations pdf, 2
(expect p2_annotations).to have_size 2
book_ref = p2_annotations[0]
(expect book_ref[:Subtype]).to be :Link
(expect book_ref[:A][:S]).to eql :URI
(expect book_ref[:A][:URI]).to eql 'book.pdf'
end
it 'should convert deep interdocument xref to PDF link with fragment' do
input_file = Pathname.new fixture_file 'reference-to-sibling.adoc'
pdf = to_pdf input_file
p2_annotations = get_annotations pdf, 2
(expect p2_annotations).to have_size 2
first_steps_ref = p2_annotations[1]
(expect first_steps_ref[:Subtype]).to be :Link
(expect first_steps_ref[:A][:S]).to eql :URI
(expect first_steps_ref[:A][:URI]).to eql 'book.pdf#_first_steps'
end
it 'should use path as fallback text for interdocument xref' do
pdf = to_pdf 'Refer to the xref:admin-guide.adoc[] to learn how to configure the system.'
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
admin_guide_ref = annotations[0]
(expect admin_guide_ref[:Subtype]).to be :Link
(expect admin_guide_ref[:A][:S]).to eql :URI
(expect admin_guide_ref[:A][:URI]).to eql 'admin-guide.pdf'
(expect (pdf.page 1).text).to eql 'Refer to the admin-guide.pdf to learn how to configure the system.'
end
it 'should convert interdocument xrefs included in current document to internal references' do
input_file = Pathname.new fixture_file 'book.adoc'
pdf = to_pdf input_file
p2_annotations = get_annotations pdf, 2
(expect p2_annotations).to have_size 1
chapter_2_ref = p2_annotations[0]
(expect chapter_2_ref[:Subtype]).to be :Link
(expect chapter_2_ref[:Dest]).to eql '_chapter_2'
p3_annotations = get_annotations pdf, 3
first_steps_ref = p3_annotations[0]
(expect first_steps_ref[:Subtype]).to be :Link
(expect first_steps_ref[:Dest]).to eql '_first_steps'
end
it 'should link self-referencing interdocument xref with text to built-in __anchor-top ref' do
pdf = to_pdf Pathname.new fixture_file 'reference-to-self.adoc'
(expect Pathname.new output_file 'reference-to-self.pdf').to exist
annotations = get_annotations pdf
(expect annotations).to have_size 2
(expect annotations[0][:Dest]).to eql '__anchor-top'
(expect (pdf.page 3).text).to eql 'go to top'
end
it 'should link self-referencing interdocument xref without text to built-in __anchor-top ref' do
pdf = to_pdf Pathname.new fixture_file 'reference-to-self.adoc'
(expect Pathname.new output_file 'reference-to-self.pdf').to exist
annotations = get_annotations pdf
(expect annotations).to have_size 2
(expect annotations[1][:Dest]).to eql '__anchor-top'
(expect (pdf.page 4).text).to eql '[^top]'
end
end
context 'xrefstyle' do
it 'should refer to part by label and number when xrefstyle is short' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:partnums:
:xrefstyle: short
= Beginner
== Basic Lesson
Now you are ready for <<_advanced>>!
= Advanced
== Advanced Lesson
If you are so advanced, why do you even need a lesson?
EOS
(expect pdf.lines).to include 'Now you are ready for Part II!'
end
it 'should refer to part by name when xrefstyle is basic' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:partnums:
:xrefstyle: basic
= Beginner
== Basic Lesson
Now you are ready for <<_advanced>>!
= Advanced
== Advanced Lesson
If you are so advanced, why do you even need a lesson?
EOS
(expect pdf.lines).to include 'Now you are ready for Advanced!'
end
it 'should refer to part by label, number, and title when xrefstyle is full' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:partnums:
:xrefstyle: full
= Beginner
== Basic Lesson
Now you are ready for <<_advanced>>!
= Advanced
== Advanced Lesson
If you are so advanced, why do you even need a lesson?
EOS
(expect pdf.lines).to include 'Now you are ready for Part II, “Advanced”!'
end
it 'should refer to chapter by label and number when xrefstyle is short' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:sectnums:
:xrefstyle: short
Start with <<_a>>.
== A
EOS
(expect pdf.lines).to include 'Start with Chapter 1.'
end
it 'should refer to chapter title and number when xrefstyle is basic' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:sectnums:
:xrefstyle: basic
Start with <<_a>>.
== A
EOS
(expect pdf.lines).to include 'Start with A.'
end
it 'should refer to chapter label, number and title when xrefstyle is full' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:sectnums:
:xrefstyle: full
Start with <<_a>>.
== A
EOS
(expect pdf.lines).to include 'Start with Chapter 1, A.'
end
it 'should use xrefstyle specified on xref macro' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:sectnums:
:xrefstyle: short
Start with xref:_a[xrefstyle=full].
== A
EOS
(expect pdf.lines).to include 'Start with Chapter 1, A.'
end
it 'should refer to image with title by title by default' do
pdf = to_pdf <<~'EOS', analyze: true
See <<img>>.
.Title of Image
[#img]
image::tux.png[]
EOS
(expect pdf.lines[0]).to eql 'See Title of Image.'
end
it 'should refer to image with title by reference signifier, number, and title when xrefstyle is full' do
pdf = to_pdf <<~'EOS', analyze: true
:xrefstyle: full
See <<img>>.
.Title of Image
[#img]
image::tux.png[]
EOS
(expect pdf.lines[0]).to eql 'See Figure 1, “Title of Image”.'
end
it 'should refer to image with title by reference signifier and number when xrefstyle is short' do
pdf = to_pdf <<~'EOS', analyze: true
:xrefstyle: short
See <<img>>.
.Title of Image
[#img]
image::tux.png[]
EOS
(expect pdf.lines[0]).to eql 'See Figure 1.'
end
it 'should show ID of reference enclosed in square brackets if reference has no xreftext' do
pdf = to_pdf <<~'EOS'
:xrefstyle: full
Jump to the <<first-item>>.
<<<
* [[first-item]]list item
EOS
names = get_names pdf
(expect names).to have_key 'first-item'
annotations = get_annotations pdf, 1
(expect annotations).to have_size 1
(expect annotations[0][:Dest]).to eql 'first-item'
(expect (pdf.page 1).text).to include '[first-item]'
end
end
end
|