forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax_highlighter_test.rb
1356 lines (1168 loc) · 45.8 KB
/
syntax_highlighter_test.rb
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
# frozen_string_literal: true
require_relative 'test_helper'
context 'Syntax Highlighter' do
test 'should set syntax_highlighter property on document if source highlighter is set and basebackend is html' do
input = <<~'EOS'
:source-highlighter: coderay
[source, ruby]
----
puts 'Hello, World!'
----
EOS
doc = document_from_string input, safe: :safe, parse: true
assert doc.basebackend? 'html'
refute_nil doc.syntax_highlighter
assert_kind_of Asciidoctor::SyntaxHighlighter, doc.syntax_highlighter
end
test 'should not set syntax_highlighter property on document if source highlighter is set and basebackend is not html' do
input = <<~'EOS'
:source-highlighter: coderay
[source, ruby]
----
puts 'Hello, World!'
----
EOS
doc = document_from_string input, safe: :safe, backend: 'docbook', parse: true
refute doc.basebackend? 'html'
assert_nil doc.syntax_highlighter
end
test 'should not set syntax_highlighter property on document if source highlighter is not set' do
input = <<~'EOS'
[source, ruby]
----
puts 'Hello, World!'
----
EOS
doc = document_from_string input, safe: :safe, parse: true
assert_nil doc.syntax_highlighter
end
test 'should not set syntax_highlighter property on document if syntax highlighter cannot be resolved' do
input = <<~'EOS'
:source-highlighter: unknown
[source, ruby]
----
puts 'Hello, World!'
----
EOS
doc = document_from_string input, safe: :safe, parse: true
assert_nil doc.syntax_highlighter
end
test 'should not allow document to enable syntax highlighter if safe mode is at least SERVER' do
input = ':source-highlighter: coderay'
doc = document_from_string input, safe: Asciidoctor::SafeMode::SERVER, parse: true
assert_nil doc.attributes['source-highlighter']
assert_nil doc.syntax_highlighter
end
test 'should not invoke highlight method on syntax highlighter if highlight? is false' do
Class.new Asciidoctor::SyntaxHighlighter::Base do
register_for 'unavailable'
def format node, language, opts
%(<pre class="highlight"><code class="language-#{language}" data-lang="#{language}">#{node.content}</code></pre>)
end
def highlight?
false
end
end
input = <<~'EOS'
[source,ruby]
----
puts 'Hello, World!'
----
EOS
doc = document_from_string input, attributes: { 'source-highlighter' => 'unavailable' }
output = doc.convert
assert_css 'pre.highlight > code.language-ruby', output, 1
source_block = (doc.find_by {|candidate| candidate.style == 'source' })[0]
assert_raises NotImplementedError do
doc.syntax_highlighter.highlight source_block, source_block.source, (source_block.attr 'language'), {}
end
end
test 'should be able to register syntax highlighter from syntax highlighter class itself' do
syntax_highlighter = Class.new Asciidoctor::SyntaxHighlighter::Base do
def format node, language, opts
%(<pre class="highlight"><code class="language-#{language}" data-lang="#{language}">#{node.content}</code></pre>)
end
def highlight?
false
end
end
syntax_highlighter.register_for 'foobar'
assert_equal syntax_highlighter, (Asciidoctor::SyntaxHighlighter.for 'foobar')
end
test 'should be able to register syntax highlighter using symbol' do
syntax_highlighter = Class.new Asciidoctor::SyntaxHighlighter::Base do
register_for :foobaz
def format node, language, opts
%(<pre class="highlight"><code class="language-#{language}" data-lang="#{language}">#{node.content}</code></pre>)
end
def highlight?
false
end
end
assert_equal syntax_highlighter, (Asciidoctor::SyntaxHighlighter.for 'foobaz')
end
test 'should set language on output of source block when source-highlighter attribute is not set' do
input = <<~'EOS'
[source, ruby]
----
puts 'Hello, World!'
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css 'pre.highlight', output, 1
assert_css 'pre.highlight > code.language-ruby', output, 1
assert_css 'pre.highlight > code.language-ruby[data-lang="ruby"]', output, 1
end
test 'should set language on output of source block when source-highlighter attribute is not recognized' do
input = <<~'EOS'
:source-highlighter: unknown
[source, ruby]
----
puts 'Hello, World!'
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css 'pre.highlight', output, 1
assert_css 'pre.highlight > code.language-ruby', output, 1
assert_css 'pre.highlight > code.language-ruby[data-lang="ruby"]', output, 1
end
test 'should highlight source if source highlighter is set even if language is not set' do
input = <<~'EOS'
:source-highlighter: coderay
[source]
----
[numbers]
one
two
three
----
EOS
output = convert_string input, safe: :safe
assert_css 'pre.CodeRay.highlight', output, 1
assert_includes output, '<code>'
end
test 'should not crash if source block has no lines and source highlighter is set' do
input = <<~'EOS'
:source-highlighter: coderay
[source,text]
----
----
EOS
output = convert_string_to_embedded input, safe: :safe
assert_css 'pre.CodeRay', output, 1
assert_css 'pre.CodeRay > code', output, 1
assert_css 'pre.CodeRay > code:empty', output, 1
end
test 'should highlight source inside AsciiDoc table cell if source-highlighter attribute is set' do
input = <<~'EOS'
:source-highlighter: coderay
|===
a|
[source, ruby]
----
require 'coderay'
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table
----
|===
EOS
output = convert_string_to_embedded input, safe: :safe
assert_xpath '/table//pre[@class="CodeRay highlight"]/code[@data-lang="ruby"]//span[@class = "constant"][text() = "CodeRay"]', output, 1
end
test 'should set starting line number in DocBook output if linenums option is enabled and start attribute is set' do
input = <<~'EOS'
[source%linenums,java,start=3]
----
public class HelloWorld {
public static void main(String[] args) {
out.println("Hello, World!");
}
}
----
EOS
output = convert_string_to_embedded input, backend: :docbook, safe: Asciidoctor::SafeMode::SAFE
assert_css 'programlisting[startinglinenumber]', output, 1
assert_css 'programlisting[startinglinenumber="3"]', output, 1
end
test 'should read source language from source-language document attribute if not specified on source block' do
input = <<~'EOS'
:source-highlighter: coderay
:source-language: ruby
[source]
----
require 'coderay'
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE, linkcss_default: true
assert_xpath '//pre[@class="CodeRay highlight"]/code[@data-lang="ruby"]//span[@class = "constant"][text() = "CodeRay"]', output, 1
end
test 'should rename document attribute named language to source-language when compat-mode is enabled' do
input = <<~'EOS'
:language: ruby
{source-language}
EOS
assert_equal 'ruby', (convert_inline_string input, attributes: { 'compat-mode' => '' })
input = <<~'EOS'
:language: ruby
{source-language}
EOS
assert_equal '{source-language}', (convert_inline_string input)
end
context 'CodeRay' do
test 'should highlight source if source-highlighter attribute is set' do
input = <<~'EOS'
:source-highlighter: coderay
[source, ruby]
----
require 'coderay'
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, linkcss_default: true
assert_xpath '//pre[@class="CodeRay highlight"]/code[@data-lang="ruby"]//span[@class = "constant"][text() = "CodeRay"]', output, 1
assert_match(/\.CodeRay *\{/, output)
style_node = xmlnodes_at_xpath '//style[contains(text(), ".CodeRay")]', output, 1
refute_nil style_node
assert_equal 'head', style_node.parent.name
end
test 'should not fail if source language is invalid' do
input = <<~'EOS'
:source-highlighter: coderay
[source, n/a]
----
PRINT 'yo'
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_css 'code[data-lang="n/a"]', output, 1
end
test 'should number lines if third positional attribute is set' do
input = <<~'EOS'
:source-highlighter: coderay
[source,ruby,linenums]
----
puts 'Hello, World!'
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_xpath '//td[@class="line-numbers"]', output, 1
end
test 'should number lines if linenums option is set on source block' do
input = <<~'EOS'
:source-highlighter: coderay
[source%linenums,ruby]
----
puts 'Hello, World!'
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_xpath '//td[@class="line-numbers"]', output, 1
end
test 'should number lines of source block if source-linenums-option document attribute is set' do
input = <<~'EOS'
:source-highlighter: coderay
:source-linenums-option:
[source,ruby]
----
puts 'Hello, World!'
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_xpath '//td[@class="line-numbers"]', output, 1
end
test 'should set starting line number in HTML output if linenums option is enabled and start attribute is set' do
input = <<~'EOS'
:source-highlighter: coderay
:coderay-linenums-mode: inline
[source%linenums,ruby,start=10]
----
puts 'Hello, World!'
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_xpath '//span[@class="line-numbers"]', output, 1
assert_xpath '//span[@class="line-numbers"][text()="10"]', output, 1
end
test 'should highlight lines specified in highlight attribute if linenums is set and source-highlighter is coderay' do
%w(highlight="1,4-6" highlight="4-6,1" highlight="5-6,1,4,5" highlight=1;4..6 highlight=1;4..;!7).each do |highlight_attr|
input = <<~EOS
:source-highlighter: coderay
[source%linenums,java,#{highlight_attr}]
----
import static java.lang.System.out;
public class HelloWorld {
public static void main(String[] args) {
out.println("Hello, World!");
}
}
----
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_css 'strong.highlighted', output, 4
assert_xpath '//strong[@class="highlighted"][text()="1"]', output, 1
assert_xpath '//strong[@class="highlighted"][text()="2"]', output, 0
assert_xpath '//strong[@class="highlighted"][text()="3"]', output, 0
assert_xpath '//strong[@class="highlighted"][text()="4"]', output, 1
assert_xpath '//strong[@class="highlighted"][text()="5"]', output, 1
assert_xpath '//strong[@class="highlighted"][text()="6"]', output, 1
assert_xpath '//strong[@class="highlighted"][text()="7"]', output, 0
end
end
test 'should replace callout marks but not highlight them if source-highlighter attribute is coderay' do
input = <<~'EOS'
:source-highlighter: coderay
[source, ruby]
----
require 'coderay' # <1>
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table # <2>
puts html # <3> <4>
exit 0 # <5><6>
----
<1> Load library
<2> Highlight source
<3> Print to stdout
<4> Redirect to a file to capture output
<5> Exit program
<6> Reports success
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_match(/<span class="content">coderay<\/span>.* # <b class="conum">\(1\)<\/b>$/, output)
assert_match(/<span class="content">puts 'Hello, world!'<\/span>.* # <b class="conum">\(2\)<\/b>$/, output)
assert_match(/puts html.* # <b class="conum">\(3\)<\/b> <b class="conum">\(4\)<\/b>$/, output)
assert_match(/exit.* # <b class="conum">\(5\)<\/b> <b class="conum">\(6\)<\/b><\/code>/, output)
end
test 'should support autonumbered callout marks if source-highlighter attribute is coderay' do
input = <<~'EOS'
:source-highlighter: coderay
[source, ruby]
----
require 'coderay' # <.><.>
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table # <.>
puts html # <.>
----
<.> Load library
<.> Gem must be installed
<.> Highlight source
<.> Print to stdout
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_match(/<span class="content">coderay<\/span>.* # <b class="conum">\(1\)<\/b> <b class="conum">\(2\)<\/b>$/, output)
assert_match(/<span class="content">puts 'Hello, world!'<\/span>.* # <b class="conum">\(3\)<\/b>$/, output)
assert_match(/puts html.* # <b class="conum">\(4\)<\/b><\/code>/, output)
assert_css '.colist ol', output, 1
assert_css '.colist ol li', output, 4
end
test 'should restore callout marks to correct lines if source highlighter is coderay and table line numbering is enabled' do
input = <<~'EOS'
:source-highlighter: coderay
:coderay-linenums-mode: table
[source, ruby, numbered]
----
require 'coderay' # <1>
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table # <2>
puts html # <3> <4>
exit 0 # <5><6>
----
<1> Load library
<2> Highlight source
<3> Print to stdout
<4> Redirect to a file to capture output
<5> Exit program
<6> Reports success
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
assert_match(/<span class="content">coderay<\/span>.* # <b class="conum">\(1\)<\/b>$/, output)
assert_match(/<span class="content">puts 'Hello, world!'<\/span>.* # <b class="conum">\(2\)<\/b>$/, output)
assert_match(/puts html.* # <b class="conum">\(3\)<\/b> <b class="conum">\(4\)<\/b>$/, output)
# NOTE notice there's a newline before the closing </pre> tag
assert_match(/exit.* # <b class="conum">\(5\)<\/b> <b class="conum">\(6\)<\/b>\n<\/pre>/, output)
end
test 'should restore isolated callout mark on last line of source when source highlighter is coderay' do
input = <<~'EOS'
:source-highlighter: coderay
[source,ruby,linenums]
----
require 'app'
launch_app
# <1>
----
<1> Profit.
EOS
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE
# NOTE notice there's a newline before the closing </pre> tag
assert_match(/\n# <b class="conum">\(1\)<\/b>\n<\/pre>/, output)
end
test 'should preserve space before callout on final line' do
inputs = []
inputs << <<~'EOS'
[source,yaml]
----
a: 'a'
key: 'value' #<1>
----
<1> key-value pair
EOS
inputs << <<~'EOS'
[source,ruby]
----
puts 'hi'
puts 'value' #<1>
----
<1> print to stdout
EOS
inputs << <<~'EOS'
[source,python]
----
print 'hi'
print 'value' #<1>
----
<1> print to stdout
EOS
inputs.each do |input|
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'source-highlighter' => 'coderay' }
output = output.gsub(/<\/?span.*?>/, '')
assert_includes output, '\'value\' #<b class="conum">(1)</b>'
end
end
test 'should preserve passthrough placeholders when highlighting source using coderay' do
input = <<~'EOS'
:source-highlighter: coderay
[source,java]
[subs="specialcharacters,macros,callouts"]
----
public class Printer {
public static void main(String[] args) {
System.pass:quotes[_out_].println("*asterisks* make text pass:quotes[*bold*]");
}
}
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_match(/\.<em>out<\/em>\./, output, 1)
assert_match(/\*asterisks\*/, output, 1)
assert_match(/<strong>bold<\/strong>/, output, 1)
refute_includes output, Asciidoctor::Substitutors::PASS_START
end
test 'should link to CodeRay stylesheet if source-highlighter is coderay and linkcss is set' do
input = <<~'EOS'
:source-highlighter: coderay
[source, ruby]
----
require 'coderay'
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'linkcss' => '' }
assert_xpath '//pre[@class="CodeRay highlight"]/code[@data-lang="ruby"]//span[@class = "constant"][text() = "CodeRay"]', output, 1
link_node = xmlnodes_at_xpath '//link[@rel="stylesheet"][@href="./coderay-asciidoctor.css"]', output, 1
refute_nil link_node
assert_equal 'head', link_node.parent.name
end
test 'should highlight source inline if source-highlighter attribute is coderay and coderay-css is style' do
input = <<~'EOS'
:source-highlighter: coderay
:coderay-css: style
[source, ruby]
----
require 'coderay'
html = CodeRay.scan("puts 'Hello, world!'", :ruby).div line_numbers: :table
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, linkcss_default: true
assert_xpath '//pre[@class="CodeRay highlight"]/code[@data-lang="ruby"]//span[@style = "color:#036;font-weight:bold"][text() = "CodeRay"]', output, 1
refute_match(/\.CodeRay \{/, output)
end
test 'should read stylesheet' do
css = (Asciidoctor::SyntaxHighlighter.for 'coderay').read_stylesheet
refute_nil css
assert_includes css, 'pre.CodeRay{background:#f7f7f8}'
end
end
context 'Highlight.js' do
test 'should add data-lang as last attribute on code tag when source-highlighter is highlight.js' do
input = <<~'EOS'
:source-highlighter: highlight.js
[source,ruby]
----
puts 'Hello, World!'
----
EOS
output = convert_string_to_embedded input, safe: :safe
assert_includes output, '<code class="language-ruby hljs" data-lang="ruby">'
end
test 'should include remote highlight.js assets if source-highlighter attribute is highlight.js' do
input = <<~'EOS'
:source-highlighter: highlight.js
[source,html]
----
<p>Highlight me!</p>
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css 'pre.highlightjs.highlight', output, 1
assert_css 'pre.highlightjs.highlight > code.language-html.hljs[data-lang="html"]', output, 1
assert_includes output, '<p>Highlight me!</p>'
assert_css 'head > link[href*="highlight.js"]', output, 1
assert_css '#footer ~ script[src*="highlight.min.js"]', output, 1
assert_xpath '//script[contains(text(), "hljs.highlightBlock(el)")]', output, 1
end
test 'should add language-none class to source block when source-highlighter is highlight.js and language is not set' do
input = <<~'EOS'
:source-highlighter: highlight.js
[source]
----
[numbers]
one
two
three
----
EOS
output = convert_string input, safe: :safe
assert_css 'code.language-none', output, 1
end
test 'should load additional languages specified by highlightjs-languages' do
input = <<~'EOS'
:source-highlighter: highlight.js
:highlightjs-languages: yaml, scilab
[source,yaml]
----
key: value
----
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css '#footer ~ script[src*="languages/yaml.min.js"]', output, 1
assert_css '#footer ~ script[src*="languages/scilab.min.js"]', output, 1
end
end
context 'Prettify' do
test 'should add language classes to child code element when source-highlighter is prettify' do
input = <<~'EOS'
[source,ruby]
----
puts "foo"
----
EOS
output = convert_string_to_embedded input, attributes: { 'source-highlighter' => 'prettify' }
assert_css 'pre[class="prettyprint highlight"]', output, 1
assert_css 'pre > code[data-lang="ruby"]', output, 1
end
test 'should set linenums start if linenums are enabled and start attribute is set when source-highlighter is prettify' do
input = <<~'EOS'
[source%linenums,ruby,start=5]
----
puts "foo"
----
EOS
output = convert_string_to_embedded input, attributes: { 'source-highlighter' => 'prettify' }
assert_css 'pre[class="prettyprint highlight linenums:5"]', output, 1
assert_css 'pre > code[data-lang="ruby"]', output, 1
end
end
context 'HTML Pipeline' do
test 'should set lang attribute on pre when source-highlighter is html-pipeline' do
input = <<~'EOS'
[source,ruby]
----
filters = [
HTML::Pipeline::AsciiDocFilter,
HTML::Pipeline::SanitizationFilter,
HTML::Pipeline::SyntaxHighlightFilter
]
puts HTML::Pipeline.new(filters, {}).call(input)[:output]
----
EOS
output = convert_string input, attributes: { 'source-highlighter' => 'html-pipeline' }
assert_css 'pre[lang="ruby"]', output, 1
assert_css 'pre[lang="ruby"] > code', output, 1
assert_css 'pre[class]', output, 0
assert_css 'code[class]', output, 0
end
end
context 'Rouge' do
test 'should syntax highlight source if source-highlighter attribute is set' do
input = <<~'EOS'
:source-highlighter: rouge
[source,ruby]
----
require 'rouge'
html = Rouge::Formatters::HTML.format(Rouge::Lexers::Ruby.lex('puts "Hello, world!"'))
----
EOS
output = convert_string input, safe: :safe, linkcss_default: true
assert_xpath '//pre[@class="rouge highlight"]/code[@data-lang="ruby"]/span[@class="no"][text()="Rouge"]', output, 2
assert_includes output, 'pre.rouge .no {'
style_node = xmlnodes_at_xpath '//style[contains(text(), "pre.rouge")]', output, 1
refute_nil style_node
assert_equal 'head', style_node.parent.name
end
test 'should highlight source using a mixed lexer (HTML + JavaScript)' do
input = <<~'EOS'
[,html]
----
<meta name="description" content="The dangerous and thrilling adventures of an open source documentation team.">
<script>alert("Do your worst!")</script>
----
EOS
output = convert_string_to_embedded input, safe: :safe, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'pre.rouge > code[data-lang="html"]', output, 1
end
test 'should enable start_inline for PHP by default' do
input = <<~'EOS'
[,php]
----
echo "<?php";
----
EOS
output = convert_string_to_embedded input, safe: :safe, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'pre.rouge > code[data-lang="php"]', output, 1
assert_include '<span class="k">echo</span>', output
end
test 'should not enable start_inline for PHP if disabled using cgi-style option on language' do
input = <<~'EOS'
[,php?start_inline=0]
----
echo "<?php";
----
EOS
output = convert_string_to_embedded input, safe: :safe, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'pre.rouge > code[data-lang="php"]', output, 1
refute_include '<span class="k">echo</span>', output
assert_include '<span class="cp"><?php</span>', output
end
test 'should not enable start_inline for PHP if mixed option is set' do
input = <<~'EOS'
[%mixed,php]
----
echo "<?php";
----
EOS
output = convert_string_to_embedded input, safe: :safe, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'pre.rouge > code[data-lang="php"]', output, 1
refute_include '<span class="k">echo</span>', output
assert_include '<span class="cp"><?php</span>', output
end
test 'should preserve cgi-style options on language when setting start_inline option for PHP', if: (Rouge.version >= '2.1.0') do
input = <<~'EOS'
[,php?funcnamehighlighting=0]
----
cal_days_in_month(CAL_GREGORIAN, 6, 2019)
----
EOS
output = convert_string_to_embedded input, safe: :safe, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'pre.rouge > code[data-lang="php"]', output, 1
# if class is "nb", then the funcnamehighlighting option is not honored
refute_include '<span class="nb">cal_days_in_month</span>', output
assert_include '<span class="mi">2019</span>', output
end
test 'should not crash if source-highlighter attribute is set and source block does not define a language' do
input = <<~'EOS'
:source-highlighter: rouge
[source]
----
require 'rouge'
html = Rouge::Formatters::HTML.format(Rouge::Lexers::Ruby.lex('puts "Hello, world!"'))
----
EOS
output = convert_string_to_embedded input, safe: :safe
assert_css 'pre > code:not([data-lang])', output, 1
end
test 'should default to plain text lexer if lexer cannot be resolved for language' do
input = <<~'EOS'
:source-highlighter: rouge
[source,lolcode]
----
CAN HAS STDIO?
PLZ OPEN FILE "LOLCATS.TXT"?
KTHXBYE
----
EOS
output = convert_string_to_embedded input, safe: :safe
assert_css 'code[data-lang=lolcode]', output, 1
assert_css 'code span', output, 0
assert_xpath %(//code[text()='CAN HAS STDIO?\nPLZ OPEN FILE "LOLCATS.TXT"?\nKTHXBYE']), output, 1
end
test 'should honor cgi-style options on language', if: (Rouge.version >= '2.1.0') do
input = <<~'EOS'
:source-highlighter: rouge
[source,console?prompt=$>]
----
$> asciidoctor --version
----
EOS
output = convert_string_to_embedded input, safe: :safe
assert_css 'code[data-lang=console]', output, 1
assert_css 'code span.gp', output, 1
end
test 'should number lines using table layout if linenums option is enabled and linenums mode is not set' do
input = <<~'EOS'
[source%linenums,ruby]
----
puts 'Hello, world!'
puts 'Goodbye, world!'
----
EOS
output = convert_string_to_embedded input, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'table.linenotable', output, 1
assert_css 'table.linenotable td.linenos', output, 2
assert_css 'table.linenotable td.linenos pre', output, 2
assert_xpath '(//td[@class="linenos"])[1]/pre[text()="1"]', output, 1
assert_xpath '(//td[@class="linenos"])[2]/pre[text()="2"]', output, 1
assert_css 'table.linenotable td.code', output, 2
assert_css 'table.linenotable td.code pre:not([class])', output, 2
assert_includes output, %(<td class="code"><pre><span class="nb">puts</span> <span class="s1">'Hello, world!'</span>\n</pre></td>)
assert_includes output, %(<td class="code"><pre><span class="nb">puts</span> <span class="s1">'Goodbye, world!'</span>\n</pre></td>)
end
test 'should number lines using inline element if linenums option is enabled and linenums mode is inline' do
input = <<~'EOS'
:rouge-linenums-mode: inline
[source%linenums,ruby]
----
puts 'Hello, world!'
puts 'Goodbye, world!'
----
EOS
expected = <<~EOS.chop
<span class="linenos">1</span><span class="nb">puts</span> <span class="s1">'Hello, world!'</span>
<span class="linenos">2</span><span class="nb">puts</span> <span class="s1">'Goodbye, world!'</span>
EOS
output = convert_string_to_embedded input, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'table.linenotable', output, 0
assert_includes output, expected
end
test 'should set starting line number in HTML output if linenums option is enabled and start attribute is set' do
input = <<~'EOS'
[source%linenums,ruby,start=9]
----
puts 'Hello, World!'
puts 'Goodbye, World!'
----
EOS
output = convert_string_to_embedded input, attributes: { 'source-highlighter' => 'rouge' }
assert_css 'table.linenotable', output, 1
assert_css 'table.linenotable td.linenos', output, 2
assert_css 'table.linenotable td.linenos pre', output, 2
assert_css 'table.linenotable td.code', output, 2
assert_css 'table.linenotable td.code pre:not([class])', output, 2
assert_xpath '(//td[@class="linenos"])[1]/pre[text()="9"]', output, 1
assert_xpath '(//td[@class="linenos"])[2]/pre[text()="10"]', output, 1
end
test 'should restore callout marks to correct lines' do
['', '%linenums'].each do |opts|
input = <<~EOS
:source-highlighter: rouge
:rouge-linenums-mode: inline
[source#{opts},ruby]
----
require 'rouge' # <1>
html = Rouge::Formatters::HTML.format(Rouge::Lexers::Ruby.lex('puts "Hello, world!"')) # <2>
puts html # <3> <4>
exit 0 # <5><6>
----
<1> Load library
<2> Highlight source
<3> Print to stdout
<4> Redirect to a file to capture output
<5> Exit program
<6> Reports success
EOS
output = convert_string_to_embedded input, safe: :safe
assert_match(/<span class="s1">'rouge'<\/span>.* # <b class="conum">\(1\)<\/b>$/, output)
assert_match(/<span class="s1">'puts "Hello, world!"'<\/span>.* # <b class="conum">\(2\)<\/b>$/, output)
assert_match(/<span class="n">html<\/span>.* # <b class="conum">\(3\)<\/b> <b class="conum">\(4\)<\/b>$/, output)
assert_match(/<span class="mi">0<\/span>.* # <b class="conum">\(5\)<\/b> <b class="conum">\(6\)<\/b><\/code><\/pre>/, output)
end
end
test 'should line highlight specified lines when last line is not highlighted' do
['', '%linenums'].each do |opts|
input = <<~EOS
:source-highlighter: rouge
:rouge-linenums-mode: inline
[source#{opts},ruby,highlight=1]
----
puts 'Hello, world!'
puts 'Goodbye, world!'
----
EOS
expected = <<~EOS.chop
<span class="hll"><span class="nb">puts</span> <span class="s1">'Hello, world!'</span>
</span>#{opts == '%linenums' ? '<span class="linenos">2</span>' : ''}<span class="nb">puts</span> <span class="s1">'Goodbye, world!'</span></code></pre>
EOS
output = convert_string_to_embedded input, safe: :safe
assert_includes output, expected
end
end
test 'should line highlight specified lines when last line is highlighted' do
['', '%linenums'].each do |opts|
input = <<~EOS
:source-highlighter: rouge
:rouge-linenums-mode: inline
[source#{opts},ruby,highlight=2]
----
puts 'Hello, world!'
puts 'Goodbye, world!'
----
EOS
expected = <<~EOS.chop
<span class="nb">puts</span> <span class="s1">'Hello, world!'</span>
#{opts == '%linenums' ? '<span class="linenos">2</span>' : ''}<span class="hll"><span class="nb">puts</span> <span class="s1">'Goodbye, world!'</span>
</span></code></pre>
EOS
output = convert_string_to_embedded input, safe: :safe
assert_includes output, expected
end
end
test 'should line highlight specified lines relative to start value when linenums mode is table' do
input = <<~EOS
:source-highlighter: rouge
[source%linenums,ruby,start=5,highlight=6]
----
get {
render "Hello, World!"
}
----
EOS
output = convert_string_to_embedded input, safe: :safe
assert_css 'table.linenotable td.linenos', output, 3
assert_css 'table.linenotable td.linenos pre', output, 3
assert_xpath '(//td[@class="linenos"])[1]/pre[text()="5"]', output, 1
assert_xpath '(//td[@class="linenos"])[2]/pre[text()="6"]', output, 1
assert_xpath '(//td[@class="linenos"])[3]/pre[text()="7"]', output, 1
end
test 'should line highlight specified lines relative to start value when linenums mode is inline' do
input = <<~EOS
:source-highlighter: rouge
:rouge-linenums-mode: inline
[source%linenums,ruby,start=5,highlight=6]
----
get {
render "Hello, World!"
}
----
EOS
expected = <<~EOS.chop
<span class="linenos">5</span><span class="n">get</span> <span class="p">{</span>
<span class="linenos">6</span><span class="hll"> <span class="n">render</span> <span class="s2">"Hello, World!"</span>
</span><span class="linenos">7</span><span class="p">}</span></code></pre>
EOS
output = convert_string_to_embedded input, safe: :safe
assert_includes output, expected
end
test 'should ignore start attribute when the value is 0' do
input = <<~EOS
:source-highlighter: rouge
:rouge-linenums-mode: inline
[source%linenums,ruby,start=0,highlight=6]
----
get {
render "Hello, World!"
}
----
EOS
expected = <<~EOS.chop
<span class="linenos">1</span><span class="n">get</span> <span class="p">{</span>