File: converter_spec.rb

package info (click to toggle)
ruby-asciidoctor-pdf 2.3.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,972 kB
  • sloc: ruby: 44,316; sh: 133; java: 45; makefile: 4
file content (1566 lines) | stat: -rw-r--r-- 48,315 bytes parent folder | download
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
# frozen_string_literal: true

require_relative 'spec_helper'

describe Asciidoctor::PDF::Converter do
  describe '.register_for' do
    it 'should self register to handle pdf backend' do
      registered = Asciidoctor::Converter.for 'pdf'
      (expect registered).to be described_class
    end

    it 'should convert AsciiDoc string to PDF object when backend is pdf' do
      (expect Asciidoctor.convert 'hello', backend: 'pdf').to be_a Prawn::Document
    end

    it 'should convert AsciiDoc file to PDF file when backend is pdf' do
      pdf = to_pdf Pathname.new fixture_file 'hello.adoc'
      (expect Pathname.new output_file 'hello.pdf').to exist
      (expect pdf.page_count).to be > 0
    end
  end

  describe '#convert' do
    it 'should not fail to convert empty string' do
      (expect to_pdf '').not_to be_nil
    end

    it 'should not fail to convert empty file' do
      pdf = to_pdf Pathname.new fixture_file 'empty.adoc'
      (expect Pathname.new output_file 'empty.pdf').to exist
      (expect pdf.page_count).to be > 0
    end

    it 'should convert file to target dir in secure mode' do
      input_file = fixture_file 'secure.adoc'
      target_file = output_file 'secure.pdf'
      doc = Asciidoctor.convert_file input_file, backend: 'pdf', to_dir: output_dir, safe: 'secure'
      (expect doc.attr 'outfile').to be_nil
      pdf = PDF::Reader.new target_file
      (expect pdf.pages).to have_size 2
      (expect pdf.pages[0].text).to include 'Book Title'
      (expect pdf.pages[1].text).to include 'Chapter'
      images = get_images pdf
      (expect images).to have_size 1
    end

    it 'should convert file to target file in secure mode' do
      input_file = fixture_file 'secure.adoc'
      target_file = output_file 'secure-alt.pdf'
      Asciidoctor.convert_file input_file, backend: 'pdf', to_file: target_file, safe: 'secure'
      (expect Pathname.new target_file).to exist
      pdf = PDF::Reader.new target_file
      (expect pdf.pages).to have_size 2
      (expect pdf.pages[0].text).to include 'Book Title'
      (expect pdf.pages[1].text).to include 'Chapter'
      images = get_images pdf
      (expect images).to have_size 1
    end

    it 'should be able to load, convert, and write in separate steps' do
      input_file = fixture_file 'hello.adoc'
      target_file = output_file 'hello.pdf'
      doc = Asciidoctor.load_file input_file, backend: 'pdf'
      doc.write doc.convert, target_file
      (expect Pathname.new target_file).to exist
      pdf = PDF::Reader.new target_file
      (expect pdf.pages).to have_size 1
    end

    it 'should be able to reuse instance of converter' do
      input_file = fixture_file 'book.adoc'
      doc = Asciidoctor.load_file input_file, backend: 'pdf', safe: :safe, attributes: { 'reproducible' => '' }
      converter = doc.converter
      pdf1 = doc.convert.render
      doc = Asciidoctor.load_file input_file, backend: 'pdf', safe: :safe, attributes: { 'reproducible' => '' }, converter: converter
      pdf2 = doc.convert.render
      (expect pdf1).to eql pdf2
    end

    it 'should warn if convert method is not found for node' do
      (expect do
        doc = Asciidoctor.load <<~'EOS', backend: 'pdf', safe: :safe, attributes: { 'nofooter' => '' }
        before

        1,2,3

        after
        EOS
        doc.blocks[1].context = :chart
        pdf_stream = StringIO.new
        doc.write doc.convert, pdf_stream
        pdf = PDF::Reader.new pdf_stream
        pages = pdf.pages
        (expect pages).to have_size 1
        lines = pdf.pages[0].text.lines.map(&:rstrip).reject(&:empty?)
        (expect lines).to eql %w(before after)
      end).to log_message severity: :WARN, message: 'missing convert handler for chart node in pdf backend'
    end

    it 'should not warn if convert method is not found for node in scratch document' do
      (expect do
        doc = Asciidoctor.load <<~'EOS', backend: 'pdf', safe: :safe, attributes: { 'nofooter' => '' }
        before

        [%unbreakable]
        --
        1,2,3
        --

        after
        EOS
        doc.blocks[1].blocks[0].context = :chart
        pdf_stream = StringIO.new
        doc.write doc.convert, pdf_stream
        pdf = PDF::Reader.new pdf_stream
        pages = pdf.pages
        (expect pages).to have_size 1
        lines = pdf.pages[0].text.lines.map(&:rstrip).reject(&:empty?)
        (expect lines).to eql %w(before after)
      end).to log_message severity: :WARN, message: 'missing convert handler for chart node in pdf backend'
    end

    it 'should ensure data-uri attribute is set' do
      doc = Asciidoctor.load <<~'EOS', backend: 'pdf', base_dir: fixtures_dir, safe: :safe
      image::logo.png[]
      EOS
      (expect doc.attr? 'data-uri').to be true
      doc.convert
      (expect doc.attr? 'data-uri').to be true
    end

    it 'should ignore data-uri attribute entry in document' do
      doc = Asciidoctor.load <<~'EOS', backend: 'pdf', base_dir: fixtures_dir, safe: :safe
      :!data-uri:

      image::logo.png[]
      EOS
      (expect doc.attr? 'data-uri').to be true
      doc.convert
      (expect doc.attr? 'data-uri').to be true
    end

    it 'should not fail to remove tmp files if already removed' do
      image_data = File.read (fixture_file 'square.jpg'), mode: 'r:UTF-8'
      encoded_image_data = [image_data].pack 'm0'
      doc = Asciidoctor.load <<~EOS, backend: 'pdf'
      :page-background-image: image:data:image/png;base64,#{encoded_image_data}[Square,fit=cover]
      EOS
      pdf_doc = doc.convert
      tmp_files = (converter = doc.converter).instance_variable_get :@tmp_files
      (expect tmp_files).to have_size 1
      tmp_files.each {|_, path| converter.send :unlink_tmp_file, path }
      doc.write pdf_doc, (pdf_io = StringIO.new)
      pdf = PDF::Reader.new pdf_io
      (expect get_images pdf).to have_size 1
    end

    it 'should not fail to remove tmp files if they are not writable' do
      (expect do
        image_data = File.read (fixture_file 'square.jpg'), mode: 'r:UTF-8'
        encoded_image_data = [image_data].pack 'm0'
        doc = Asciidoctor.load <<~EOS, backend: 'pdf'
        :page-background-image: image:data:image/png;base64,#{encoded_image_data}[Square,fit=cover]
        EOS
        pdf_doc = doc.convert
        tmp_files = doc.converter.instance_variable_get :@tmp_files
        (expect tmp_files).to have_size 1
        tmp_file_paths = tmp_files.map do |_, path|
          FileUtils.mv path, (tmp_path = %(#{path}-tmp))
          Dir.mkdir path
          FileUtils.mv tmp_path, (File.join path, (File.basename path))
          path
        end
        doc.write pdf_doc, (pdf_io = StringIO.new)
        pdf = PDF::Reader.new pdf_io
        (expect get_images pdf).to have_size 1
        tmp_file_paths.each {|path| (Pathname.new path).rmtree secure: true }
      end).to log_message severity: :WARN, message: '~could not delete temporary file'
    end

    it 'should keep tmp files if KEEP_ARTIFACTS environment variable is set' do
      image_data = File.read (fixture_file 'square.jpg'), mode: 'r:UTF-8'
      encoded_image_data = [image_data].pack 'm0'
      doc = Asciidoctor.load <<~EOS, backend: 'pdf'
      :page-background-image: image:data:image/png;base64,#{encoded_image_data}[Square,fit=cover]
      EOS
      pdf_doc = doc.convert
      tmp_files = doc.converter.instance_variable_get :@tmp_files
      (expect tmp_files).to have_size 1
      ENV['KEEP_ARTIFACTS'] = 'true'
      doc.write pdf_doc, (pdf_io = StringIO.new)
      ENV.delete 'KEEP_ARTIFACTS'
      pdf = PDF::Reader.new pdf_io
      (expect get_images pdf).to have_size 1
      (expect tmp_files).to have_size 1
      tmp_files.each do |_, path|
        (expect Pathname.new path).to exist
        File.unlink path
      end
    end

    context 'theme' do
      it 'should apply the theme at the path specified by pdf-theme' do
        with_pdf_theme_file <<~'EOS' do |theme_path|
        base:
          font-color: ff0000
        EOS
          pdf = to_pdf <<~EOS, analyze: true
          = Document Title
          :pdf-theme: #{theme_path}

          red text
          EOS

          (expect pdf.find_text font_color: 'FF0000').to have_size pdf.text.size
        end
      end

      it 'should only load theme from pdf-themesdir if pdf-theme attribute specified' do
        [nil, 'default'].each do |theme|
          to_pdf_opts = { analyze: true }
          to_pdf_opts[:attribute_overrides] = { 'pdf-theme' => theme } if theme
          pdf = to_pdf <<~EOS, to_pdf_opts
          = Document Title
          :pdf-themesdir: #{fixtures_dir}

          body text
          EOS

          expected_font_color = theme ? 'AA0000' : '333333'
          body_text = (pdf.find_text 'body text')[0]
          (expect body_text).not_to be_nil
          (expect body_text[:font_color]).to eql expected_font_color
        end
      end

      it 'should apply the named theme specified by pdf-theme located in the specified pdf-themesdir' do
        with_pdf_theme_file <<~'EOS' do |theme_path|
        base:
          font-color: ff0000
        EOS
          pdf = to_pdf <<~EOS, analyze: true
          = Document Title
          :pdf-theme: #{File.basename theme_path, '-theme.yml'}
          :pdf-themesdir: #{File.dirname theme_path}

          red text
          EOS

          (expect pdf.find_text font_color: 'FF0000').to have_size pdf.text.size
        end
      end

      it 'should resolve pdf-themesdir relative to the current working directory' do
        input_file = Pathname.new fixture_file 'hello-with-custom-theme.adoc'
        relative_themesdir = fixture_file '.', relative: true
        pdf = to_pdf input_file, analyze: true, attribute_overrides: { 'pdf-themesdir' => relative_themesdir }
        (expect pdf.find_text font_name: 'Times-Roman').to have_size pdf.text.size
      end

      it 'should replace {docdir} token in value of pdf-themesdir' do
        input_file = Pathname.new fixture_file 'hello-with-custom-theme.adoc'
        pdf = to_pdf input_file, analyze: true, attribute_overrides: { 'pdf-themesdir' => '{docdir}' }
        (expect pdf.find_text font_name: 'Times-Roman').to have_size pdf.text.size
      end

      it 'should resolve theme at root of classloader when pdf-themesdir is uri:classloader:/', if: RUBY_ENGINE == 'jruby' do
        require fixture_file 'pdf-themes.jar'
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'pdf-themesdir' => 'uri:classloader:/', 'pdf-theme' => 'custom' }, analyze: true
        hi there
        EOS

        text = pdf.find_unique_text 'hi there'
        (expect text[:font_color]).to eql '0000FF'
      end

      it 'should resolve theme from folder in classloader when pdf-themesdir starts with uri:classloader:', if: RUBY_ENGINE == 'jruby' do
        require fixture_file 'pdf-themes.jar'
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'pdf-themesdir' => 'uri:classloader:/pdf-themes', 'pdf-theme' => 'another-custom' }, analyze: true
        hi there
        EOS

        text = pdf.find_unique_text 'hi there'
        (expect text[:font_color]).to eql 'FF0000'
      end

      it 'should set text color to black when default-for-print theme is specified' do
        pdf = to_pdf <<~EOS, analyze: true
        = Document Title
        :pdf-theme: default-for-print

        black `text`

        > loud quote
        EOS

        (expect pdf.find_text font_color: '000000').to have_size pdf.text.size
      end

      it 'should set font family to Noto Sans when default-sans themme is specified' do
        pdf = to_pdf <<~EOS, analyze: true
        = Document Title
        :pdf-theme: default-sans

        We don't like those _pesky_ serifs in these here parts.
        EOS

        text = pdf.text
        sans_text = text.select {|it| it[:font_name].start_with? 'NotoSans' }
        (expect sans_text).to have_size text.size
      end

      it 'should use theme passed in through :pdf_theme option' do
        theme = Asciidoctor::PDF::ThemeLoader.load_theme 'custom', fixtures_dir
        theme.base_font_size = 14
        theme.base_font_color = '1a1a1a'
        pdf = Asciidoctor.convert 'content', backend: 'pdf', pdf_theme: theme
        converter_theme = pdf.instance_variable_get :@theme
        (expect converter_theme.base_font_size).to eql theme.base_font_size
        (expect converter_theme.base_font_color).to eql theme.base_font_color
      end

      it 'should set themesdir theme with __dir__ is passed via :pdf_theme option' do
        theme = Asciidoctor::PDF::ThemeLoader.load_base_theme
        theme.delete_field :__dir__
        pdf = Asciidoctor.convert 'content', backend: 'pdf', pdf_theme: theme
        (expect pdf.instance_variable_get :@themesdir).to eql Dir.pwd
      end

      it 'should log error if built-in theme cannot be found or loaded' do
        (expect do
          Asciidoctor.convert 'foo', backend: 'pdf', attributes: { 'pdf-theme' => 'foo' }
        end).to log_message severity: :ERROR, message: /could not locate or load the built-in pdf theme `foo' because of .*?; reverting to default theme/
      end

      it 'should log error if user theme cannot be found or loaded' do
        (expect do
          Asciidoctor.convert 'foo', backend: 'pdf', attributes: { 'pdf-theme' => 'foo', 'pdf-themesdir' => fixtures_dir }
        end).to log_message severity: :ERROR, message: /could not locate or load the pdf theme `foo' in #{Regexp.escape fixtures_dir} because of .*?; reverting to default theme/
      end

      it 'should log error with filename and reason if theme file cannot be parsed' do
        pdf_theme = fixture_file 'tab-indentation-theme.yml'
        (expect do
          pdf = to_pdf 'content', attribute_overrides: { 'pdf-theme' => pdf_theme }, analyze: true
          (expect pdf.pages).to have_size 1
        end).to log_message severity: :ERROR, message: /because of Psych::SyntaxError \(#{Regexp.escape pdf_theme}\): found character .*that cannot start any token.*; reverting to default theme/
      end

      it 'should log error with filename and reason if exception is thrown during theme compilation' do
        (expect do
          pdf = to_pdf 'content', attribute_overrides: { 'pdf-theme' => (fixture_file 'invalid-theme.yml') }, analyze: true
          (expect pdf.pages).to have_size 1
        end).to log_message severity: :ERROR, message: /because of NoMethodError undefined method `start_with\?' for (?:10:(?:Fixnum|Integer)|an instance of Integer); reverting to default theme/
      end

      it 'should not crash if theme does not specify any keys' do
        pdf = to_pdf <<~'EOS', attribute_overrides: { 'pdf-theme' => (fixture_file 'bare-theme.yml') }, analyze: true
        = Document Title
        :doctype: book

        This is the stark theme.

        == Chapter Title

        === Section Title

        .dlist
        term:: desc

        .ulist
        * one
        * two

        ----
        key=val <1>
        ----
        <1> A variable assignment

        NOTE: That's all, folks!
        EOS

        (expect pdf.pages).to have_size 3
        (expect pdf.find_text font_name: 'Helvetica', font_size: 12).not_to be_empty
        (expect (pdf.find_text 'Document Title')[0]).not_to be_nil
        (expect (pdf.find_text 'Chapter Title')[0]).not_to be_nil
        (expect (pdf.find_text 'Section Title')[0]).not_to be_nil
        (expect (pdf.find_text 'ulist')[0]).not_to be_nil
        (expect (pdf.find_text 'one')[0]).not_to be_nil
      end

      it 'should not crash if theme does not specify any keys when converting chronicles example' do
        input_path = Pathname.new example_file 'chronicles-example.adoc'
        pdf = to_pdf input_path, attribute_overrides: { 'imagesdir' => '@', 'pdf-theme' => (fixture_file 'bare-theme.yml'), 'source-highlighter' => nil }
        (expect pdf.pages).to have_size 14
        (expect (pdf.page 1).text).to include 'Documentation Chronicles'
      end

      it 'should not warn when using dark theme to convert chronicles example' do
        input_path = Pathname.new example_file 'chronicles-example.adoc'
        pdf = to_pdf input_path, attribute_overrides: { 'imagesdir' => '@', 'pdf-theme' => 'chronicles-dark', 'source-highlighter' => nil }, analyze: true
        (expect pdf.pages).to have_size 17
        gs_p1 = pdf.pages[0][:raw_content]
        (expect gs_p1).to start_with %(q\n/DeviceRGB cs\n0.0 0.0 0.0 scn\n0.0 0.0 595.28 841.89 re\n)
        doctitle = pdf.find_text page_number: 1, string: 'Documentation Chronicles'
        (expect doctitle).to have_size 1
        (expect doctitle[0][:font_color]).to eql '666666'
        p4_text = pdf.find_text page_number: 4
        heading_text = p4_text[0]
        paragraph_text = p4_text[1]
        link_text = p4_text[2]
        (expect heading_text[:font_color]).to eql 'CCCCCC'
        (expect paragraph_text[:font_color]).to eql 'CCCCCC'
        (expect link_text[:font_color]).to eql 'BD7435'
      end

      it 'should allow all border colors to be set using base-border-color when extending base theme' do
        [
          %(****\ncontent\n****),
          %(====\ncontent\n====),
          %([cols=2*]\n|===\n|a|b\n|c|d\n|===),
          %(____\ncontent\n____),
          %([verse]\n____\ncontent\n____),
          %(----\ncontent\n----),
          '---',
          'NOTE: content',
        ].each do |input|
          pdf = to_pdf input, pdf_theme: { extends: 'base', base_border_color: '0000EE' }, analyze: :line
          (expect pdf.lines.map {|it| it[:color] }.uniq).to eql %w(0000EE)
        end
      end

      it 'should convert background position to options' do
        converter = Asciidoctor::Converter.create 'pdf'
        {
          'center' => { position: :center, vposition: :center },
          'top' => { position: :center, vposition: :top },
          'bottom' => { position: :center, vposition: :bottom },
          'left' => { position: :left, vposition: :center },
          'right' => { position: :right, vposition: :center },
          'top left' => { position: :left, vposition: :top },
          'right top' => { position: :right, vposition: :top },
          'bottom left' => { position: :left, vposition: :bottom },
          'right bottom' => { position: :right, vposition: :bottom },
          'center right' => { position: :right, vposition: :center },
          'left center' => { position: :left, vposition: :center },
          'center center' => { position: :center, vposition: :center },
          'bogus' => nil,
          'bogus bogus' => nil,
        }.each do |value, expected|
          (expect converter.send :resolve_background_position, value, nil).to eql expected
        end
      end

      it 'should expose theme as property on converter' do
        doc = Asciidoctor.load 'yo', backend: :pdf
        doc.convert
        (expect doc.converter.theme).not_to be_nil
        (expect doc.converter.theme.base_font_family).to eql 'Noto Serif'
      end
    end
  end

  describe 'helpers' do
    it 'should not drop lines with unresolved attributes when apply_subs_discretely is called without options' do
      input = <<~'EOS'
      foo
      {undefined}
      bar
      EOS
      doc = Asciidoctor.load 'yo', backend: :pdf
      converter = doc.converter
      converter.load_theme doc
      result = converter.apply_subs_discretely doc, input
      (expect result).to eql input
    end

    it 'should raise exception if an unsupported unit of measurement is passed to to_pt' do
      (expect do
        converter = (Asciidoctor.load 'yo', backend: :pdf).converter
        converter.to_pt 3, 'ft'
      end).to raise_exception ArgumentError, /unknown unit of measurement: ft/
    end

    it 'should return previous integer as string when pred is invoked on integer string' do
      {
        '1' => '0',
        '10' => '9',
        '0' => '-1',
        '-9' => '-10',
      }.each do |curr, pred|
        (expect curr.pred).to eql pred
      end
    end

    it 'should not delegate to formatter when parse_text is called without options' do
      doc = Asciidoctor.load 'text', backend: :pdf
      converter = doc.converter
      converter.init_pdf doc
      result = converter.parse_text 'text'
      (expect result).to eql [text: 'text']
    end

    it 'should not delegate to formatter with default options when parse_text is called with inline_format: true' do
      doc = Asciidoctor.load 'text', backend: :pdf
      converter = doc.converter
      converter.init_pdf doc
      result = converter.parse_text %(foo\n<strong>bar</strong>), inline_format: true
      (expect result).to eql [{ text: %(foo\n) }, { text: 'bar', styles: [:bold].to_set }]
    end

    it 'should not delegate to formatter with specified options when parse_text is called with inline_format: Array' do
      doc = Asciidoctor.load 'text', backend: :pdf
      converter = doc.converter
      converter.init_pdf doc
      result = converter.parse_text %(foo\n<strong>bar</strong>), inline_format: [normalize: true]
      (expect result).to eql [{ text: 'foo ' }, { text: 'bar', styles: [:bold].to_set }]
    end

    it 'should restore current column after float yields to current block' do
      doc = Asciidoctor.load 'text', backend: :pdf
      converter = doc.converter
      actual_column = nil
      last_visited_column = nil
      converter.instance_exec do
        init_pdf doc
        start_new_page
        column_box [bounds.left, cursor], width: bounds.width, columns: 2 do
          float do
            ink_prose 'before'
            bounds.move_past_bottom
            ink_prose 'after'
            last_visited_column = bounds.instance_variable_get :@current_column
          end
          actual_column = bounds.instance_variable_get :@current_column
        end
      end
      (expect actual_column).to eql 0
      (expect last_visited_column).to eql 1
    end

    it 'should short-circuit formatted_text and log error if text cannot not fit on new page' do
      doc = Asciidoctor.load 'text', backend: :pdf
      last_page = last_page_number = nil
      (expect do
        doc.converter.instance_exec do
          init_pdf doc
          start_new_page
          ink_prose 'before'
          formatted_text [{ text: 'x', ascender: bounds.height, descender: font.descender }]
          last_page = page
          last_page_number = page_number
        end
      end).to log_message severity: :ERROR, message: 'cannot fit formatted text on page: x'
      (expect last_page).to be_empty
      (expect last_page_number).to eql 2
    end
  end

  describe '#next_enclosed_block' do
    let(:doc) { Asciidoctor.load input_source, backend: 'pdf' }
    let(:converter) { doc.converter }
    let(:result) { converter.next_enclosed_block start }

    context 'of document' do
      let(:input_source) { 'paragraph' }
      let(:start) { doc }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last block in document' do
      let(:input_source) { 'paragraph' }
      let(:start) { doc.blocks[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of block followed by block' do
      let :input_source do
        <<~'EOS'
        first paragraph

        second paragraph
        EOS
      end

      let(:start) { doc.blocks[0] }

      it('should be next block') { (expect result).to eql doc.blocks[1] }
    end

    context 'of last block in open block followed by block' do
      let :input_source do
        <<~'EOS'
        first paragraph

        --
        second paragraph
        --

        third paragraph
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[1] }

      it('should be next block adjacent to open block') { (expect result).to eql doc.blocks[2] }
    end

    context 'of last block before parent section' do
      let :input_source do
        <<~'EOS'
        == First Section

        paragraph

        == Second Section
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be next section') { (expect result).to eql doc.sections[1] }
    end

    context 'of last block before subsection' do
      let :input_source do
        <<~'EOS'
        == Section

        paragraph

        === Subsection
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be next section') { (expect result).to eql (doc.find_by context: :section)[-1] }
    end

    context 'of last block before grandparent section' do
      let :input_source do
        <<~'EOS'
        == First Section

        paragraph

        === Subsection

        paragraph

        == Last Section
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[-1] }

      it('should be next section') { (expect result).to eql (doc.find_by context: :section)[-1] }
    end

    context 'of preamble' do
      let :input_source do
        <<~'EOS'
        = Document Title

        preamble

        == First Section
        EOS
      end

      let(:start) { (doc.find_by context: :preamble)[0] }

      it('should be first section') { (expect result).to eql doc.sections[0] }
    end

    context 'of abstract' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        abstract
        --

        == First Section
        EOS
      end

      let(:start) { (doc.find_by context: :open, style: 'abstract')[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of abstract followed by more preamble' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        abstract
        --

        more preamble

        == First Section
        EOS
      end

      let(:start) { (doc.find_by context: :open, style: 'abstract')[0] }

      it('should be next block in preamble') { (expect result).to eql (doc.find_by context: :paragraph)[1] }
    end

    context 'of last block in abstract' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        abstract
        --

        == First Section
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last block inside abstract followed by more preamble' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        abstract
        --

        more preamble

        == First Section
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last block inside delimited block' do
      let :input_source do
        <<~'EOS'
        ****
        inside paragraph
        ****

        outside paragraph
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of list followed by block' do
      let :input_source do
        <<~'EOS'
        * list item

        paragraph
        EOS
      end

      let(:start) { doc.blocks[0] }

      it('should be next block adjacent to list') { (expect result).to eql doc.blocks[1] }
    end

    context 'of first list item in list' do
      let :input_source do
        <<~'EOS'
        * yin
        * yang
        EOS
      end

      let(:start) { doc.blocks[0].items[0] }

      it('should be next list item') { (expect result).to eql doc.blocks[0].items[1] }
    end

    context 'of last list item in list' do
      let :input_source do
        <<~'EOS'
        * yin
        * yang

        paragraph
        EOS
      end

      let(:start) { doc.blocks[0].items[-1] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last attached block in first item in list' do
      let :input_source do
        <<~'EOS'
        * moon
        +
        stars
        * sun
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be next list item') { (expect result).to eql doc.blocks[0].items[-1] }
    end

    context 'of last attached block in last item in list' do
      let :input_source do
        <<~'EOS'
        * sun
        * moon
        +
        stars

        paragraph
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last block in open block attached to first item in list' do
      let :input_source do
        <<~'EOS'
        * moon
        +
        --
        light side

        dark side
        --
        * sun
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[1] }

      it('should be next list item') { (expect result).to eql doc.blocks[0].items[-1] }
    end

    context 'of last item in nested list of first item in list' do
      let :input_source do
        <<~'EOS'
        * sun
         ** star
        * moon
        EOS
      end

      let(:start) { (doc.find_by context: :list_item)[1] }

      it('should be next top-level list item') { (expect result).to eql doc.blocks[0].items[-1] }
    end

    context 'of last item in nested list of last item in list' do
      let :input_source do
        <<~'EOS'
        * moon
        * sun
         ** star

        paragraph
        EOS
      end

      let(:start) { (doc.find_by context: :list_item)[2] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last item in deeply nested list of first item in list' do
      let :input_source do
        <<~'EOS'
        * foo
         ** bar
          *** baz
        * moon
        EOS
      end

      let(:start) { (doc.find_by context: :list_item)[2] }

      it('should be next top-level list item') { (expect result).to eql doc.blocks[0].items[-1] }
    end

    context 'of term of first item in dlist' do
      let :input_source do
        <<~'EOS'
        foo:: bar
        EOS
      end

      let(:start) { (doc.find_by context: :list_item)[0] }

      it('should be desc of current item') { (expect result).to eql (doc.find_by context: :list_item)[1] }
    end

    context 'of desc text of first item in dlist' do
      let :input_source do
        <<~'EOS'
        foo:: bar
        yin:: yang
        EOS
      end

      let(:start) { (doc.find_by context: :list_item)[1] }

      it('should be term of next item') { (expect result).to eql (doc.find_by context: :list_item)[2] }
    end

    context 'of desc text of last item in dlist' do
      let :input_source do
        <<~'EOS'
        foo:: bar
        yin:: yang

        paragraph
        EOS
      end

      let(:start) { (doc.find_by context: :list_item)[3] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of attached block in last item in dlist' do
      let :input_source do
        <<~'EOS'
        foo:: bar
        sun:: moon
        +
        stars

        paragraph
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of missing block' do
      let :input_source do
        <<~'EOS'
        foo:: bar
        yin:: yang
        EOS
      end

      let :start do
        list_items = (doc.find_by context: :list_item)
        list_items[0].parent.items[0].replace [[list_items[0].dup], list_items[1].dup]
        list_items[1]
      end

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of preamble followed by section' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        A glimpse at what is to come.
        --

        == Intro
        EOS
      end

      let(:start) { (doc.find_by context: :open)[0].parent }

      it('should be next section') { (expect result).to eql doc.sections[-1] }
    end

    context 'of paragraph in abstract followed by section' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        A glimpse at what is to come.
        --

        == Intro
        EOS
      end

      let(:start) { (doc.find_by context: :paragraph)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of quote block in abstract followed by section' do
      let :input_source do
        <<~'EOS'
        = Document Title

        [abstract]
        --
        ____
        A glimpse at what is to come.
        ____
        --

        == Intro
        EOS
      end

      let(:start) { (doc.find_by context: :quote)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last block in AsciiDoc table cell' do
      let :input_source do
        <<~'EOS'
        [cols=2*]
        |===
        a|
        foo

        bar

        ____
        quote
        ____

        | another cell
        |===

        after
        EOS
      end

      let(:start) { (doc.find_by context: :quote, traverse_documents: true)[0] }

      it('should be nil') { (expect result).to be_nil }
    end

    context 'of last block in description of first item in horizontal dlist' do
      example_group = self

      let :input_source do
        <<~'EOS'
        .Title
        [horizontal]
        term:: desc
        +
        [quote]
        capture:quote[]

        another term::
        EOS
      end

      let :doc do
        Asciidoctor.load input_source, backend: 'pdf', extensions: (proc do
          inline_macro :capture do
            process do |parent, target|
              example_group.let(:captured_block) { parent }
              create_inline parent, :quoted, target
            end
          end
        end)
      end

      let :start do
        doc.convert
        captured_block
      end

      it('should be next term') { (expect result).to eql (doc.find_by context: :list_item)[-1] }
    end

    context 'of last block in description of last item in horizontal dlist' do
      example_group = self

      let :input_source do
        <<~'EOS'
        .Title
        [horizontal]
        term:: desc
        +
        [quote]
        capture:quote[]

        after
        EOS
      end

      let :doc do
        Asciidoctor.load input_source, backend: 'pdf', extensions: (proc do
          inline_macro :capture do
            process do |parent, target|
              example_group.let(:captured_block) { parent }
              create_inline parent, :quoted, target
            end
          end
        end)
      end

      let :start do
        doc.convert
        captured_block
      end

      it('should be nil') { (expect result).to be_nil }
    end
  end

  describe 'Bounding Box' do
    it 'should use correct left value when creating bounding box', visual: true do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def traverse node
          return super unless node.context == :document
          column_box [0, cursor], columns: 2, width: bounds.width, reflow_margins: true, spacer: 12 do
            bounds.move_past_bottom
            super
          end
        end
      end

      pdf_theme = { caption_background_color: 'EEEEEE' }
      input = <<~'EOS'
      = Article Title

      * list item

      NOTE: admonition

      > quote

      .Block title
      ----
      code block <1>
      ----
      <1> Callout description
      EOS

      (expect to_pdf_file input, 'bounding-box-left.pdf', backend: backend, pdf_theme: pdf_theme).to visually_match 'bounding-box-left.pdf'
    end

    it 'should not reflow margins on column box if reflow_margins option is not set' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def traverse node
          return super unless node.context == :document
          column_box [0, cursor], columns: 2, width: bounds.width, spacer: 12 do
            super
          end
        end
      end

      input = <<~'EOS'
      = Article Title

      column 1, page 1

      [.column]
      <<<

      column 2, page 1

      [.column]
      <<<

      column 1, page 2
      EOS

      pdf = to_pdf input, backend: backend, analyze: true
      (expect (pdf.find_unique_text 'column 1, page 2')[:page_number]).to eql 2
      (expect (pdf.find_unique_text 'column 1, page 2')[:y]).to eql (pdf.find_unique_text 'column 1, page 1')[:y]
    end
  end

  describe 'extend' do
    it 'should use specified extended converter' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def convert_paragraph node
          layout_prose node.content, anchor: 'next-section'
        end
      end

      input = <<~'EOS'
      see next section

      [#next-section]
      == Next Section
      EOS

      pdf = to_pdf input, backend: backend, analyze: true
      para_text = pdf.find_unique_text 'see next section'
      (expect para_text[:font_color]).to eql '428BCA'

      pdf = to_pdf input, backend: backend
      (expect get_names pdf).to have_key 'next-section'
      annotations = get_annotations pdf, 1
      (expect annotations).to have_size 1
      link_annotation = annotations[0]
      (expect link_annotation[:Subtype]).to be :Link
      (expect link_annotation[:Dest]).to eql 'next-section'
    end

    it 'should allow extended converter to invoke layout_heading without any opts' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def convert_paragraph node
          layout_heading %(#{node.role.capitalize} Heading) if node.role?
          super
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, pdf_theme: { heading_margin_bottom: 0, heading_margin_top: 100 }, analyze: true
      [.first]
      paragraph

      [.second]
      paragraph
      EOS

      first_heading_text = pdf.find_unique_text 'First Heading'
      (expect first_heading_text).not_to be_nil
      (expect first_heading_text[:font_size]).to eql 10.5
      (expect first_heading_text[:font_color]).to eql '333333'
      second_heading_text = pdf.find_unique_text 'Second Heading'
      (expect second_heading_text).not_to be_nil
      (expect second_heading_text[:font_size]).to eql 10.5
      (expect second_heading_text[:font_color]).to eql '333333'
      (expect second_heading_text[:y]).to be < 700
      text = pdf.text
      (expect text[0][:y] - text[1][:y]).to be < text[1][:y] - text[2][:y]
    end

    it 'should allow custom converter to invoke layout_heading with opts' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def convert_paragraph node
          if node.has_role? 'heading'
            layout_heading node.source, text_transform: 'uppercase', size: 100, color: 'AA0000', line_height: 1.2, margin: 20
          else
            super
          end
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: true
      before

      [.heading]
      heading

      paragraph
      EOS

      heading_text = pdf.find_unique_text 'HEADING'
      (expect heading_text).not_to be_nil
      (expect heading_text[:font_size]).to eql 100
      (expect heading_text[:font_color]).to eql 'AA0000'
      (expect heading_text[:y].floor).to eql 650
      (expect (pdf.find_unique_text 'paragraph')[:y].floor).to eql 588
    end

    it 'should allow custom converter to invoke layout_general_heading without any opts' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def convert_paragraph node
          layout_general_heading node, %(#{node.role.capitalize} Heading) if node.role?
          super
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, pdf_theme: { heading_margin_bottom: 0, heading_margin_top: 100 }, analyze: true
      [.first]
      paragraph

      [.second]
      paragraph
      EOS

      first_heading_text = pdf.find_unique_text 'First Heading'
      (expect first_heading_text).not_to be_nil
      (expect first_heading_text[:font_size]).to eql 10.5
      (expect first_heading_text[:font_color]).to eql '333333'
      second_heading_text = pdf.find_unique_text 'Second Heading'
      (expect second_heading_text).not_to be_nil
      (expect second_heading_text[:font_size]).to eql 10.5
      (expect second_heading_text[:font_color]).to eql '333333'
      (expect second_heading_text[:y]).to be < 700
      text = pdf.text
      (expect text[0][:y] - text[1][:y]).to be < text[1][:y] - text[2][:y]
    end

    it 'should allow custom converter to invoke layout_general_heading with opts' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def convert_paragraph node
          if node.has_role? 'heading'
            layout_general_heading node, node.source, text_transform: 'uppercase', size: 100, color: 'AA0000', line_height: 1.2, margin: 20
          else
            super
          end
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: true
      before

      [.heading]
      heading

      paragraph
      EOS

      heading_text = pdf.find_unique_text 'HEADING'
      (expect heading_text).not_to be_nil
      (expect heading_text[:font_size]).to eql 100
      (expect heading_text[:font_color]).to eql 'AA0000'
      (expect heading_text[:y].floor).to eql 650
      (expect (pdf.find_unique_text 'paragraph')[:y].floor).to eql 588
    end

    it 'should allow custom converter to override layout_general_heading for section title' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def layout_general_heading node, title, opts = {}
          layout_heading title, (opts.merge transform: (node.attr :transform).to_sym)
        end

        def layout_heading title, opts
          title = title.send opts.delete :transform
          super
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: true
      [transform=upcase]
      == Section Title
      EOS

      heading_text = pdf.find_unique_text 'SECTION TITLE'
      (expect heading_text).not_to be_nil
    end

    it 'should allow custom converter to override ink_general_heading for section title' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def ink_general_heading sect, title, opts = {}
          if (image_path = sect.attr 'image')
            image_attrs = { 'target' => image_path, 'pdfwidth' => '1in' }
            image_block = Asciidoctor::Block.new sect.document, :image, content_model: :empty, attributes: image_attrs
            convert_image image_block, relative_to_imagesdir: true, pinned: true
          end
          super
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: :image
      [image=tux.png]
      == Section Title
      EOS

      (expect pdf.images).to have_size 1
    end

    it 'should allow custom converter to override ink_general_heading for article doctitle' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
        def ink_general_heading _sect, title, opts = {}
          return super unless opts[:role] == :doctitle
          theme_font :heading_doctitle do
            ink_prose title, align: :center, margin: 0
          end
          theme_margin :heading_doctitle, :bottom
        end
      end

      pdf_theme = { heading_doctitle_font_color: '0000EE', heading_doctitle_margin_bottom: 24 }
      pdf = to_pdf <<~'EOS', backend: backend, pdf_theme: pdf_theme, analyze: true
      = Article Title

      First paragraph of body.
      First paragraph of body.
      First paragraph of body.
      First paragraph of body.
      EOS

      (expect pdf.pages).to have_size 1
      title_text = pdf.find_unique_text 'Article Title'
      (expect title_text[:font_color]).to eql '0000EE'
      para_text = pdf.text[1]
      (expect title_text[:y] - (para_text[:y] + para_text[:font_size])).to be > 24
    end

    it 'should remap layout_ methods added by prepended module' do
      backend = nil
      converter_class = create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)
      end
      converter_class.prepend (Module.new do
        def layout_prose string, opts = {}
          opts[:color] = 'FF0000'
          super
        end
      end)

      pdf = to_pdf 'color me red', backend: backend, analyze: true
      text = pdf.text
      (expect text).to have_size 1
      (expect text[0][:font_color]).to eql 'FF0000'
    end

    it 'should allow extended converter to flag page as imported to suppress running content' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)

        def ink_part_title sect, title, opts
          super
          page.imported
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, enable_footer: true, analyze: true
      = Document Title
      :doctype: book

      = Part Title

      == Chapter
      EOS

      page_2_text = pdf.find_text page_number: 2
      (expect page_2_text).to have_size 1
      (expect page_2_text[0][:string]).to eql 'Part Title'
      (expect (pdf.find_text page_number: 3).last[:string]).to eql '2'
    end

    it 'should allow extended converter to override convert_listing_or_literal to handle calls to convert_listing and convert_literal' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)

        def convert_listing_or_literal node
          node.lines[0] = node.lines[0].sub 'Ruby', 'World'
          super
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: true
      [,ruby]
      ----
      puts "Hello, Ruby!"
      ----
      EOS

      (expect pdf.text[0][:string]).to eql 'puts "Hello, World!"'
    end

    it 'should allow extended converter to override convert_code to handle calls to convert_listing and convert_literal' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)

        def convert_code node
          node.lines[0] = node.lines[0].sub 'Ruby', 'World'
          super
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: true
      [,ruby]
      ----
      puts "Hello, Ruby!"
      ----
      EOS

      (expect pdf.text[0][:string]).to eql 'puts "Hello, World!"'
    end

    it 'should allow extended converter to temporarily override theme using save_theme' do
      backend = nil
      create_class (Asciidoctor::Converter.for 'pdf') do
        register_for (backend = %(pdf#{object_id}).to_sym)

        def convert_table node
          if node.role?
            save_theme do
              theme.table_border_color = theme.table_grid_color = '0000EE'
              super
            end
          else
            super
          end
        end
      end

      pdf = to_pdf <<~'EOS', backend: backend, analyze: :line
      [.custom,cols=2*]
      |===
      |a |b
      |c |d
      |===

      <<<

      [cols=2*]
      |===
      |a |b
      |c |d
      |===
      EOS

      lines = pdf.lines
      custom_lines = lines.select {|it| it[:color] == '0000EE' }
      default_lines = lines.reject {|it| it[:color] == '0000EE' }
      (expect custom_lines).to have_size 16
      (expect custom_lines[0][:page_number]).to eql 1
      (expect default_lines).to have_size 16
      (expect default_lines[0][:page_number]).to eql 2
    end
  end
end