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
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
|
# frozen_string_literal: true
require_relative 'spec_helper'
describe 'Asciidoctor::PDF::Converter - Source' do
context 'Rouge' do
it 'should use plain text lexer if language is not recognized' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,foobar]
----
puts "Hello, World!"
----
END
puts_text = (pdf.find_text 'puts')[0]
(expect puts_text).to be_nil
(expect pdf.text).to have_size 1
(expect pdf.text[0][:font_color]).to eql '333333'
end
it 'should expand tabs to preserve indentation' do
pdf = to_pdf <<~END, analyze: true
:source-highlighter: rouge
[source,c]
----
int main() {
\tevent_loop();
\treturn 0;
}
----
END
lines = pdf.lines
(expect lines).to have_size 4
(expect lines[1]).to eql %(\u00a0 event_loop();)
(expect lines[2]).to eql %(\u00a0 return 0;)
end
it 'should expand tabs used for column alignment' do
pdf = to_pdf <<~END, analyze: true
:source-highlighter: rouge
[source,sql]
----
SELECT
\tname,\t firstname,\t\tlastname
FROM
\tusers
WHERE
\tusername\t=\t'foobar'
----
END
lines = pdf.lines
(expect lines).to have_size 6
(expect lines).to include %(\u00a0 name, firstname, lastname)
(expect lines).to include %(\u00a0 username = 'foobar')
end
it 'should enable start_inline option for PHP by default' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,php]
----
echo "<?php";
----
END
echo_text = (pdf.find_text 'echo')[0]
(expect echo_text).not_to be_nil
# NOTE: the echo keyword should be highlighted
(expect echo_text[:font_color]).to eql '008800'
end
it 'should not enable the start_inline option for PHP if the mixed option is set' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source%mixed,php]
----
echo "<?php";
----
END
echo_text = (pdf.find_text 'echo')[0]
# NOTE: the echo keyword should not be highlighted
(expect echo_text).to be_nil
end
it 'should preserve cgi-style options when setting start_inline option for PHP' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,php?funcnamehighlighting=1]
----
cal_days_in_month(CAL_GREGORIAN, 6, 2019)
----
[source,php?funcnamehighlighting=0]
----
cal_days_in_month(CAL_GREGORIAN, 6, 2019)
----
END
if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
ref_funcname_text = (pdf.find_text 'cal_days_in_month')[0]
(expect ref_funcname_text).not_to be_nil
ref_year_text = (pdf.find_text '2019')[0]
(expect ref_year_text).not_to be_nil
funcname_text = (pdf.find_text 'cal_days_in_month')[1]
(expect funcname_text).not_to be_nil
year_text = (pdf.find_text '2019')[1]
(expect year_text).not_to be_nil
(expect funcname_text[:font_color]).not_to eql ref_funcname_text[:font_color]
(expect funcname_text[:font_name]).not_to eql ref_funcname_text[:font_name]
(expect year_text[:font_color]).to eql ref_year_text[:font_color]
(expect year_text[:font_name]).to eql ref_year_text[:font_name]
else
text = pdf.text
(expect text).to have_size 2
(expect text[0][:string]).to eql 'cal_days_in_month(CAL_GREGORIAN, 6, 2019)'
(expect text[0][:font_color]).to eql '333333'
(expect text[1][:string]).to eql 'cal_days_in_month(CAL_GREGORIAN, 6, 2019)'
(expect text[1][:font_color]).to eql '333333'
end
end
it 'should enable start_inline option for PHP if enabled by cgi-style option' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,php?start_inline=1]
----
echo "<?php";
----
END
if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
echo_text = pdf.find_unique_text 'echo'
(expect echo_text).not_to be_nil
# NOTE: the echo keyword should be highlighted
(expect echo_text[:font_color]).to eql '008800'
end
end
it 'should not enable the start_inline option for PHP if the mixed option is set and other cgi-style options specified' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source%mixed,php?foo=bar]
----
echo "<?php";
----
END
echo_text = pdf.find_unique_text 'echo'
# NOTE: the echo keyword should not be highlighted
(expect echo_text).to be_nil
end
it 'should not enable start_inline option for PHP if disabled by cgi-style option' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,php?start_inline=0]
----
cal_days_in_month(CAL_GREGORIAN, 6, 2019)
----
END
text = pdf.text
(expect text).to have_size 1
(expect text[0][:string]).to eql 'cal_days_in_month(CAL_GREGORIAN, 6, 2019)'
(expect text[0][:font_color]).to eql '333333'
end
it 'should respect cgi-style options for languages other than PHP' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,console?prompt=%]
----
% bundle
----
END
if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
prompt_text = pdf.find_unique_text '%'
(expect prompt_text).not_to be_nil
(expect prompt_text[:font_color]).to eql '555555'
end
end
it 'should use plain text lexer if language is not recognized and cgi-style options are present' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,foobar?start_inline=1]
----
puts "Hello, World!"
----
END
puts_text = (pdf.find_text 'puts')[0]
(expect puts_text).to be_nil
(expect pdf.text).to have_size 1
(expect pdf.text[0][:font_color]).to eql '333333'
end
it 'should use rouge style specified by rouge-style attribute', visual: true do
input = <<~'END'
:source-highlighter: rouge
:rouge-style: molokai
[source,js]
----
'use strict'
const TAG_ALL_RX = /<[^>]+>/g
module.exports = (html) => html && html.replace(TAG_ALL_RX, '')
----
END
to_pdf input
expected_file = (Gem::Version.new Rouge.version) >= (Gem::Version.new '4.1.0') ?
'source-rouge-style-with-highlighted-method-name.pdf' :
'source-rouge-style.pdf'
to_file = to_pdf_file input, 'source-rouge-style.pdf'
(expect to_file).to visually_match expected_file
to_file = to_pdf_file input, 'source-rouge-style.pdf', attribute_overrides: { 'rouge-style' => (Rouge::Theme.find 'molokai').new }
(expect to_file).to visually_match expected_file
to_file = to_pdf_file input, 'source-rouge-style.pdf', attribute_overrides: { 'rouge-style' => (Rouge::Theme.find 'molokai') }
(expect to_file).to visually_match expected_file
end
it 'should disable highlighting instead of crashing if lexer fails to lex source' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,console]
----
$ cd application-name\bin\
----
END
source_lines = pdf.lines pdf.text {|it| it.font_name.start_with? 'mplus1mn-' }
(expect source_lines).not_to be_empty
(expect source_lines[0]).to start_with '$ cd'
end).not_to raise_exception
end
it 'should not crash if source-highlighter attribute is defined outside of document header' do
(expect do
pdf = to_pdf <<~'END', analyze: true
= Document Title
:source-highlighter: rouge
[source,ruby]
----
puts 'yo, world!'
----
END
source_text = pdf.find_unique_text font_name: 'mplus1mn-regular'
(expect source_text).not_to be_nil
(expect source_text[:string]).to start_with 'puts '
end).not_to raise_exception
end
it 'should apply bw style if specified' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
:rouge-style: bw
[source,ruby]
----
class Beer
attr_reader :style
end
----
END
beer_text = (pdf.find_text 'Beer')[0]
(expect beer_text).not_to be_nil
(expect beer_text[:font_name]).to eql 'mplus1mn-bold'
if (Gem::Version.new Rouge.version) >= (Gem::Version.new '3.4.0')
(expect beer_text[:font_color]).to eql '333333'
else
(expect beer_text[:font_color]).to eql 'BB0066'
end
end
it 'should allow token to be formatted in bold, italic, and bold italic' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
:rouge-style: monokai
[source,d]
----
int #line 6 "pkg/mod.d"
x; // this is now line 6 of file pkg/mod.d
----
END
line_text = pdf.find_unique_text 'int'
(expect line_text).not_to be_nil
(expect line_text[:font_name]).to eql 'mplus1mn-bold'
line_text = pdf.find_unique_text %r/^#line 6 /
(expect line_text).not_to be_nil
(expect line_text[:font_name]).to eql 'mplus1mn-bold_italic'
if (line_text = pdf.find_unique_text %r/^\/\/ this is now /)
(expect line_text[:font_name]).to eql 'mplus1mn-italic'
end
end
it 'should allow token to add underline style to token', visual: true do
input = <<~'END'
:source-highlighter: rouge
[source,ruby]
----
class Beer
attr_reader :style
def drink
puts 'aaaaaaaaah'
end
end
----
END
# NOTE: convert to load Rouge
to_pdf input
rouge_style = Class.new Rouge::Theme.find 'molokai' do
style Rouge::Token::Tokens::Name::Class, fg: :green, bold: true, underline: true
style Rouge::Token::Tokens::Name::Function, fg: :green, underline: true
end
to_file = to_pdf_file input, 'source-rouge-underline-style.pdf', attribute_overrides: { 'rouge-style' => rouge_style }
(expect to_file).to visually_match 'source-rouge-underline-style.pdf' if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
end
it 'should allow token to extend to width of block', visual: true do
input = <<~'END'
:source-highlighter: rouge
:rouge-style: github
[source,ruby]
----
puts 'string'
----
END
# NOTE: convert to load Rouge
to_pdf input
rouge_style = Class.new Rouge::Theme.find 'github' do
style Rouge::Token::Tokens::Literal::String::Single, fg: '#333333', bg: '#ff4dcd', extend: true, inline_block: true
end
pdf = to_pdf input, attribute_overrides: { 'rouge-style' => rouge_style }, analyze: :rect
rects = pdf.rectangles
(expect rects).to have_size 1
(expect rects[0][:width]).to be > 44
(expect rects[0][:height]).to be > 11
end
it 'should not crash if theme does not define style for Text token' do
input = <<~'END'
:source-highlighter: rouge
[source,ruby]
----
puts "Hello, World!"
----
END
# NOTE: convert to load Rouge
to_pdf input
rouge_style = Class.new Rouge::CSSTheme do
name 'foobar'
style Rouge::Token::Tokens::Literal::String, italic: true
end
pdf = to_pdf input, attribute_overrides: { 'rouge-style' => rouge_style }, analyze: true
hello_world_text = pdf.find_unique_text '"Hello, World!"'
(expect hello_world_text[:font_name]).to eql 'mplus1mn-italic'
end
it 'should expand color value for token' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
:rouge-style: colorful
[source,ruby]
----
class Type; end
----
END
pdf.text.each do |text|
(expect text[:font_color].length).to be 6
(expect text[:font_color].upcase).to eql text[:font_color]
end
classname_text = pdf.find_unique_text 'Type'
(expect ((Rouge::Theme.find 'colorful').new.style_for Rouge::Token::Tokens::Name::Class)[:fg]).to eql '#B06'
(expect classname_text[:font_color]).to eql 'BB0066'
end
it 'should draw background color around token', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-bg.pdf'
:source-highlighter: rouge
:rouge-style: pastie
[source,ruby]
----
type, name = ARGV
case type
when :hello
puts sprintf %(Hello, %s!), name
when :goodbye
puts 'See ya, ' + name + '!'
end
----
END
(expect to_file).to visually_match 'source-rouge-bg.pdf' if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
end
it 'should draw background color across whole line for line-oriented tokens', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-bg-line.pdf'
:source-highlighter: rouge
[source,diff]
----
--- /tmp/list1.txt
+++ /tmp/list2.txt
@@ -1,4 +1,4 @@
apples
-oranges
kiwis
carrots
+grapefruits
----
END
(expect to_file).to visually_match 'source-rouge-bg-line.pdf' if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
end
it 'should not draw background color across whole line for line-oriented tokens if disabled in theme' do
input = <<~'END'
:source-highlighter: rouge
[source,diff]
----
-oranges
+grapefruits
----
END
# NOTE: convert to load Rouge
to_pdf input
rouge_style = Class.new Rouge::Theme.find 'asciidoctor_pdf_default' do
style Rouge::Token::Tokens::Generic::Deleted, fg: '#000000', bg: '#ffdddd', extend: false, inline_block: false
style Rouge::Token::Tokens::Generic::Inserted, fg: '#000000', bg: '#ddffdd', extend: false, inline_block: false
end
pdf = to_pdf input, attribute_overrides: { 'rouge-style' => rouge_style }, analyze: :rect
rects = pdf.rectangles
(expect rects).to have_size 2
(expect rects[0][:width]).to be < 100
# FIXME: the first token ends in a newline, so it gets coerced to an inline block
(expect rects[0][:height]).to eql 14.8
(expect rects[1][:height]).to be < 14.8
end
it 'should fall back to default line gap if line gap is not specified in theme', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-bg-line-no-gap.pdf', pdf_theme: { code_line_gap: nil }
:source-highlighter: rouge
[source,diff]
----
--- /tmp/list1.txt
+++ /tmp/list2.txt
@@ -1,4 +1,4 @@
apples
-oranges
kiwis
carrots
+grapefruits
----
END
(expect to_file).to visually_match 'source-rouge-bg-line-no-gap.pdf' if (Gem::Version.new Rouge.version) >= (Gem::Version.new '2.1.0')
end
it 'should add line numbers to start of line if linenums option is enabled' do
expected_lines = <<~'END'.split ?\n
1 <?xml version="1.0" encoding="UTF-8"?>
2 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3 <url>
4 <loc>https://example.org/home.html</loc>
5 <lastmod>2019-01-01T00:00:00.000Z</lastmod>
6 </url>
7 <url>
8 <loc>https://example.org/about.html</loc>
9 <lastmod>2019-01-01T00:00:00.000Z</lastmod>
10 </url>
11 </urlset>
END
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,xml,linenums]
----
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.org/home.html</loc>
<lastmod>2019-01-01T00:00:00.000Z</lastmod>
</url>
<url>
<loc>https://example.org/about.html</loc>
<lastmod>2019-01-01T00:00:00.000Z</lastmod>
</url>
</urlset>
----
END
(expect pdf.lines).to eql expected_lines
linenum_text = (pdf.find_text %r/^11 *$/)[0]
(expect linenum_text[:font_color]).to eql '888888'
end
it 'should continue to add line numbers after page split' do
source_lines = (1..55).map {|it| %(puts "Please come forward if your number is #{it}.") }
pdf = to_pdf <<~END, analyze: true
:source-highlighter: rouge
[source%linenums,ruby]
----
#{source_lines.join ?\n}
----
END
lines_after_split = pdf.lines pdf.find_text page_number: 2
(expect lines_after_split).not_to be_empty
(expect lines_after_split[0]).to eql '51 puts "Please come forward if your number is 51."'
end
it 'should honor start value for line numbering' do
expected_lines = <<~'END'.split ?\n
5 puts 'Hello, World!'
END
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,xml,linenums,start=5]
----
puts 'Hello, World!'
----
END
(expect pdf.lines).to eql expected_lines
end
it 'should coerce start value for line numbering to 1 if less than 1' do
expected_lines = <<~'END'.split ?\n
1 puts 'Hello, World!'
END
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,xml,linenums,start=0]
----
puts 'Hello, World!'
----
END
(expect pdf.lines).to eql expected_lines
end
it 'should not add line number to first line if source is empty' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source%linenums]
----
----
END
(expect pdf.text).to be_empty
end
it 'should not emit error if linenums are enabled and language is not set' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source%linenums]
----
fee
fi
fo
fum
----
END
(expect pdf.lines).to eql ['1 fee', '2 fi', '3 fo', '4 fum']
end).to not_log_message
end
it 'should preserve orphan callout on last line' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,yaml]
----
foo: 'bar'
key: 'value'
<1>
----
<1> End the file with a trailing newline
END
conum_texts = pdf.find_text '①'
(expect conum_texts).to have_size 2
end
it 'should use font color from style' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
:rouge-style: monokai
[source,text]
----
foo
bar
baz
----
END
pdf.text.each do |text|
(expect text[:font_color]).to eql 'F8F8F2'
end
end
it 'should highlight lines specified by highlight attribute on block', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-line-highlighting.pdf'
:source-highlighter: rouge
[source,c,highlight=4;7-8]
----
/**
* A program that prints "Hello, World!"
**/
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
----
END
(expect to_file).to visually_match 'source-rouge-line-highlighting.pdf'
end
it 'should highlight lines specified by highlight attribute on block when linenums are enabled', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-line-highlighting-with-linenums.pdf'
:source-highlighter: rouge
[source,c,linenums,highlight=4;7-8]
----
/**
* A program that prints "Hello, World!"
**/
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
----
END
(expect to_file).to visually_match 'source-rouge-line-highlighting-with-linenums.pdf'
end
it 'should interpret highlight lines relative to start value', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-line-highlighting-with-linenums-start.pdf'
:source-highlighter: rouge
[source,c,linenums,start=4,highlight=4;7-8]
----
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
----
END
(expect to_file).to visually_match 'source-rouge-line-highlighting-with-linenums-start.pdf'
end
it 'should preserve indentation when highlighting lines without linenums enabled', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-line-highlighting-indent.pdf'
:source-highlighter: rouge
[source,groovy,highlight=4-5]
----
ratpack {
handlers {
get {
render '''|Hello,
|World!'''.stripMargin()
}
}
}
----
END
(expect to_file).to visually_match 'source-rouge-line-highlighting-indent.pdf'
end
it 'should ignore highlight attribute if empty' do
pdf = to_pdf <<~'END', analyze: :rect
:source-highlighter: rouge
[source,ruby,linenums,highlight=]
----
puts "Hello, World!"
----
END
(expect pdf.rectangles).to be_empty
end
it 'should preserve indentation of highlighted line' do
input = <<~'END'
:source-highlighter: rouge
[source,text,highlight=1]
----
indented line
----
END
pdf = to_pdf input, analyze: true
(expect pdf.lines).to eql [%(\u00a0 indented line)]
pdf = to_pdf input, analyze: :rect
(expect pdf.rectangles).to have_size 1
end
it 'should highlight lines using custom color specified in theme', visual: true do
pdf_theme = { code_highlight_background_color: 'FFFF00' }
to_file = to_pdf_file <<~'END', 'source-rouge-highlight-background-color.pdf', pdf_theme: pdf_theme
:source-highlighter: rouge
[source,c,highlight=4]
----
/**
* A program that prints "Hello, World!"
**/
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
----
END
(expect to_file).to visually_match 'source-rouge-highlight-background-color.pdf'
end
it 'should indent wrapped line if line numbers are enabled' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,text,linenums]
----
Here we go again here we go again here we go again here we go again here we go again Here we go again
----
END
linenum_text = (pdf.find_text '1 ')[0]
(expect linenum_text[:x]).not_to be_nil
start_texts = pdf.find_text %r/^Here we go again/
(expect start_texts).to have_size 2
(expect start_texts[0][:x]).to eql start_texts[1][:x]
(expect start_texts[0][:x]).to be > linenum_text[:x]
indent_texts = pdf.find_text %r/\u00a0/
(expect indent_texts).to have_size 1
(expect indent_texts[0][:x]).to eql linenum_text[:x]
(expect indent_texts[0][:string]).to eql %(\u00a0 )
end
it 'should indent wrapped line if line numbers are enabled and block has an AFM font' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
:pdf-theme: base
[,text,linenums]
----
Here we go again here we go again here we go again here we go again here we go again Here we go again
----
END
linenum_text = (pdf.find_text '1 ')[0]
(expect linenum_text[:x]).not_to be_nil
start_texts = pdf.find_text %r/Here we go/
(expect start_texts).to have_size 2
(expect start_texts[0][:x]).to eql start_texts[1][:x]
(expect start_texts[0][:x]).to be > linenum_text[:x]
indent_texts = pdf.find_text %r/\u00a0/
(expect indent_texts).to have_size 1
(expect indent_texts[0][:x]).to eql linenum_text[:x]
(expect indent_texts[0][:string]).to eql %(\u00a0 )
end
it 'should highlight and indent wrapped line', visual: true do
to_file = to_pdf_file <<~'END', 'source-rouge-highlight-wrapped-line.pdf'
:source-highlighter: rouge
[source,xml,linenums,highlight=1;3]
----
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
</project>
----
END
(expect to_file).to visually_match 'source-rouge-highlight-wrapped-line.pdf'
end
it 'should not apply syntax highlighting or borders and backgrounds in scratch document' do
scratch_pdf = nil
postprocessor_impl = proc do
process do |doc, output|
scratch_pdf = doc.converter.scratch
output
end
end
opts = { extension_registry: Asciidoctor::Extensions.create { postprocessor(&postprocessor_impl) } }
main_pdf = to_pdf <<~'END', (opts.merge analyze: true)
:source-highlighter: rouge
filler
[%unbreakable]
--
[source,ruby]
----
puts "Hello, World!"
----
--
END
main_pdf_text = main_pdf.text.reject {|it| it[:string] == 'filler' }
(expect main_pdf_text[0][:string]).to eql 'puts'
(expect main_pdf_text[0][:font_color]).not_to eql '333333'
scratch_pdf_output = scratch_pdf.render
scratch_pdf_text = (TextInspector.analyze scratch_pdf_output).text
(expect scratch_pdf_text[0][:string]).to eql 'puts "Hello, World!"'
(expect scratch_pdf_text[0][:font_color]).to eql '333333'
scratch_pdf_lines = (LineInspector.analyze scratch_pdf_output).lines
(expect scratch_pdf_lines).to be_empty
end
it 'should not leak patch for linenums if unbreakable block is split across pages' do
formatted_text_box_extensions_count = nil
extensions = proc do
postprocessor do
process do |_, output|
formatted_text_box_extensions_count = Prawn::Text::Formatted::Box.extensions.size
output
end
end
end
source_file = fixture_file 'TicTacToeGame.java'
pdf = to_pdf <<~END, extensions: extensions, enable_footer: true, analyze: true
:source-highlighter: rouge
before block
[%linenums%autofit%unbreakable,java]
----
include::#{source_file}[]
----
END
(expect (pdf.find_unique_text 'before block')[:page_number]).to be 1
(expect (pdf.find_unique_text 'package')[:page_number]).to be 1
(expect (pdf.find_unique_text 'package')[:font_color]).not_to be '333333'
(expect (pdf.find_unique_text %r/^\s*70\s*$/)[:page_number]).to be 2
(expect formatted_text_box_extensions_count).to be 0
end
it 'should break and wrap numbered line if text does not fit on a single line' do
input = <<~END
:source-highlighter: rouge
[%linenums,text]
----
y#{'o' * 100}
----
END
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 1
linenum_text = pdf.find_unique_text '1 '
(expect linenum_text).not_to be_nil
first_line_text = pdf.find_unique_text %r/^yo/
wrapped_text = pdf.find_unique_text %r/^ooo/
(expect linenum_text[:x]).to be < first_line_text[:x]
(expect linenum_text[:y]).to eql first_line_text[:y]
(expect linenum_text[:x]).to be < wrapped_text[:x]
(expect wrapped_text[:x]).to eql first_line_text[:x]
(expect linenum_text[:y]).to be > wrapped_text[:y]
lines = (to_pdf input, pdf_theme: { code_border_radius: 0, code_border_width: [1, 0] }, analyze: :line).lines
(expect (lines[0][:from][:y] - lines[1][:from][:y]).abs).to (be_within 2).of 50
end
it 'should break and wrap numbered line if indented text does not fit on a single line' do
input = <<~END
:source-highlighter: rouge
[%linenums,text]
----
before
#{' ' * 2}y#{'o' * 100}
after
----
END
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 1
text_lines = pdf.lines pdf.text
# FIXME: we lose the indentation on the second line, but that's true of plain listing blocks too
expected_lines = <<~END.chomp.split ?\n
1 before
2
\u00a0 yooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
\u00a0 ooooooooooooooooo
3 after
END
(expect text_lines).to eql expected_lines
end
it 'should break and wrap numbered line if text wraps but still does not fit on a single line' do
input = <<~END
:source-highlighter: rouge
[%linenums,text]
----
one
two and then s#{'o' * 100}me
three
----
END
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 1
text_lines = pdf.lines pdf.text
expected_lines = <<~END.chomp.split ?\n
1 one
2 two and then
\u00a0 sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
\u00a0 ooooooooooooooooome
3 three
END
(expect text_lines).to eql expected_lines
end
it 'should not apply syntax highlighting if specialchars sub is disabled' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,ruby,subs=-specialchars]
----
puts "Hello, World!"
----
END
text = pdf.text
(expect text).to have_size 1
(expect text[0][:string]).to eql 'puts "Hello, World!"'
(expect text[0][:font_color]).to eql '333333'
end
it 'should not apply syntax highlighting in scratch document if specialchars sub is disabled' do
scratch_pdf = nil
postprocessor_impl = proc do
process do |doc, output|
scratch_pdf = doc.converter.scratch
output
end
end
opts = { extension_registry: Asciidoctor::Extensions.create { postprocessor(&postprocessor_impl) } }
pdf = to_pdf <<~'END', (opts.merge analyze: true)
:source-highlighter: rouge
[%unbreakable]
--
[source,ruby,subs=-specialchars]
----
puts "Hello, World!"
----
--
END
[pdf.text, (TextInspector.analyze scratch_pdf.render).text].each do |text|
(expect text[0][:string]).to eql 'puts "Hello, World!"'
(expect text[0][:font_color]).to eql '333333'
end
end
it 'should remove bare HTML tags added by substitutions' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[,ruby,subs="+quotes,+macros"]
----
require %(https://asciidoctor.org[asciidoctor])
Asciidoctor._convert_file_ 'README.adoc', *safe: :safe*
----
END
lines = pdf.lines pdf.text
(expect lines).to eql ['require %(asciidoctor)', %(Asciidoctor.convert_file 'README.adoc', safe: :safe)]
require_text = pdf.find_unique_text 'require'
(expect require_text[:font_color]).not_to eql '333333'
end
it 'should substitute attribute references if attributes substitution is enabled' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
:converter-library: asciidoctor-pdf
:backend-value: :pdf
[,ruby,subs=attributes+]
----
require '{converter-library}'
Asciidoctor.convert_file 'README.adoc', safe: :safe, backend: {backend-value}
----
END
lines = pdf.lines pdf.text
(expect lines).to eql [%(require 'asciidoctor-pdf'), %(Asciidoctor.convert_file 'README.adoc', safe: :safe, backend: :pdf)]
require_text = pdf.find_unique_text 'require'
(expect require_text[:font_color]).not_to eql '333333'
end
end if gem_available? 'rouge'
context 'CodeRay' do
it 'should highlight source using CodeRay if source-highlighter is coderay' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[source,ruby]
----
puts 'Hello, CodeRay!'
----
END
hello_text = (pdf.find_text 'Hello, CodeRay!')[0]
(expect hello_text).not_to be_nil
(expect hello_text[:font_color]).to eql 'CC3300'
(expect hello_text[:font_name]).to eql 'mplus1mn-regular'
end
it 'should fallback to text language if language is not recognized' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[,scala]
----
val l = List(1,2,3,4)
l.foreach {i => println(i)}
----
END
expected_color = '333333'
(expect pdf.text.map {|it| it[:font_color] }.uniq).to eql [expected_color]
end).not_to raise_exception
end
it 'should not crash if token text is nil' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[source,sass]
----
$icon-font-path: "node_modules/package-name/icon-fonts/";
body {
background: #fafafa;
}
----
END
closing_bracket_text = pdf.find_unique_text '}'
(expect closing_bracket_text[:font_color]).to eql 'CC3300'
end).not_to raise_exception
end
it 'should use sub-language if language starts with html+' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[source,html+js]
----
document.addEventListener('load', function () { console.log('page is loaded!') })
----
END
message_text = (pdf.find_text 'page is loaded!')[0]
(expect message_text).not_to be_nil
(expect message_text[:font_color]).to eql 'CC3300'
end
it 'should fall back to text if language does not have valid characters' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[source,?!?]
----
,[.,]
----
END
text = (pdf.find_text ',[.,]')[0]
(expect text[:font_color]).to eql '333333'
end
it 'should not crash if source-highlighter attribute is defined outside of document header' do
(expect do
to_pdf <<~'END'
= Document Title
:source-highlighter: coderay
[source,ruby]
----
puts 'yo, world!'
----
END
end).not_to raise_exception
end
it 'should add indentation guards at start of line that begins with space to preserve indentation' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[source,yaml]
----
category:
hash:
key: "value"
----
END
(expect pdf.lines).to eql ['category:', %(\u00a0 hash:), %(\u00a0 key: "value")]
end
it 'should expand tabs to preserve indentation' do
pdf = to_pdf <<~END, analyze: true
:source-highlighter: coderay
[source,c]
----
int main() {
\tevent_loop();
\treturn 0;
}
----
END
lines = pdf.lines
(expect lines).to have_size 4
(expect lines[1]).to eql %(\u00a0 event_loop();)
(expect lines[2]).to eql %(\u00a0 return 0;)
end
it 'should extract conums so they do not interfere with syntax highlighting' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: coderay
[source,xml]
----
<tag <1>
attr="value">
content
</tag>
----
END
attr_name_text = (pdf.find_text 'attr')[0]
(expect attr_name_text).not_to be_nil
(expect attr_name_text[:font_color]).to eql '4F9FCF'
(expect (pdf.find_text '①')[0]).not_to be_nil
end
it 'should not crash if source highlighter is enabled and subs is set on empty block' do
(expect do
input = <<~'END'
[source,ruby,subs=attributes+]
----
----
END
lines = (to_pdf input, attribute_overrides: { 'source-highlighter' => 'coderay' }, analyze: :line).lines
text = (to_pdf input, attribute_overrides: { 'source-highlighter' => 'coderay' }, analyze: true).text
(expect (lines[0][:from][:y] - lines[1][:from][:y]).abs).to be < 5
(expect text).to be_empty
end).not_to raise_exception
end
end
context 'Pygments' do
it 'should highlight source using Pygments if source-highlighter is pygments' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,ruby]
----
puts "Hello, Pygments!"
----
END
hello_text = (pdf.find_text '"Hello, Pygments!"')[0]
(expect hello_text).not_to be_nil
(expect hello_text[:font_color]).to eql 'DD2200'
(expect hello_text[:font_name]).to eql 'mplus1mn-regular'
end
it 'should display encoded source without highlighting if lexer fails to return a value' do
input = <<~'END'
:source-highlighter: pygments
[source,xml]
----
<payload>&</payload>
----
END
# warm up pygments
pdf = to_pdf input, analyze: true
(expect pdf.text).to have_size 3
xml_lexer = Pygments::Lexer.find_by_alias 'xml'
# emulate highlight returning nil
class << xml_lexer
alias_method :_highlight, :highlight
def highlight *_args; end
end
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,xml]
----
<payload>&</payload>
----
END
(expect pdf.text).to have_size 1
source_text = pdf.text[0]
(expect source_text[:string]).to eql '<payload>&</payload>'
(expect source_text[:font_color]).to eql '333333'
ensure
class << xml_lexer
undef_method :highlight
alias_method :highlight, :_highlight
end
end
it 'should not crash when adding indentation guards' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,yaml]
----
category:
hash:
key: "value"
----
END
(expect pdf.find_text %r/: ?/).to have_size 3
lines = pdf.lines
(expect lines).to have_size 3
(expect lines[0]).to eql 'category:'
(expect lines[1]).to eql %(\u00a0 hash:)
(expect lines[2]).to eql %(\u00a0 key: "value")
(expect pdf.find_text '"value"').to have_size 1
end).not_to raise_exception
end
it 'should expand tabs to preserve indentation' do
pdf = to_pdf <<~END, analyze: true
:source-highlighter: pygments
[source,c]
----
int main() {
\tevent_loop();
\treturn 0;
}
----
END
lines = pdf.lines
(expect lines).to have_size 4
(expect lines[1]).to eql %(\u00a0 event_loop();)
(expect lines[2]).to eql %(\u00a0 return 0;)
end
it 'should use plain text lexer if language is not recognized' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,foobar]
----
puts "Hello, World!"
----
END
puts_text = (pdf.find_text 'puts')[0]
(expect puts_text).to be_nil
(expect pdf.text).to have_size 1
(expect pdf.text[0][:font_color]).to eql '333333'
end
it 'should enable start_inline option for PHP by default' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,php]
----
echo "<?php";
----
END
echo_text = (pdf.find_text 'echo')[0]
(expect echo_text).not_to be_nil
# NOTE: the echo keyword should be highlighted
(expect echo_text[:font_color]).to eql '008800'
end
it 'should not enable the start_inline option for PHP if the mixed option is set' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source%mixed,php]
----
echo "<?php";
----
END
echo_text = (pdf.find_text 'echo')[0]
# NOTE: the echo keyword should not be highlighted
(expect echo_text).to be_nil
end
it 'should not crash when aligning line numbers' do
(expect do
expected_lines = <<~'END'.split ?\n
1 <?xml version="1.0" encoding="UTF-8"?>
2 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3 <url>
4 <loc>https://example.org/home.html</loc>
5 <lastmod>2019-01-01T00:00:00.000Z</lastmod>
6 </url>
7 <url>
8 <loc>https://example.org/about.html</loc>
9 <lastmod>2019-01-01T00:00:00.000Z</lastmod>
10 </url>
11 </urlset>
END
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,xml,linenums]
----
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.org/home.html</loc>
<lastmod>2019-01-01T00:00:00.000Z</lastmod>
</url>
<url>
<loc>https://example.org/about.html</loc>
<lastmod>2019-01-01T00:00:00.000Z</lastmod>
</url>
</urlset>
----
END
(expect pdf.lines).to eql expected_lines
end).not_to raise_exception
end
it 'should honor start value for line numbering' do
expected_lines = <<~'END'.split ?\n
5 puts 'Hello, World!'
END
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,xml,linenums,start=5]
----
puts 'Hello, World!'
----
END
(expect pdf.lines).to eql expected_lines
end
it 'should preserve space before callout on last line' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,yaml]
----
foo: 'bar'
key: 'value' #<1>
----
<1> key-value pair
END
text = pdf.text
conum_idx = text.index {|it| it[:string] == '①' }
(expect text[conum_idx - 1][:string]).to eql ' '
(expect text[conum_idx - 2][:string]).to eql '\'value\''
end
it 'should support background color on highlighted tokens', visual: true do
to_file = to_pdf_file <<~'END', 'source-pygments-token-background-color.pdf'
:source-highlighter: pygments
:pygments-style: murphy
[source,ruby]
----
# Matches a hex color value like #FF0000
if /^#[a-fA-F0-9]{6}$/.match? color
puts 'hex color'
end
----
END
(expect to_file).to visually_match 'source-pygments-token-background-color.pdf'
end
it 'should use background color from style', visual: true do
to_file = to_pdf_file <<~'END', 'source-pygments-background-color.pdf', pdf_theme: { code_background_color: 'fafafa' }
:source-highlighter: pygments
:pygments-style: monokai
.Ruby
[source,ruby]
----
if /^#[a-fA-F0-9]{6}$/.match? color
puts 'hex color'
end
----
.JavaScript
[source,js]
----
'use strict'
const TAG_ALL_RX = /<[^>]+>/g
module.exports = (html) => html && html.replace(TAG_ALL_RX, '')
----
END
(expect to_file).to visually_match 'source-pygments-background-color.pdf'
end
it 'should use font color from style' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
:pygments-style: monokai
[source,text]
----
foo
bar
baz
----
END
pdf.text.each do |text|
(expect text[:font_color]).to eql 'F8F8F2'
end
end
it 'should ignore highlight attribute if empty' do
pdf = to_pdf <<~'END', analyze: :rect
:source-highlighter: pygments
:pygments-style: tango
[source,ruby,linenums,highlight=]
----
puts "Hello, World!"
----
END
(expect pdf.rectangles).to be_empty
end
it 'should fall back to pastie style if style is not recognized' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
:pygments-style: not-recognized
[source,ruby]
----
# Matches a hex color value like #FF0000
if /^#[a-fA-F0-9]{6}$/.match? color
puts 'hex color'
end
----
END
comment_text = pdf.find_unique_text %r/^#/
(expect comment_text[:font_color]).to eql '888888'
rx_text = pdf.find_unique_text %r/^\/\^/
(expect rx_text[:font_color]).to eql '008800'
end).not_to raise_exception
end
it 'should highlight selected lines but not the line numbers', visual: true do
to_file = to_pdf_file <<~'END', 'source-pygments-line-highlighting.pdf'
:source-highlighter: pygments
[source,groovy,linenums,highlight=7-9]
----
package com.example
import static ratpack.groovy.Groovy.ratpack
ratpack {
handlers {
get {
render "Hello, World!"
}
}
}
----
END
(expect to_file).to visually_match 'source-pygments-line-highlighting.pdf'
end
it 'should not add line number to first line if source is empty' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source%linenums]
----
----
END
(expect pdf.text).to be_empty
end
it 'should not emit error if linenums are enabled and language is not set' do
(expect do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source%linenums]
----
fee
fi
fo
fum
----
END
(expect pdf.lines).to eql ['1 fee', '2 fi', '3 fo', '4 fum']
end).to not_log_message
end
it 'should indent wrapped line if line numbers are enabled' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: pygments
[source,text,linenums]
----
Here we go again here we go again here we go again here we go again here we go again Here we go again
----
END
linenum_text = (pdf.find_text '1 ')[0]
(expect linenum_text[:x]).not_to be_nil
start_texts = pdf.find_text %r/^Here we go again/
(expect start_texts).to have_size 2
(expect start_texts[0][:x]).to eql start_texts[1][:x]
(expect start_texts[0][:x]).to be > linenum_text[:x]
end
it 'should highlight and indent wrapped line', visual: true do
to_file = to_pdf_file <<~'END', 'source-pygments-highlight-wrapped-line.pdf'
:source-highlighter: pygments
[source,xml,linenums,highlight=1;3]
----
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
</project>
----
END
(expect to_file).to visually_match 'source-pygments-highlight-wrapped-line.pdf'
end
it 'should guard inner indents' do
pdf = to_pdf <<~END, analyze: true
:source-highlighter: pygments
[source,text]
----
lead space
flush
lead space
----
END
(expect pdf.lines).to eql [%(\u00a0 lead space), 'flush', %(\u00a0 lead space)]
end
it 'should ignore fragment if empty' do
pdf = to_pdf <<~END, analyze: true
:source-highlighter: pygments
[source,ruby]
----
<1>
----
END
(expect pdf.lines).to eql ['①']
end
end if gem_available? 'pygments.rb'
context 'Unsupported' do
it 'should apply specialcharacters substitution and indentation guards for client-side syntax highlighter' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: highlight.js
[source,xml]
----
<root>
<child>content</child>
</root>
----
END
(expect pdf.lines).to eql ['<root>', %(\u00a0 <child>content</child>), '</root>']
(expect pdf.text.map {|it| it[:font_color] }.uniq).to eql ['333333']
end
it 'should apply specialcharacters substitution and indentation guards if syntax highlighter is unsupported' do
Class.new Asciidoctor::SyntaxHighlighter::Base do
register_for :foobar
def highlight?
true
end
end
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: foobar
[source,xml]
----
<root>
<child>content</child>
</root>
----
END
(expect pdf.lines).to eql ['<root>', %(\u00a0 <child>content</child>), '</root>']
(expect pdf.text.map {|it| it[:font_color] }.uniq).to eql ['333333']
end
end
context 'Callouts' do
it 'should allow callout to be escaped' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,ruby]
----
source = %(before
\<1>
after)
----
END
(expect pdf.lines).to include '<1>'
(expect pdf.find_text '①').to be_empty
end
it 'should not replace callouts if callouts sub is not present' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,ruby,subs=-callouts]
----
source = %(before
not a conum <1>
after)
----
END
(expect pdf.lines).to include 'not a conum <1>'
(expect pdf.find_text '①').to be_empty
end
it 'should inherit font color if not set in theme when source highlighter is enabled' do
pdf = to_pdf <<~'END', pdf_theme: { code_font_color: '111111', conum_font_color: nil }, analyze: true
:source-highlighter: rouge
[source,ruby]
----
puts 'Hello, World' <1>
----
<1> Just a programming saying hi.
END
conum_texts = pdf.find_text %r/①/
(expect conum_texts).to have_size 2
(expect conum_texts[0][:font_color]).to eql '111111'
(expect conum_texts[1][:font_color]).to eql '333333'
end
it 'should inherit font color if not set in theme when source highlighter is not enabled' do
pdf = to_pdf <<~'END', pdf_theme: { code_font_color: '111111', conum_font_color: nil }, analyze: true
[source,ruby]
----
puts 'Hello, World' <1>
----
<1> Just a programming saying hi.
END
conum_texts = pdf.find_text %r/①/
(expect conum_texts).to have_size 2
(expect conum_texts[0][:font_color]).to eql '111111'
(expect conum_texts[1][:font_color]).to eql '333333'
end
it 'should process a sequence of two or more callouts when not separated by spaces' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,java]
----
public interface Person {
String getName(); // <1><2>
String getDob();
int getAge(); // <3><4><5>
}
----
END
lines = pdf.lines
(expect lines[1]).to end_with '; ① ②'
(expect lines[3]).to end_with '; ③ ④ ⑤'
end
it 'should process a sequence of two or more callouts when separated by spaces' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,java]
----
public interface Person {
String getName(); // <1> <2>
String getDob();
int getAge(); // <3> <4> <5>
}
----
END
lines = pdf.lines
(expect lines[1]).to end_with '; ① ②'
(expect lines[3]).to end_with '; ③ ④ ⑤'
end
it 'should honor font family set on conum category in theme for conum in source block' do
pdf = to_pdf <<~'END', pdf_theme: { code_font_family: 'Courier' }, analyze: true
:source-highlighter: rouge
[source,java]
----
public interface Person {
String getName(); <1>
String getDob(); <2>
int getAge(); <3>
}
----
END
lines = pdf.lines
(expect lines[1]).to end_with '; ①'
(expect lines[2]).to end_with '; ②'
(expect lines[3]).to end_with '; ③'
conum_text = (pdf.find_text '①')[0]
(expect conum_text[:font_name]).not_to eql 'Courier'
end
it 'should substitute autonumber callouts with circled numbers when using rouge as syntax highlighter' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,java]
----
public interface Person {
String getName(); // <.>
String getDob(); // <.>
int getAge(); // <.>
}
----
END
lines = pdf.lines
(expect lines[1]).to end_with '; ①'
(expect lines[2]).to end_with '; ②'
(expect lines[3]).to end_with '; ③'
end
it 'should process multiple autonumber callouts on a single line when using rouge as syntax highlighter' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,java]
----
public interface Person {
String getName(); // <.>
String getDob(); // <.>
int getAge(); // <.> <.>
}
----
END
lines = pdf.lines
(expect lines[1]).to end_with '; ①'
(expect lines[2]).to end_with '; ②'
(expect lines[3]).to end_with '; ③ ④'
end
it 'should preserve space before callout on final line' do
['rouge', (gem_available? 'pygments.rb') ? 'pygments' : nil].compact.each do |highlighter|
pdf = to_pdf <<~'END', attribute_overrides: { 'source-highlighter' => highlighter }, analyze: true
[source,java]
----
public interface Person {
String getName();
} <1>
----
<1> End class definition
END
lines = pdf.lines
(expect lines).to include '} ①'
end
end
it 'should hide spaces in front of conum from source highlighter' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[source,apache]
----
<Directory /usr/share/httpd/noindex>
AllowOverride None <1>
Require all granted
</Directory>
----
<1> Cannot be overridden by .htaccess
END
none_text = (pdf.find_text 'None')[0]
(expect none_text).not_to be_nil
(expect none_text[:font_color]).to eql 'AA6600'
end
it 'should preserve callouts if custom subs are used on code block when source highlighter is enabled' do
pdf = to_pdf <<~'END', analyze: true
:source-highlighter: rouge
[,ruby,subs=+quotes]
----
puts "*Hello, World!*" <1>
----
<1> Welcome the world to the show.
END
lines = pdf.lines
(expect lines).to have_size 2
(expect lines[0]).to eql 'puts "Hello, World!" ①'
(expect lines[1]).to eql '① Welcome the world to the show.'
msg = pdf.find_unique_text '"Hello, World!"'
(expect msg[:font_name]).to eql 'mplus1mn-regular'
end
end if gem_available? 'rouge'
context 'Label' do
it 'should add label to code block if language is specified' do
source_file = doc_file 'modules/extend/examples/pdf-converter-source-language-label.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 <<~'END', backend: backend, analyze: true
:source-highlighter: coderay
[,ruby]
----
lines = (File.readlines ARGV[0]).each_with_object [] do |ln, acc|
acc << ln unless (ln = ln.rstrip).empty? || (ln.start_with? '#')
end
puts lines * ?\n
----
END
midpoint = pdf.pages[0][:size][0] * 0.5
reference_text = (pdf.find_text 'lines')[0]
label_text = pdf.find_unique_text 'RUBY'
(expect label_text).not_to be_nil
(expect label_text[:y]).to eql reference_text[:y]
(expect label_text[:x]).to be > midpoint
(expect label_text[:font_size]).to eql reference_text[:font_size]
end
it 'should add label to code block in second column if language is specified' do
source_file = doc_file 'modules/extend/examples/pdf-converter-source-language-label.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_theme = { page_columns: 2, page_column_gap: 12 }
pdf = to_pdf <<~'END', backend: backend, pdf_theme: pdf_theme, analyze: true
:source-highlighter: coderay
image::tall-spacer.png[pdfwidth=5]
[%unbreakable,ruby]
----
require 'asciidoctor-pdf'
Asciidoctor.convert_file 'README.adoc',
backend: 'pdf', safe: :safe
----
END
midpoint = pdf.pages[0][:size][0] * 0.5
reference_text = (pdf.find_text 'require')[0]
label_text = pdf.find_unique_text 'RUBY'
(expect label_text).not_to be_nil
(expect label_text[:y]).to eql reference_text[:y]
(expect label_text[:x]).to be > midpoint
(expect label_text[:font_size]).to eql reference_text[:font_size]
end
end
end
|