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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
|
# frozen_string_literal: true
require_relative 'spec_helper'
describe 'Asciidoctor::PDF::Converter - TOC' do
context 'book' do
it 'should not generate toc by default' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
= Document Title
== Introduction
== Main
== Conclusion
EOS
(expect pdf.pages).to have_size 4
(expect pdf.find_text 'Table of Contents').to be_empty
end
it 'should insert toc between title page and first page of body when toc is set' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
= Document Title
:toc:
== Introduction
== Main
== Conclusion
EOS
(expect pdf.pages).to have_size 5
(expect pdf.find_text 'Document Title', page_number: 1).not_to be_empty
(expect pdf.find_text 'Table of Contents', page_number: 2).not_to be_empty
(expect pdf.find_text '1', page_number: 2).not_to be_empty
(expect pdf.find_text '2', page_number: 2).not_to be_empty
(expect pdf.find_text '3', page_number: 2).not_to be_empty
(expect pdf.find_text 'Introduction', page_number: 3).not_to be_empty
end
it 'should space items in toc evently even if title is entirely monospace' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
= Document Title
:toc:
== Beginning
== `Middle`
== End
EOS
(expect pdf.find_text 'Table of Contents', page_number: 2).not_to be_empty
beginning_pagenum_text = (pdf.find_text '1', page_number: 2)[0]
middle_pagenum_text = (pdf.find_text '2', page_number: 2)[0]
end_pagenum_text = (pdf.find_text '3', page_number: 2)[0]
beginning_to_middle_spacing = (beginning_pagenum_text[:y] - middle_pagenum_text[:y]).round 2
middle_to_end_spacing = (middle_pagenum_text[:y] - end_pagenum_text[:y]).round 2
(expect beginning_to_middle_spacing).to eql middle_to_end_spacing
end
it 'should only include preface in toc if preface-title is set' do
input = <<~'EOS'
= Document Title
[preface]
This is the preface.
== Chapter 1
And away we go!
EOS
[{ 'toc' => '' }, { 'toc' => '', 'preface-title' => 'Preface' }].each do |attrs|
pdf = to_pdf input, doctype: :book, attributes: attrs, analyze: :page
(expect pdf.pages).to have_size 4
(expect pdf.pages[0][:strings]).to include 'Document Title'
(expect pdf.pages[1][:strings]).to include 'Table of Contents'
if attrs.include? 'preface-title'
(expect pdf.pages[1][:strings]).to include 'Preface'
(expect pdf.pages[1][:strings]).to include '1'
else
(expect pdf.pages[1][:strings]).not_to include 'Preface'
(expect pdf.pages[1][:strings]).not_to include '1'
end
(expect pdf.pages[1][:strings]).to include 'Chapter 1'
(expect pdf.pages[1][:strings]).to include '2'
(expect pdf.pages[2][:strings]).to include 'Preface' if attrs.include? 'preface-title'
(expect pdf.pages[2][:strings]).to include 'This is the preface.'
(expect pdf.pages[3][:strings]).to include 'Chapter 1'
end
end
it 'should output toc with depth specified by toclevels' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: :page
= Document Title
:toc:
:toclevels: 1
== Level 1
=== Level 2
==== Level 3
EOS
(expect pdf.pages).to have_size 3
(expect pdf.pages[0][:strings]).to include 'Document Title'
(expect pdf.pages[1][:strings]).to include 'Table of Contents'
(expect pdf.pages[1][:strings]).to include 'Level 1'
(expect pdf.pages[1][:strings]).not_to include 'Level 2'
(expect pdf.pages[1][:strings]).not_to include 'Level 3'
(expect pdf.pages[2][:strings]).to include 'Level 1'
end
it 'should only show parts in toc if toclevels attribute is 0' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: :page
= Document Title
:toc:
:toclevels: 0
= Part One
== Chapter A
= Part Two
== Chapter B
EOS
(expect pdf.pages).to have_size 6
(expect pdf.pages[1][:strings]).to include 'Table of Contents'
(expect pdf.pages[1][:strings]).to include 'Part One'
(expect pdf.pages[1][:strings]).to include 'Part Two'
(expect pdf.pages[1][:strings]).not_to include 'Chapter A'
(expect pdf.pages[1][:strings]).not_to include 'Chapter B'
end
it 'should not show any section titles when toclevels is less than 0' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
= Document Title
:toc:
:toclevels: -1
= Part One
== Chapter A
= Part Two
== Chapter B
EOS
(expect pdf.pages).to have_size 6
toc_lines = pdf.lines pdf.find_text page_number: 2
(expect toc_lines).to eql ['Table of Contents']
end
it 'should allow section to override toclevels for descendant sections' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:toc:
:toclevels: 3
== Chapter
=== Chapter Section
==== Chapter Subsection
[appendix,toclevels=1]
== Lorem Ipsum
=== Appendix Section
==== Appendix Subsection
EOS
(expect pdf.find_text page_number: 2, string: 'Chapter').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Chapter Section').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Chapter Subsection').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Appendix A: Lorem Ipsum').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Appendix Section').to have_size 0
end
it 'should allow section to remove itself from toc by setting toclevels to less than section level' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:toc:
:toclevels: 3
== Chapter
=== Chapter Section
==== Chapter Subsection
[appendix,toclevels=0]
== Lorem Ipsum
=== Appendix Section
==== Appendix Subsection
EOS
(expect pdf.find_text page_number: 2, string: 'Chapter').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Chapter Section').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Chapter Subsection').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Appendix A: Lorem Ipsum').to have_size 0
end
it 'should reserve enough pages for toc if it spans more than one page' do
sections = (1..40).map {|num| %(\n\n=== Section #{num}) }
pdf = to_pdf <<~EOS, doctype: :book, analyze: :page
= Document Title
:toc:
== Chapter 1#{sections.join}
EOS
(expect pdf.pages).to have_size 6
(expect pdf.pages[0][:strings]).to include 'Document Title'
(expect pdf.pages[1][:strings]).to include 'Table of Contents'
(expect pdf.pages[3][:strings]).to include 'Chapter 1'
end
it 'should render descendants of section without ID when computing extent of TOC' do
input = <<~EOS
= Document Title
:doctype: book
:pdf-page-size: A5
:toc:
the preface
:!sectids:
== Chapter 1
:sectids:
#{5.times.map {|idx| %(=== Section #{idx + 1}) }.join ?\n}
#{21.times.map {|idx| %(== Chapter #{idx + 2}) }.join ?\n}
EOS
pdf = to_pdf input, analyze: true
preface_text = pdf.find_unique_text 'the preface'
last_toc_entry_text = (pdf.find_text 'Chapter 22')[0]
(expect preface_text[:page_number]).to be > last_toc_entry_text[:page_number]
end
it 'should insert toc after preamble if toc attribute is preamble' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:toc: preamble
This is the preamble.
== Introduction
content
== Conclusion
content
EOS
toc_title_text = pdf.find_unique_text 'Table of Contents'
(expect toc_title_text[:page_number]).to be 1
introduction_title_text = pdf.find_unique_text 'Introduction', font_name: 'NotoSerif-Bold'
(expect introduction_title_text[:y]).to be < toc_title_text[:y]
introduction_toc_text = pdf.find_unique_text 'Introduction', font_name: 'NotoSerif'
(expect introduction_toc_text[:y]).to be > introduction_title_text[:y]
end
it 'should insert toc at location of toc macro if toc attribute is macro' do
lorem = ['lorem ipsum'] * 10 * %(\n\n)
input = <<~EOS
= Document Title
:doctype: book
:toc: macro
Preamble
== Introduction
#{lorem}
toc::[]
== Main
#{lorem}
== Conclusion
#{lorem}
EOS
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 6
toc_title_text = (pdf.find_text 'Table of Contents')[0]
(expect toc_title_text[:page_number]).to be 4
pdf = to_pdf input
outline = extract_outline pdf
(expect outline).to have_size 5
(expect outline.map {|it| it[:title] }).to eql ['Document Title', 'Introduction', 'Table of Contents', 'Main', 'Conclusion']
toc_dest = outline[2][:dest]
(expect toc_dest[:pagenum]).to be 4
(expect toc_dest[:label]).to eql '3'
(expect toc_dest[:y]).to be > 800
end
it 'should insert macro toc in outline as sibling of section in which it is contained' do
pdf = to_pdf <<~'EOS'
= Document Title
:doctype: book
:toc: macro
== Chapter
=== Section
toc::[]
=== Another Section
EOS
outline = extract_outline pdf
chapter_entry = outline[1]
(expect chapter_entry).not_to be_nil
(expect chapter_entry[:title]).to eql 'Chapter'
chapter_entry_children = chapter_entry[:children]
(expect chapter_entry_children).to have_size 3
toc_entry = chapter_entry_children[1]
(expect toc_entry[:title]).to eql 'Table of Contents'
(expect toc_entry[:dest][:label]).to eql '2'
end
it 'should not insert toc at location of toc macro if document has no sections' do
pdf = to_pdf <<~'EOS', analyze: true
:toc: macro
No sections here.
toc::[]
No sections here either.
Fin.
EOS
(expect pdf.lines).to eql ['No sections here.', 'No sections here either.', 'Fin.']
p1_text = pdf.find_unique_text 'No sections here.'
p2_text = pdf.find_unique_text 'No sections here either.'
p3_text = pdf.find_unique_text 'Fin.'
(expect (p1_text[:y] - p2_text[:y]).round 2).to eql ((p2_text[:y] - p3_text[:y]).round 2)
end
it 'should only insert macro toc at location of first toc macro' do
lorem = ['lorem ipsum'] * 10 * %(\n\n)
input = <<~EOS
= Document Title
:doctype: book
:toc: macro
Preamble
== Introduction
#{lorem}
toc::[]
== Main
#{lorem}
toc::[]
== Conclusion
#{lorem}
EOS
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 6
toc_title_text = (pdf.find_text 'Table of Contents')[0]
(expect toc_title_text[:page_number]).to be 4
toc_lines = pdf.lines pdf.find_text page_number: 4
(expect toc_lines).to have_size 4
['Table of Contents', 'Introduction', 'Main', 'Conclusion'].each_with_index do |title, idx|
(expect toc_lines[idx]).to start_with title
end
end
it 'should not insert toc at location of toc macro if toc attribute is not set' do
pdf = to_pdf <<~'EOS', analyze: true
== Before
toc::[]
== After
EOS
(expect pdf.lines).to eql %w(Before After)
end
it 'should not insert toc at location of toc macro if toc-placement attribute is set but not toc attribute' do
pdf = to_pdf <<~'EOS', analyze: true
:toc-placement: macro
== Before
toc::[]
== After
EOS
(expect pdf.lines).to eql %w(Before After)
end
it 'should not insert toc at location of toc macro if value of toc attribute is not macro' do
pdf = to_pdf <<~'EOS', analyze: true
:doctype: book
:toc:
== Chapter
toc::[]
text
EOS
(expect pdf.find_unique_text 'Table of Contents').not_to be_nil
(expect pdf.lines pdf.find_text page_number: 2).to eql %w(Chapter text)
end
it 'should not start new page for toc in book if already at top of page' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:doctype: book
:toc: macro
== First Chapter
#{(['filler'] * 26).join %(\n\n)}
toc::[]
== Last Chapter
Fin.
EOS
(expect pdf.pages).to have_size 4
toc_heading_text = pdf.find_unique_text 'Table of Contents'
(expect toc_heading_text[:page_number]).to be 3
end
it 'should add top margin specified by theme to toc contents when toc has a title' do
input = <<~'EOS'
= Document Title
:toc:
== Section A
=== Subsection
== Section B
EOS
toc_top_without_margin_top = ((to_pdf input, analyze: true).find_text 'Section A')[0][:y]
toc_top_with_margin_top = ((to_pdf input, pdf_theme: { toc_margin_top: 50 }, analyze: true).find_text 'Section A')[0][:y]
(expect toc_top_without_margin_top - toc_top_with_margin_top).to eql 50.0
end
it 'should add top margin specified by theme to toc contents when toc has no title and not at page top' do
input = <<~'EOS'
= Document Title
:toc:
:!toc-title:
== Section A
=== Subsection
== Section B
EOS
toc_top_without_margin_top = ((to_pdf input, analyze: true).find_text 'Section A')[0][:y]
toc_top_with_margin_top = ((to_pdf input, pdf_theme: { toc_margin_top: 50 }, analyze: true).find_text 'Section A')[0][:y]
(expect toc_top_without_margin_top - toc_top_with_margin_top).to eql 50.0
end
it 'should not add top margin specified by theme to toc contents when toc contents is at top of page' do
input = <<~'EOS'
= Document Title
:doctype: book
:toc:
:!toc-title:
== Section A
=== Subsection
== Section B
EOS
toc_top_without_margin_top = ((to_pdf input, analyze: true).find_text 'Section A')[0][:y]
toc_top_with_margin_top = ((to_pdf input, pdf_theme: { toc_margin_top: 50 }, analyze: true).find_text 'Section A')[0][:y]
(expect toc_top_without_margin_top).to eql toc_top_with_margin_top
end
it 'should start preamble toc on recto page for prepress book' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:doctype: book
:media: prepress
:toc: preamble
In a land far away...
== First Chapter
There was a hero...
== Last Chapter
Fin.
EOS
(expect pdf.pages).to have_size 9
toc_heading_text = pdf.find_unique_text 'Table of Contents'
(expect toc_heading_text[:page_number]).to be 5
end
it 'should start macro toc on recto page for prepress book' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:doctype: book
:media: prepress
:toc: macro
== First Chapter
#{(['filler'] * 26).join %(\n\n)}
toc::[]
== Last Chapter
Fin.
EOS
(expect pdf.pages).to have_size 7
toc_heading_text = pdf.find_unique_text 'Table of Contents'
(expect toc_heading_text[:page_number]).to be 5
end
it 'should not advance toc to recto page for prepress book when nonfacing option is specified on macro' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:doctype: book
:media: prepress
:toc: macro
== First Chapter
#{(['filler'] * 26).join %(\n\n)}
toc::[opts=nonfacing]
== Last Chapter
Fin.
EOS
(expect pdf.pages).to have_size 5
toc_heading_text = pdf.find_unique_text 'Table of Contents'
(expect toc_heading_text[:page_number]).to be 4
end
it 'should not advance toc in preamble to recto page for prepress book when nonfacing option is specified on macro' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:doctype: book
:media: prepress
:toc: macro
[%nonfacing]
toc::[]
== First Chapter
Début.
== Last Chapter
Fin.
EOS
(expect pdf.pages).to have_size 5
toc_heading_text = pdf.find_unique_text 'Table of Contents'
(expect toc_heading_text[:page_number]).to be 2
end
it 'should disable running content periphery on toc page if noheader or nofooter option is set on macro' do
pdf_theme = {
header_height: 30,
header_font_color: 'FF0000',
header_recto_right_content: 'Page {page-number}',
header_verso_right_content: 'Page {page-number}',
footer_font_color: '0000FF',
footer_recto_right_content: 'Page {page-number}',
footer_verso_right_content: 'Page {page-number}',
}
sections = (1..37).map {|num| %(== Section #{num}) }.join %(\n\n)
pdf = to_pdf <<~EOS, pdf_theme: pdf_theme, enable_footer: true, analyze: true
= Document Title
:doctype: book
:toc: macro
== First Chapter
#{sections}
[%noheader%nofooter]
toc::[]
== Last Chapter
EOS
toc_text = (pdf.find_text 'Table of Contents')[0]
(expect toc_text).not_to be_nil
toc_page_number = toc_text[:page_number]
last_chapter_text = (pdf.find_text 'Last Chapter')[-1]
last_chapter_page_number = last_chapter_text[:page_number]
toc_page_number.upto last_chapter_page_number do |page_number|
header_texts = (pdf.find_text font_color: 'FF0000', page_number: page_number)
footer_texts = (pdf.find_text font_color: '0000FF', page_number: page_number)
if page_number < last_chapter_page_number
(expect header_texts).to be_empty
(expect footer_texts).to be_empty
else
(expect header_texts).not_to be_empty
(expect footer_texts).not_to be_empty
end
end
end
it 'should not add toc title to page or outline if toc-title is unset' do
pdf = to_pdf <<~'EOS'
= Document Title
:doctype: book
:toc:
:!toc-title:
== Beginning
== Middle
== End
EOS
(expect pdf.pages).to have_size 5
(expect pdf.pages[1].text).to start_with 'Beginning'
outline = extract_outline pdf
(expect outline).to have_size 4
(expect outline[0][:title]).to eql 'Document Title'
(expect outline[1][:title]).to eql 'Beginning'
end
it 'should not attempt to create dots if number of dots is less than 0' do
section_title = (%w(verylongsectiontitle) * 5).join
pdf = to_pdf <<~EOS, doctype: :book, analyze: true
:toc:
:toc-max-pagenum-digits: 0
== #{section_title}
EOS
toc_lines = pdf.lines pdf.find_text page_number: 1
(expect toc_lines).to have_size 2
(expect toc_lines[0]).to eql 'Table of Contents'
(expect toc_lines[1]).to eql %(#{section_title}\u00a01)
end
it 'should line up dots and page number with wrapped line' do
pdf = to_pdf <<~'EOS', doctype: :book, analyze: true
= Document Title
:toc:
== This Here is an Absurdly Long Section Title That Exceeds the Length of a Single Line and Therefore Wraps
content
EOS
toc_text = pdf.find_text page_number: 2
(expect toc_text.size).to be > 1
(expect toc_text[1][:string]).to eql 'This Here is an Absurdly Long Section Title That Exceeds the Length of a Single Line and'
(expect toc_text[2][:string]).to eql 'Therefore Wraps'
(expect toc_text[1][:x]).to eql toc_text[2][:x]
dot_leader_text = (pdf.find_text page_number: 2).select {|it| it[:string].start_with? '.' }
(expect dot_leader_text).not_to be_empty
(expect dot_leader_text[0][:y]).to be < toc_text[1][:y]
page_number_text = pdf.find_text page_number: 2, string: '1'
(expect page_number_text).to have_size 1
end
it 'should line up dots and page number with wrapped line when section title gets split across a page boundary' do
sections = (1..37).map {|num| %(\n\n== Section #{num}) }.join
pdf = to_pdf <<~EOS, doctype: :book, analyze: true
= Document Title
:toc:
#{sections}
== This is a unbelievably long section title that probably shouldn't be a section title at all but here we are
content
EOS
page_2_lines = pdf.lines pdf.find_text page_number: 2
(expect page_2_lines).to include 'Table of Contents'
(expect page_2_lines[-1]).to end_with 'but here'
page_3_lines = pdf.lines pdf.find_text page_number: 3
(expect page_3_lines).to have_size 1
(expect page_3_lines[0]).to match %r/we are ?(\. )+ ?\u00a038$/
end
it 'should not crash if theme does not specify toc_indent' do
(expect do
pdf = to_pdf <<~'EOS', attributes: { 'pdf-theme' => (fixture_file 'custom-theme.yml') }, analyze: true
= Document Title
:toc:
== Section
EOS
toc_text = pdf.find_unique_text %r/Table of Contents/
(expect toc_text).not_to be_nil
(expect toc_text[:font_name]).to eql 'Times-Roman'
end).to not_raise_exception
end
it 'should allow hanging indent to be applied to lines that wrap' do
pdf = to_pdf <<~'EOS', doctype: :book, pdf_theme: { toc_hanging_indent: 36 }, analyze: true
= Document Title
:toc:
== This Here is an Absurdly Long Section Title That Exceeds the Length of a Single Line and Therefore Wraps
content
EOS
toc_text = pdf.find_text page_number: 2
(expect toc_text.size).to be > 1
(expect toc_text[1][:string]).to eql 'This Here is an Absurdly Long Section Title That Exceeds the Length of a Single Line and'
(expect toc_text[2][:string]).to eql 'Therefore Wraps'
(expect toc_text[2][:x]).to be > toc_text[1][:x]
dot_leader_text = (pdf.find_text page_number: 2).select {|it| it[:string].start_with? '.' }
(expect dot_leader_text).not_to be_empty
(expect dot_leader_text[0][:y]).to be < toc_text[1][:y]
page_number_text = pdf.find_text page_number: 2, string: '1'
(expect page_number_text).to have_size 1
end
it 'should allow theme to control font size of dot leader' do
pdf = to_pdf <<~'EOS', pdf_theme: { toc_dot_leader_font_size: '0.5em' }, analyze: true
= Book Title
:doctype: book
:toc:
== Foo
== Bar
EOS
reference_text = pdf.find_unique_text 'Foo', page_number: 2
dot_leader_texts = pdf.find_text %r/(?:\. )+/, page_number: 2
(expect dot_leader_texts).not_to be_empty
dot_leader_texts.each do |text|
(expect text[:font_size]).to eql (reference_text[:font_size] * 0.5)
end
end
it 'should allow theme to control font style of dot leader' do
pdf = to_pdf <<~'EOS', pdf_theme: { toc_dot_leader_font_style: 'bold' }, analyze: true
= Book Title
:doctype: book
:toc:
== Foo
EOS
dot_leader_text = pdf.find_unique_text %r/(?:\. )+/
(expect dot_leader_text[:font_name]).to eql 'NotoSerif-Bold'
end
it 'should allow theme to disable dot leader by setting content to empty string' do
pdf = to_pdf <<~'EOS', pdf_theme: { toc_dot_leader_content: '' }, analyze: true
= Book Title
:doctype: book
:toc:
== Foo
== Bar
== Baz
EOS
toc_lines = (pdf.lines pdf.find_text page_number: 2).join ?\n
(expect toc_lines).to include 'Foo'
(expect toc_lines).to include 'Bar'
(expect toc_lines).to include 'Baz'
(expect toc_lines).not_to include '.'
end
it 'should allow theme to disable dot leader by setting levels to none' do
pdf = to_pdf <<~'EOS', pdf_theme: { toc_dot_leader_levels: 'none' }, analyze: true
= Book Title
:doctype: book
:toc:
== Foo
== Bar
== Baz
EOS
toc_lines = (pdf.lines pdf.find_text page_number: 2).join ?\n
(expect toc_lines).to include 'Foo'
(expect toc_lines).to include 'Bar'
(expect toc_lines).to include 'Baz'
(expect toc_lines).not_to include '.'
end
it 'should allow theme to disable dot leader for nested levels' do
pdf = to_pdf <<~'EOS', pdf_theme: { toc_dot_leader_levels: 1 }, analyze: true
= Book Title
:doctype: book
:toc:
== Foo
=== Foo Subsection
== Bar
=== Bar Subsection
== Baz
=== Baz Subsection
EOS
toc_lines = pdf.lines.select {|it| it.include? 'Subsection' }.join ?\n
(expect toc_lines).to include 'Foo Subsection'
(expect toc_lines).to include 'Bar Subsection'
(expect toc_lines).to include 'Baz Subsection'
(expect toc_lines).not_to include '.'
end
it 'should allow theme to enable dot leader per level' do
pdf = to_pdf <<~'EOS', pdf_theme: { toc_dot_leader_levels: '1 3' }, analyze: true
= Book Title
:doctype: book
:toc:
:toclevels: 3
== Foo Top
=== Foo Subsection
==== Foo Deep
== Bar Top
=== Bar Subsection
==== Bar Deep
== Baz Top
=== Baz Subsection
==== Baz Deep
EOS
lines = pdf.lines
main_section_toc_lines = lines.select {|it| it.include? 'Top' }.join ?\n
(expect main_section_toc_lines).to include 'Foo Top'
(expect main_section_toc_lines).to include 'Bar Top'
(expect main_section_toc_lines).to include 'Baz Top'
(expect main_section_toc_lines).to include '.'
subsection_toc_lines = lines.select {|it| it.include? 'Subsection' }.join ?\n
(expect subsection_toc_lines).to include 'Foo Subsection'
(expect subsection_toc_lines).to include 'Bar Subsection'
(expect subsection_toc_lines).to include 'Baz Subsection'
(expect subsection_toc_lines).not_to include '.'
deep_section_toc_lines = lines.select {|it| it.include? 'Deep' }.join ?\n
(expect deep_section_toc_lines).to include 'Foo Deep'
(expect deep_section_toc_lines).to include 'Bar Deep'
(expect deep_section_toc_lines).to include 'Baz Deep'
(expect deep_section_toc_lines).to include '.'
end
it 'should not use part or chapter signifier in toc' do
pdf = to_pdf <<~'EOS', analyze: true
= Book Title
:doctype: book
:sectnums:
:partnums:
:toc:
= P1
== C1
= P2
== C2
EOS
lines = pdf.lines pdf.find_text page_number: 2
(expect lines).to have_size 5
(expect lines[0]).to eql 'Table of Contents'
(expect lines[1]).to start_with 'I: P1'
(expect lines[3]).to start_with 'II: P2'
(expect pdf.find_text 'Part I: P1').to have_size 1
(expect lines[2]).to start_with '1. C1'
(expect lines[4]).to start_with '2. C2'
(expect pdf.find_text 'Chapter 1. C1').to have_size 1
end
it 'should reserve enough room for toc when page number forces section title in toc to wrap' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
:doctype: book
:notitle:
:toc:
#{(['== Chapter'] * 9).join ?\n}
== This is a very long section title that wraps in the table of contents when the page number is added
#{(['== Chapter'] * 27).join ?\n}
== Last Chapter
EOS
(expect pdf.find_text page_number: 2, string: 'Last Chapter').to have_size 1
(expect pdf.find_text page_number: 2, string: 'Chapter').to be_empty
end
end
context 'article' do
it 'should not generate toc by default' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
== Introduction
== Main
== Conclusion
EOS
(expect pdf.pages).to have_size 1
(expect pdf.find_text 'Table of Contents').to be_empty
end
it 'should insert toc between document title and content when toc is set' do
lorem = ['lorem ipsum'] * 10 * %(\n\n)
input = <<~EOS
= Document Title
:toc:
Preamble
== Introduction
#{lorem}
== Main
#{lorem}
== Conclusion
#{lorem}
EOS
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 2
(expect pdf.find_text 'Table of Contents', page_number: 1).to have_size 1
(expect pdf.find_text 'Introduction', page_number: 1).to have_size 2
doctitle_text = (pdf.find_text 'Document Title')[0]
toc_title_text = (pdf.find_text 'Table of Contents')[0]
toc_bottom_text = (pdf.find_text '2')[0]
content_top_text = (pdf.find_text 'Preamble')[0]
(expect doctitle_text[:y]).to be > toc_title_text[:y]
(expect toc_title_text[:y]).to be > content_top_text[:y]
(expect toc_bottom_text[:y]).to be > content_top_text[:y]
# NOTE: assert there's no excess gap between end of toc and start of content
(expect toc_bottom_text[:y] - content_top_text[:y]).to be < 35
end
it 'should insert toc at top of first page if toc is set and document has no doctitle' do
pdf = to_pdf <<~'EOS', analyze: true
:toc:
== Section A
== Section B
EOS
toc_title_text = (pdf.find_text 'Table of Contents')[0]
sect_a_text = (pdf.find_text 'Section A', font_size: 22)[0]
(expect toc_title_text[:y]).to be > sect_a_text[:y]
end
it 'should reserve enough pages for toc if it spans more than one page' do
sections = (1..40).map {|num| %(\n\n== Section #{num}) }
input = <<~EOS
= Document Title
:toc:
#{sections.join}
EOS
pdf = to_pdf input, analyze: :page
(expect pdf.pages).to have_size 4
(expect pdf.pages[0][:strings]).to include 'Document Title'
(expect pdf.pages[0][:strings]).to include 'Table of Contents'
(expect pdf.pages[0][:strings]).not_to include 'Section 40'
(expect pdf.pages[1][:strings]).to include 'Section 40'
(expect pdf.pages[1][:strings]).to include 'Section 1'
pdf = to_pdf input, analyze: true
text = pdf.text
idx_toc_bottom = nil
idx_content_top = nil
text.each_with_index do |candidate, idx|
idx_toc_bottom = idx if candidate[:string] == 'Section 40' && candidate[:font_size] == 10.5
idx_content_top = idx if candidate[:string] == 'Section 1' && candidate[:font_size] == 22
end
(expect text[idx_toc_bottom][:y]).to be > text[idx_content_top][:y]
# NOTE: assert there's no excess gap between end of toc and start of content
(expect text[idx_toc_bottom][:y] - text[idx_content_top][:y]).to be < 50
end
it 'should insert toc between title page and first page of body when toc and title-page are set' do
pdf = to_pdf <<~'EOS', analyze: :page
= Document Title
:toc:
:title-page:
== Introduction
== Main
== Conclusion
EOS
(expect pdf.pages).to have_size 3
(expect pdf.pages[0][:strings]).to include 'Document Title'
(expect pdf.pages[1][:strings]).to include 'Table of Contents'
(expect pdf.pages[1][:strings]).to include '1'
(expect pdf.pages[1][:strings]).not_to include '2'
(expect pdf.pages[2][:strings]).to include 'Introduction'
end
it 'should insert toc at location of toc macro if toc attribute is macro' do
lorem = ['lorem ipsum'] * 10 * %(\n\n)
input = <<~EOS
= Document Title
:toc: macro
Preamble
== Introduction
#{lorem}
toc::[]
== Main
#{lorem}
== Conclusion
#{lorem}
EOS
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 2
(expect pdf.find_text 'Table of Contents', page_number: 1).to have_size 1
(expect pdf.find_text 'Introduction', page_number: 1).to have_size 2
doctitle_text = (pdf.find_text 'Document Title')[0]
toc_title_text = (pdf.find_text 'Table of Contents')[0]
toc_bottom_text = (pdf.find_text '2')[0]
content_top_text = (pdf.find_text 'Preamble')[0]
intro_title_text = (pdf.find_text 'Introduction')[0]
(expect doctitle_text[:y]).to be > toc_title_text[:y]
(expect toc_title_text[:y]).to be < content_top_text[:y]
(expect toc_bottom_text[:y]).to be < content_top_text[:y]
(expect toc_title_text[:y]).to be < intro_title_text[:y]
pdf = to_pdf input
outline = extract_outline pdf
(expect outline).to have_size 5
(expect outline.map {|it| it[:title] }).to eql ['Document Title', 'Introduction', 'Table of Contents', 'Main', 'Conclusion']
toc_dest = outline[2][:dest]
(expect toc_dest[:pagenum]).to be 1
(expect toc_dest[:label]).to eql '1'
(expect toc_dest[:y]).to be < 800
end
it 'should insert macro toc in outline as sibling of section in which it is contained' do
pdf = to_pdf <<~'EOS'
= Document Title
:toc: macro
== Section
=== Subsection
toc::[]
=== Another Subsection
EOS
outline = extract_outline pdf
section_entry = outline[1]
(expect section_entry).not_to be_nil
(expect section_entry[:title]).to eql 'Section'
section_entry_children = section_entry[:children]
(expect section_entry_children).to have_size 3
toc_entry = section_entry_children[1]
(expect toc_entry[:title]).to eql 'Table of Contents'
(expect toc_entry[:dest][:label]).to eql '1'
end
it 'should insert macro toc in outline before other sections if macro proceeds sections' do
pdf = to_pdf <<~'EOS'
= Document Title
:toc: macro
toc::[]
== Section
=== Subsection
=== Another Subsection
EOS
outline = extract_outline pdf
toc_entry = outline[1]
(expect toc_entry[:title]).to eql 'Table of Contents'
(expect toc_entry[:dest][:label]).to eql '1'
end
end
it 'should apply consistent font color to running content when base font color is unset', visual: true do
theme_overrides = {
extends: 'base',
base_font_color: nil,
header_height: 36,
header_font_color: '0000FF',
header_columns: '0% =100% 0%',
header_recto_center_content: 'header text',
header_verso_center_content: 'header text',
toc_dot_leader_font_color: 'CCCCCC',
running_content_start_at: 'toc',
}
to_file = to_pdf_file <<~'EOS', 'toc-running-content-font-color.pdf', pdf_theme: theme_overrides
= Document Title
Author Name
:doctype: book
:toc:
== A
text
== B
text
EOS
(expect to_file).to visually_match 'toc-running-content-font-color.pdf'
end
it 'should use same font color for text and dot leader if dot leader font color is unspecified' do
pdf_theme = {
extends: 'base',
toc_font_color: '4a4a4a',
}
pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
= Document Title
Author Name
:doctype: book
:toc:
== Intro
text
== Conclusion
text
EOS
intro_entry_font_color = (pdf.find_unique_text 'Intro', page_number: 2)[:font_color]
dot_leader_font_color = (pdf.find_text page_number: 2).select {|it| it[:string].start_with? '.' }.map {|it| it[:font_color] }.uniq[0]
(expect dot_leader_font_color).to eql intro_entry_font_color
end
it 'should not apply bold to italic text if headings are bold in theme' do
pdf_theme = {
toc_font_style: 'bold',
}
pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
= Document Title
:doctype: book
:toc:
== Get Started _Quickly_
EOS
get_started_text = (pdf.find_text page_number: 2, string: /^Get Started/)[0]
quickly_text = (pdf.find_text page_number: 2, string: 'Quickly')[0]
(expect get_started_text[:font_name]).to eql 'NotoSerif-Bold'
(expect quickly_text[:font_name]).to eql 'NotoSerif-Italic'
end
it 'should allow theme to specify text decoration for entries in toc' do
pdf_theme = {
toc_text_decoration: 'underline',
}
input = <<~'EOS'
= Document Title
:toc:
:title-page:
== Underline Me
EOS
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: :line
lines = pdf.lines
(expect lines).to have_size 1
toc_entry_underline = lines[0]
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
toc_entry_text = (pdf.find_text page_number: 2, string: 'Underline Me')[0]
(expect toc_entry_underline[:from][:x]).to eql toc_entry_text[:x]
(expect toc_entry_underline[:from][:y]).to be_within(2).of(toc_entry_text[:y])
(expect toc_entry_underline[:color]).to eql toc_entry_text[:font_color]
end
it 'should allow theme to specify color and width of text decoration for entries in toc' do
pdf_theme = {
toc_text_decoration: 'underline',
toc_text_decoration_color: 'cccccc',
toc_text_decoration_width: 0.5,
}
input = <<~'EOS'
= Document Title
:toc:
:title-page:
== Underline Me
EOS
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: :line
lines = pdf.lines
(expect lines).to have_size 1
toc_entry_underline = lines[0]
(expect toc_entry_underline[:color]).to eql 'CCCCCC'
(expect toc_entry_underline[:width]).to eql 0.5
end
it 'should use fallback value to align toc title if alignment not specified in theme' do
[
{
toc_title_text_align: 'center',
heading_h2_text_align: 'left',
heading_text_align: 'left',
},
{
toc_title_text_align: nil,
heading_h2_text_align: 'center',
heading_text_align: 'left',
},
{
toc_title_text_align: nil,
heading_h2_text_align: nil,
heading_text_align: 'center',
},
{
toc_title_text_align: nil,
heading_h2_text_align: nil,
heading_text_align: nil,
base_text_align: 'center',
},
].each do |pdf_theme|
pdf = to_pdf <<~'EOS', pdf_theme: pdf_theme, analyze: true
= Document Title
:toc:
:doctype: book
== Section A
== Section B
EOS
toc_title_text = pdf.find_unique_text 'Table of Contents'
(expect toc_title_text[:x]).to be > 48.24
end
end
it 'should allow theme to specify text decoration per heading level in toc' do
pdf_theme = {
toc_h3_text_decoration: 'underline',
toc_h3_text_decoration_color: 'cccccc',
toc_h3_text_decoration_width: 0.5,
}
input = <<~'EOS'
= Document Title
:toc:
:title-page:
== Plain Title
=== Decorated Title
EOS
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: :line
lines = pdf.lines
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
underlined_toc_entry_text = pdf.find_unique_text 'Decorated Title', page_number: 2
(expect lines).to have_size 1
toc_entry_underline = lines[0]
(expect toc_entry_underline[:color]).to eql 'CCCCCC'
(expect toc_entry_underline[:width]).to eql 0.5
(expect toc_entry_underline[:from][:y]).to be < underlined_toc_entry_text[:y]
(expect toc_entry_underline[:from][:y]).to be_within(2).of(underlined_toc_entry_text[:y])
end
it 'should allow theme to specify text transform for entries in toc' do
pdf_theme = {
toc_text_transform: 'uppercase',
}
input = <<~'EOS'
= Document Title
:doctype: book
:toc:
== Transform Me
EOS
pdf = to_pdf input, pdf_theme: pdf_theme, analyze: true
toc_lines = pdf.lines pdf.find_text page_number: 2
(expect toc_lines.join ?\n).to include 'TRANSFORM ME'
end
it 'should decode character references in toc entries' do
pdf = to_pdf <<~'EOS', analyze: true
= Document Title
:toc:
== Paper Clips № 4
EOS
(expect pdf.find_text %(Paper Clips \u2116\u00a04)).to have_size 2
end
it 'should not crash if section title is empty' do
pdf = to_pdf <<~'EOS', analyze: true
:toc:
== {empty}
content
EOS
(expect pdf.text).to have_size 2
(expect pdf.find_unique_text 'content').not_to be_nil
(expect pdf.find_unique_text 'Table of Contents').not_to be_nil
end
it 'should allocate correct number of pages for toc if line numbers cause lines to wrap' do
chapter_title = %(\n\n== A long chapter title that wraps to a second line in the toc when the page number exceeds one digit)
input = <<~EOS
= Document Title
:doctype: book
:toc:
:nofooter:
#{chapter_title * 38}
EOS
pdf = to_pdf input, analyze: true
last_pagenum_text = (pdf.find_text '38')[0]
first_chapter_text = (pdf.find_text font_name: 'NotoSerif-Bold', font_size: 22, string: /^A long chapter title/)[0]
(expect first_chapter_text[:page_number]).to be last_pagenum_text[:page_number].next
end
it 'should render image at end of section title in toc entry' do
pdf = to_pdf <<~'EOS', analyze: :image
= Document Title
:doctype: book
:toc:
== Chapter image:tux.png[,16]
EOS
images = pdf.images
(expect images).to have_size 2
(expect images[0][:page_number]).to be 2
(expect images[1][:page_number]).to be 3
(expect images[0][:data]).to eql images[1][:data]
(expect images[0][:width]).to eql images[1][:width]
end
it 'should allow extended converter to insert extra page before toc' do
backend = nil
create_class (Asciidoctor::Converter.for 'pdf') do
register_for (backend = %(pdf#{object_id}).to_sym)
def ink_toc doc, num_levels, toc_page_number, start_cursor, num_front_matter_pages = 0
go_to_page toc_page_number unless (page_number == toc_page_number) || scratch?
unless scratch?
theme_font :heading, level: 2 do
ink_heading 'Extra', level: 2
end
go_to_page page_number + 1
end
offset = 1
toc_page_numbers = super doc, num_levels, (toc_page_number + offset), start_cursor, num_front_matter_pages
scratch? ? ((toc_page_numbers.begin - offset)..toc_page_numbers.end) : toc_page_numbers
end
end
input = <<~'EOS'
= Document Title
:doctype: book
:toc:
== Chapter A
== Chapter B
EOS
pdf = to_pdf input, backend: backend, analyze: true
(expect pdf.pages).to have_size 5
[['Extra', 2], ['Table of Contents', 3], ['Chapter A', 4]].each do |title, pnum|
title_text = (pdf.find_text title)[-1]
(expect title_text[:page_number]).to eql pnum
end
(expect (pdf.find_text 'Chapter A')[0][:page_number]).to eql 3
(expect (pdf.find_text 'Chapter B')[0][:page_number]).to eql 3
end
it 'should allow extended converter to insert extra entries into TOC' do
backend = nil
create_class (Asciidoctor::Converter.for 'pdf') do
register_for (backend = %(pdf#{object_id}).to_sym)
def get_entries_for_toc node
return super if node.context == :document
node.blocks.select(&:id)
end
end
input = <<~'EOS'
= Document Title
:doctype: book
:toc:
== Chapter A
.Check for Ruby
[#check-for-ruby]
****
Run `ruby -v` to check if you have Ruby installed.
****
<<<
=== Section
== Chapter B
[#screenshot,reftext=Screenshot]
image::tux.png[]
=== Another Section
EOS
pdf = to_pdf input, backend: backend, analyze: true
(expect pdf.pages).to have_size 5
toc_text = (pdf.find_text page_number: 2).reject {|it| it[:string] == 'Table of Contents' }
toc_lines = pdf.lines toc_text
[['Chapter A', 1], ['Check for Ruby', 1], ['Section', 2], ['Chapter B', 3], ['Screenshot', 3], ['Another Section', 3]].each_with_index do |(title, pnum), idx|
line = toc_lines[idx]
(expect line).to start_with title
(expect line).to end_with pnum.to_s
end
pdf = to_pdf input, backend: backend
annots = get_annotations pdf, 2
(expect annots.select {|it| it[:Dest] == 'check-for-ruby' }).to have_size 2
(expect annots.select {|it| it[:Dest] == 'screenshot' }).to have_size 2
end
it 'should allow extended converter to insert chapter per TOC' do
source_file = doc_file 'modules/extend/examples/pdf-converter-chapter-toc.rb'
source_lines = (File.readlines source_file).select {|l| l == ?\n || (l.start_with? ' ') }
ext_class = create_class Asciidoctor::Converter.for 'pdf'
backend = %(pdf#{ext_class.object_id})
source_lines[0] = %( register_for '#{backend}'\n)
ext_class.class_eval source_lines.join, source_file
pdf = to_pdf <<~'EOS', backend: backend, analyze: true
= Document Title
:doctype: book
:toc:
:toclevels: 1
:chapter-toc:
:chapter-toclevels: 2
== Chapter Title
=== First Section
==== Subsection
<<<
=== Last Section
== Another Chapter
EOS
toc_text = pdf.find_text page_number: 2
toc_lines = toc_text
.sort_by {|it| -it[:y] }
.group_by {|it| it[:y] }
.map {|_, it| it.sort_by {|fragment| fragment[:order] }.map {|fragment| fragment[:string] }.join }
(expect toc_lines).to have_size 3
(expect toc_lines[0]).to eql 'Table of Contents'
(expect toc_lines[1]).to start_with 'Chapter Title'
(expect toc_lines[1]).to end_with '1'
(expect toc_lines[2]).to start_with 'Another Chapter'
(expect toc_lines[2]).to end_with '3'
ch1_text = pdf.find_text page_number: 3
ch1_lines = ch1_text
.sort_by {|it| -it[:y] }
.group_by {|it| it[:y] }
.map {|_, it| it.sort_by {|fragment| fragment[:order] }.map {|fragment| fragment[:string] }.join }
(expect ch1_lines).to have_size 6
(expect ch1_lines[0]).to eql 'Chapter Title'
(expect ch1_lines[1]).to start_with 'First Section'
(expect ch1_lines[1]).to end_with '1'
(expect ch1_lines[2]).to start_with 'Subsection'
(expect ch1_lines[2]).to end_with '1'
(expect ch1_lines[3]).to start_with 'Last Section'
(expect ch1_lines[3]).to end_with '2'
(expect ch1_lines[4]).to eql 'First Section'
(expect ch1_lines[5]).to eql 'Subsection'
end
end
|