forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
substitutions_test.rb
2302 lines (1947 loc) · 115 KB
/
substitutions_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'
# TODO
# - test negatives
# - test role on every quote type
context 'Substitutions' do
BACKSLASH = ?\\
context 'Dispatcher' do
test 'apply normal substitutions' do
para = block_from_string("[blue]_http://asciidoc.org[AsciiDoc]_ & [red]*Ruby*\n§ Making +++<u>documentation</u>+++ together +\nsince (C) {inception_year}.")
para.document.attributes['inception_year'] = '2012'
result = para.apply_subs(para.source)
assert_equal %{<em class="blue"><a href="http://asciidoc.org">AsciiDoc</a></em> & <strong class="red">Ruby</strong>\n§ Making <u>documentation</u> together<br>\nsince © 2012.}, result
end
test 'apply_subs should not modify string directly' do
input = '<html> -- the root of all web'
para = block_from_string input
para_source = para.source
result = para.apply_subs para_source
assert_equal '<html> — the root of all web', result
assert_equal input, para_source
end
test 'should not drop trailing blank lines when performing substitutions' do
para = block_from_string %([%hardbreaks]\nthis\nis\n-> {program})
para.lines << ''
para.lines << ''
para.document.attributes['program'] = 'Asciidoctor'
result = para.apply_subs(para.lines)
assert_equal ['this<br>', 'is<br>', '→ Asciidoctor<br>', '<br>', ''], result
result = para.apply_subs(para.lines * "\n")
assert_equal %(this<br>\nis<br>\n→ Asciidoctor<br>\n<br>\n), result
end
test 'should expand subs passed to expand_subs' do
para = block_from_string %({program}\n*bold*\n2 > 1)
para.document.attributes['program'] = 'Asciidoctor'
assert_equal [:specialcharacters], (para.expand_subs [:specialchars])
refute para.expand_subs([:none])
assert_equal [:specialcharacters, :quotes, :attributes, :replacements, :macros, :post_replacements], (para.expand_subs [:normal])
end
test 'apply_subs should allow the subs argument to be nil' do
block = block_from_string %([pass]\n*raw*)
result = block.apply_subs block.source, nil
assert_equal '*raw*', result
end
end
context 'Quotes' do
test 'single-line double-quoted string' do
para = block_from_string(%q{``a few quoted words''}, attributes: { 'compat-mode' => '' })
assert_equal '“a few quoted words”', para.sub_quotes(para.source)
para = block_from_string(%q{"`a few quoted words`"})
assert_equal '“a few quoted words”', para.sub_quotes(para.source)
para = block_from_string(%q{"`a few quoted words`"}, backend: 'docbook')
assert_equal '<quote>a few quoted words</quote>', para.sub_quotes(para.source)
end
test 'escaped single-line double-quoted string' do
para = block_from_string %(#{BACKSLASH}``a few quoted words''), attributes: { 'compat-mode' => '' }
assert_equal %q(‘`a few quoted words’'), para.sub_quotes(para.source)
para = block_from_string %(#{BACKSLASH * 2}``a few quoted words''), attributes: { 'compat-mode' => '' }
assert_equal %q(``a few quoted words''), para.sub_quotes(para.source)
para = block_from_string(%(#{BACKSLASH}"`a few quoted words`"))
assert_equal %q("`a few quoted words`"), para.sub_quotes(para.source)
para = block_from_string(%(#{BACKSLASH * 2}"`a few quoted words`"))
assert_equal %(#{BACKSLASH}"`a few quoted words`"), para.sub_quotes(para.source)
end
test 'multi-line double-quoted string' do
para = block_from_string(%Q{``a few\nquoted words''}, attributes: { 'compat-mode' => '' })
assert_equal "“a few\nquoted words”", para.sub_quotes(para.source)
para = block_from_string(%Q{"`a few\nquoted words`"})
assert_equal "“a few\nquoted words”", para.sub_quotes(para.source)
end
test 'double-quoted string with inline single quote' do
para = block_from_string(%q{``Here's Johnny!''}, attributes: { 'compat-mode' => '' })
assert_equal %q{“Here's Johnny!”}, para.sub_quotes(para.source)
para = block_from_string(%q{"`Here's Johnny!`"})
assert_equal %q{“Here's Johnny!”}, para.sub_quotes(para.source)
end
test 'double-quoted string with inline backquote' do
para = block_from_string(%q{``Here`s Johnny!''}, attributes: { 'compat-mode' => '' })
assert_equal %q{“Here`s Johnny!”}, para.sub_quotes(para.source)
para = block_from_string(%q{"`Here`s Johnny!`"})
assert_equal %q{“Here`s Johnny!”}, para.sub_quotes(para.source)
end
test 'double-quoted string around monospaced text' do
para = block_from_string(%q("``E=mc^2^` is the solution!`"))
assert_equal %q(“`E=mc<sup>2</sup>` is the solution!”), para.apply_subs(para.source);
para = block_from_string(%q("```E=mc^2^`` is the solution!`"))
assert_equal %q(“<code>E=mc<sup>2</sup></code> is the solution!”), para.apply_subs(para.source);
end
test 'single-line single-quoted string' do
para = block_from_string(%q{`a few quoted words'}, attributes: { 'compat-mode' => '' })
assert_equal '‘a few quoted words’', para.sub_quotes(para.source)
para = block_from_string(%q{'`a few quoted words`'})
assert_equal '‘a few quoted words’', para.sub_quotes(para.source)
para = block_from_string(%q{'`a few quoted words`'}, backend: 'docbook')
assert_equal '<quote>a few quoted words</quote>', para.sub_quotes(para.source)
end
test 'escaped single-line single-quoted string' do
para = block_from_string(%(#{BACKSLASH}`a few quoted words'), attributes: { 'compat-mode' => '' })
assert_equal %(`a few quoted words'), para.sub_quotes(para.source)
para = block_from_string(%(#{BACKSLASH}'`a few quoted words`'))
assert_equal %('`a few quoted words`'), para.sub_quotes(para.source)
end
test 'multi-line single-quoted string' do
para = block_from_string(%Q{`a few\nquoted words'}, attributes: { 'compat-mode' => '' })
assert_equal "‘a few\nquoted words’", para.sub_quotes(para.source)
para = block_from_string(%Q{'`a few\nquoted words`'})
assert_equal "‘a few\nquoted words’", para.sub_quotes(para.source)
end
test 'single-quoted string with inline single quote' do
para = block_from_string(%q{`That isn't what I did.'}, attributes: { 'compat-mode' => '' })
assert_equal %q{‘That isn't what I did.’}, para.sub_quotes(para.source)
para = block_from_string(%q{'`That isn't what I did.`'})
assert_equal %q{‘That isn't what I did.’}, para.sub_quotes(para.source)
end
test 'single-quoted string with inline backquote' do
para = block_from_string(%q{`Here`s Johnny!'}, attributes: { 'compat-mode' => '' })
assert_equal %q{‘Here`s Johnny!’}, para.sub_quotes(para.source)
para = block_from_string(%q{'`Here`s Johnny!`'})
assert_equal %q{‘Here`s Johnny!’}, para.sub_quotes(para.source)
end
test 'single-line constrained marked string' do
#para = block_from_string(%q{#a few words#}, attributes: { 'compat-mode' => '' })
#assert_equal 'a few words', para.sub_quotes(para.source)
para = block_from_string(%q{#a few words#})
assert_equal '<mark>a few words</mark>', para.sub_quotes(para.source)
end
test 'escaped single-line constrained marked string' do
para = block_from_string(%(#{BACKSLASH}#a few words#))
assert_equal '#a few words#', para.sub_quotes(para.source)
end
test 'multi-line constrained marked string' do
#para = block_from_string(%Q{#a few\nwords#}, attributes: { 'compat-mode' => '' })
#assert_equal "a few\nwords", para.sub_quotes(para.source)
para = block_from_string(%Q{#a few\nwords#})
assert_equal "<mark>a few\nwords</mark>", para.sub_quotes(para.source)
end
test 'constrained marked string should not match entity references' do
para = block_from_string('111 #mark a# 222 "`quote a`" 333 #mark b# 444')
assert_equal %(111 <mark>mark a</mark> 222 “quote a” 333 <mark>mark b</mark> 444), para.sub_quotes(para.source)
end
test 'single-line unconstrained marked string' do
#para = block_from_string(%q{##--anything goes ##}, attributes: { 'compat-mode' => '' })
#assert_equal '--anything goes ', para.sub_quotes(para.source)
para = block_from_string(%q{##--anything goes ##})
assert_equal '<mark>--anything goes </mark>', para.sub_quotes(para.source)
end
test 'escaped single-line unconstrained marked string' do
para = block_from_string(%(#{BACKSLASH}#{BACKSLASH}##--anything goes ##))
assert_equal '##--anything goes ##', para.sub_quotes(para.source)
end
test 'multi-line unconstrained marked string' do
#para = block_from_string(%Q{##--anything\ngoes ##}, attributes: { 'compat-mode' => '' })
#assert_equal "--anything\ngoes ", para.sub_quotes(para.source)
para = block_from_string(%Q{##--anything\ngoes ##})
assert_equal "<mark>--anything\ngoes </mark>", para.sub_quotes(para.source)
end
test 'single-line constrained marked string with role' do
para = block_from_string(%q{[statement]#a few words#})
assert_equal '<span class="statement">a few words</span>', para.sub_quotes(para.source)
end
test 'single-line constrained strong string' do
para = block_from_string(%q{*a few strong words*})
assert_equal '<strong>a few strong words</strong>', para.sub_quotes(para.source)
end
test 'escaped single-line constrained strong string' do
para = block_from_string(%(#{BACKSLASH}*a few strong words*))
assert_equal '*a few strong words*', para.sub_quotes(para.source)
end
test 'multi-line constrained strong string' do
para = block_from_string(%Q{*a few\nstrong words*})
assert_equal "<strong>a few\nstrong words</strong>", para.sub_quotes(para.source)
end
test 'constrained strong string containing an asterisk' do
para = block_from_string(%q{*bl*ck*-eye})
assert_equal '<strong>bl*ck</strong>-eye', para.sub_quotes(para.source)
end
test 'constrained strong string containing an asterisk and multibyte word chars' do
para = block_from_string(%q{*黑*眼圈*})
assert_equal '<strong>黑*眼圈</strong>', para.sub_quotes(para.source)
end
test 'single-line constrained quote variation emphasized string' do
para = block_from_string(%q{_a few emphasized words_})
assert_equal '<em>a few emphasized words</em>', para.sub_quotes(para.source)
end
test 'escaped single-line constrained quote variation emphasized string' do
para = block_from_string(%(#{BACKSLASH}_a few emphasized words_))
assert_equal %q(_a few emphasized words_), para.sub_quotes(para.source)
end
test 'escaped single quoted string' do
para = block_from_string(%(#{BACKSLASH}'a few emphasized words'))
# NOTE the \' is replaced with ' by the :replacements substitution, later in the substitution pipeline
assert_equal %(#{BACKSLASH}'a few emphasized words'), para.sub_quotes(para.source)
end
test 'multi-line constrained emphasized quote variation string' do
para = block_from_string(%Q{_a few\nemphasized words_})
assert_equal "<em>a few\nemphasized words</em>", para.sub_quotes(para.source)
end
test 'single-quoted string containing an emphasized phrase' do
para = block_from_string(%q{`I told him, 'Just go for it!''}, attributes: { 'compat-mode' => '' })
assert_equal '‘I told him, <em>Just go for it!</em>’', para.sub_quotes(para.source)
para = block_from_string(%q{'`I told him, 'Just go for it!'`'})
assert_equal %q(‘I told him, 'Just go for it!'’), para.sub_quotes(para.source)
end
test 'escaped single-quotes inside emphasized words are restored' do
para = block_from_string(%('Here#{BACKSLASH}'s Johnny!'), attributes: { 'compat-mode' => '' })
assert_equal %q(<em>Here's Johnny!</em>), para.apply_subs(para.source)
para = block_from_string(%('Here#{BACKSLASH}'s Johnny!'))
assert_equal %q('Here's Johnny!'), para.apply_subs(para.source)
end
test 'single-line constrained emphasized underline variation string' do
para = block_from_string(%q{_a few emphasized words_})
assert_equal '<em>a few emphasized words</em>', para.sub_quotes(para.source)
end
test 'escaped single-line constrained emphasized underline variation string' do
para = block_from_string(%(#{BACKSLASH}_a few emphasized words_))
assert_equal '_a few emphasized words_', para.sub_quotes(para.source)
end
test 'multi-line constrained emphasized underline variation string' do
para = block_from_string(%Q{_a few\nemphasized words_})
assert_equal "<em>a few\nemphasized words</em>", para.sub_quotes(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'single-line constrained monospaced string' do
para = block_from_string(%(`a few <{monospaced}> words`), attributes: { 'monospaced' => 'monospaced', 'compat-mode' => '' })
assert_equal '<code>a few <{monospaced}> words</code>', para.apply_subs(para.source)
para = block_from_string(%(`a few <{monospaced}> words`), attributes: { 'monospaced' => 'monospaced' })
assert_equal '<code>a few <monospaced> words</code>', para.apply_subs(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'single-line constrained monospaced string with role' do
para = block_from_string(%([input]`a few <{monospaced}> words`), attributes: { 'monospaced' => 'monospaced', 'compat-mode' => '' })
assert_equal '<code class="input">a few <{monospaced}> words</code>', para.apply_subs(para.source)
para = block_from_string(%([input]`a few <{monospaced}> words`), attributes: { 'monospaced' => 'monospaced' })
assert_equal '<code class="input">a few <monospaced> words</code>', para.apply_subs(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'escaped single-line constrained monospaced string' do
para = block_from_string(%(#{BACKSLASH}`a few <monospaced> words`), attributes: { 'compat-mode' => '' })
assert_equal '`a few <monospaced> words`', para.apply_subs(para.source)
para = block_from_string(%(#{BACKSLASH}`a few <monospaced> words`))
assert_equal '`a few <monospaced> words`', para.apply_subs(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'escaped single-line constrained monospaced string with role' do
para = block_from_string(%([input]#{BACKSLASH}`a few <monospaced> words`), attributes: { 'compat-mode' => '' })
assert_equal '[input]`a few <monospaced> words`', para.apply_subs(para.source)
para = block_from_string(%([input]#{BACKSLASH}`a few <monospaced> words`))
assert_equal '[input]`a few <monospaced> words`', para.apply_subs(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'escaped role on single-line constrained monospaced string' do
para = block_from_string(%(#{BACKSLASH}[input]`a few <monospaced> words`), attributes: { 'compat-mode' => '' })
assert_equal '[input]<code>a few <monospaced> words</code>', para.apply_subs(para.source)
para = block_from_string(%(#{BACKSLASH}[input]`a few <monospaced> words`))
assert_equal '[input]<code>a few <monospaced> words</code>', para.apply_subs(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'escaped role on escaped single-line constrained monospaced string' do
para = block_from_string(%(#{BACKSLASH}[input]#{BACKSLASH}`a few <monospaced> words`), attributes: { 'compat-mode' => '' })
assert_equal %(#{BACKSLASH}[input]`a few <monospaced> words`), para.apply_subs(para.source)
para = block_from_string(%(#{BACKSLASH}[input]#{BACKSLASH}`a few <monospaced> words`))
assert_equal %(#{BACKSLASH}[input]`a few <monospaced> words`), para.apply_subs(para.source)
end
# NOTE must use apply_subs because constrained monospaced is handled as a passthrough
test 'multi-line constrained monospaced string' do
para = block_from_string(%(`a few\n<{monospaced}> words`), attributes: { 'monospaced' => 'monospaced', 'compat-mode' => '' })
assert_equal "<code>a few\n<{monospaced}> words</code>", para.apply_subs(para.source)
para = block_from_string(%(`a few\n<{monospaced}> words`), attributes: { 'monospaced' => 'monospaced' })
assert_equal "<code>a few\n<monospaced> words</code>", para.apply_subs(para.source)
end
test 'single-line unconstrained strong chars' do
para = block_from_string(%q{**Git**Hub})
assert_equal '<strong>Git</strong>Hub', para.sub_quotes(para.source)
end
test 'escaped single-line unconstrained strong chars' do
para = block_from_string(%(#{BACKSLASH}**Git**Hub))
assert_equal '<strong>*Git</strong>*Hub', para.sub_quotes(para.source)
end
test 'multi-line unconstrained strong chars' do
para = block_from_string(%Q{**G\ni\nt\n**Hub})
assert_equal "<strong>G\ni\nt\n</strong>Hub", para.sub_quotes(para.source)
end
test 'unconstrained strong chars with inline asterisk' do
para = block_from_string(%q{**bl*ck**-eye})
assert_equal '<strong>bl*ck</strong>-eye', para.sub_quotes(para.source)
end
test 'unconstrained strong chars with role' do
para = block_from_string(%q{Git[blue]**Hub**})
assert_equal %q{Git<strong class="blue">Hub</strong>}, para.sub_quotes(para.source)
end
# TODO this is not the same result as AsciiDoc, though I don't understand why AsciiDoc gets what it gets
test 'escaped unconstrained strong chars with role' do
para = block_from_string(%(Git#{BACKSLASH}[blue]**Hub**))
assert_equal %q{Git[blue]<strong>*Hub</strong>*}, para.sub_quotes(para.source)
end
test 'single-line unconstrained emphasized chars' do
para = block_from_string(%q{__Git__Hub})
assert_equal '<em>Git</em>Hub', para.sub_quotes(para.source)
end
test 'escaped single-line unconstrained emphasized chars' do
para = block_from_string(%(#{BACKSLASH}__Git__Hub))
assert_equal '__Git__Hub', para.sub_quotes(para.source)
end
test 'escaped single-line unconstrained emphasized chars around word' do
para = block_from_string(%(#{BACKSLASH}#{BACKSLASH}__GitHub__))
assert_equal '__GitHub__', para.sub_quotes(para.source)
end
test 'multi-line unconstrained emphasized chars' do
para = block_from_string(%Q{__G\ni\nt\n__Hub})
assert_equal "<em>G\ni\nt\n</em>Hub", para.sub_quotes(para.source)
end
test 'unconstrained emphasis chars with role' do
para = block_from_string(%q{[gray]__Git__Hub})
assert_equal %q{<em class="gray">Git</em>Hub}, para.sub_quotes(para.source)
end
test 'escaped unconstrained emphasis chars with role' do
para = block_from_string(%(#{BACKSLASH}[gray]__Git__Hub))
assert_equal %q{[gray]__Git__Hub}, para.sub_quotes(para.source)
end
test 'single-line constrained monospaced chars' do
para = block_from_string(%q{call +save()+ to persist the changes}, attributes: { 'compat-mode' => '' })
assert_equal 'call <code>save()</code> to persist the changes', para.sub_quotes(para.source)
para = block_from_string(%q{call [x-]+save()+ to persist the changes})
assert_equal 'call <code>save()</code> to persist the changes', para.apply_subs(para.source)
para = block_from_string(%q{call `save()` to persist the changes})
assert_equal 'call <code>save()</code> to persist the changes', para.sub_quotes(para.source)
end
test 'single-line constrained monospaced chars with role' do
para = block_from_string(%q{call [method]+save()+ to persist the changes}, attributes: { 'compat-mode' => '' })
assert_equal 'call <code class="method">save()</code> to persist the changes', para.sub_quotes(para.source)
para = block_from_string(%q{call [method x-]+save()+ to persist the changes})
assert_equal 'call <code class="method">save()</code> to persist the changes', para.apply_subs(para.source)
para = block_from_string(%q{call [method]`save()` to persist the changes})
assert_equal 'call <code class="method">save()</code> to persist the changes', para.sub_quotes(para.source)
end
test 'escaped single-line constrained monospaced chars' do
para = block_from_string(%(call #{BACKSLASH}+save()+ to persist the changes), attributes: { 'compat-mode' => '' })
assert_equal 'call +save()+ to persist the changes', para.sub_quotes(para.source)
para = block_from_string(%(call #{BACKSLASH}`save()` to persist the changes))
assert_equal 'call `save()` to persist the changes', para.sub_quotes(para.source)
end
test 'escaped single-line constrained monospaced chars with role' do
para = block_from_string(%(call [method]#{BACKSLASH}+save()+ to persist the changes), attributes: { 'compat-mode' => '' })
assert_equal 'call [method]+save()+ to persist the changes', para.sub_quotes(para.source)
para = block_from_string(%(call [method]#{BACKSLASH}`save()` to persist the changes))
assert_equal 'call [method]`save()` to persist the changes', para.sub_quotes(para.source)
end
test 'escaped role on single-line constrained monospaced chars' do
para = block_from_string(%(call #{BACKSLASH}[method]+save()+ to persist the changes), attributes: { 'compat-mode' => '' })
assert_equal 'call [method]<code>save()</code> to persist the changes', para.sub_quotes(para.source)
para = block_from_string(%(call #{BACKSLASH}[method]`save()` to persist the changes))
assert_equal 'call [method]<code>save()</code> to persist the changes', para.sub_quotes(para.source)
end
test 'escaped role on escaped single-line constrained monospaced chars' do
para = block_from_string(%(call #{BACKSLASH}[method]#{BACKSLASH}+save()+ to persist the changes), attributes: { 'compat-mode' => '' })
assert_equal %(call #{BACKSLASH}[method]+save()+ to persist the changes), para.sub_quotes(para.source)
para = block_from_string(%(call #{BACKSLASH}[method]#{BACKSLASH}`save()` to persist the changes))
assert_equal %(call #{BACKSLASH}[method]`save()` to persist the changes), para.sub_quotes(para.source)
end
test 'single-line unconstrained monospaced chars' do
para = block_from_string(%q{Git++Hub++}, attributes: { 'compat-mode' => '' })
assert_equal 'Git<code>Hub</code>', para.sub_quotes(para.source)
para = block_from_string(%q{Git[x-]++Hub++})
assert_equal 'Git<code>Hub</code>', para.apply_subs(para.source)
para = block_from_string(%q{Git``Hub``})
assert_equal 'Git<code>Hub</code>', para.sub_quotes(para.source)
end
test 'escaped single-line unconstrained monospaced chars' do
para = block_from_string(%(Git#{BACKSLASH}++Hub++), attributes: { 'compat-mode' => '' })
assert_equal 'Git+<code>Hub</code>+', para.sub_quotes(para.source)
para = block_from_string(%(Git#{BACKSLASH * 2}++Hub++), attributes: { 'compat-mode' => '' })
assert_equal 'Git++Hub++', para.sub_quotes(para.source)
para = block_from_string(%(Git#{BACKSLASH}``Hub``))
assert_equal 'Git``Hub``', para.sub_quotes(para.source)
end
test 'multi-line unconstrained monospaced chars' do
para = block_from_string(%Q{Git++\nH\nu\nb++}, attributes: { 'compat-mode' => '' })
assert_equal "Git<code>\nH\nu\nb</code>", para.sub_quotes(para.source)
para = block_from_string(%Q{Git[x-]++\nH\nu\nb++})
assert_equal %(Git<code>\nH\nu\nb</code>), para.apply_subs(para.source)
para = block_from_string(%Q{Git``\nH\nu\nb``})
assert_equal "Git<code>\nH\nu\nb</code>", para.sub_quotes(para.source)
end
test 'single-line superscript chars' do
para = block_from_string(%(x^2^ = x * x, e = mc^2^, there's a 1^st^ time for everything))
assert_equal %(x<sup>2</sup> = x * x, e = mc<sup>2</sup>, there\'s a 1<sup>st</sup> time for everything), para.sub_quotes(para.source)
end
test 'escaped single-line superscript chars' do
para = block_from_string(%(x#{BACKSLASH}^2^ = x * x))
assert_equal 'x^2^ = x * x', para.sub_quotes(para.source)
end
test 'does not match superscript across whitespace' do
para = block_from_string(%Q{x^(n\n-\n1)^})
assert_equal para.source, para.sub_quotes(para.source)
end
test 'does not match adjacent superscript chars' do
para = block_from_string 'a ^^ b'
assert_equal 'a ^^ b', para.sub_quotes(para.source)
end
test 'does not confuse superscript and links with blank window shorthand' do
para = block_from_string(%Q{http://localhost[Text^] on the 21^st^ and 22^nd^})
assert_equal '<a href="http://localhost" target="_blank" rel="noopener">Text</a> on the 21<sup>st</sup> and 22<sup>nd</sup>', para.content
end
test 'single-line subscript chars' do
para = block_from_string(%q{H~2~O})
assert_equal 'H<sub>2</sub>O', para.sub_quotes(para.source)
end
test 'escaped single-line subscript chars' do
para = block_from_string(%(H#{BACKSLASH}~2~O))
assert_equal 'H~2~O', para.sub_quotes(para.source)
end
test 'does not match subscript across whitespace' do
para = block_from_string(%Q{project~ view\non\nGitHub~})
assert_equal para.source, para.sub_quotes(para.source)
end
test 'does not match adjacent subscript chars' do
para = block_from_string 'a ~~ b'
assert_equal 'a ~~ b', para.sub_quotes(para.source)
end
test 'does not match subscript across distinct URLs' do
para = block_from_string(%Q{http://www.abc.com/~def[DEF] and http://www.abc.com/~ghi[GHI]})
assert_equal para.source, para.sub_quotes(para.source)
end
test 'quoted text with role shorthand' do
para = block_from_string(%q{[.white.red-background]#alert#})
assert_equal '<span class="white red-background">alert</span>', para.sub_quotes(para.source)
end
test 'quoted text with id shorthand' do
para = block_from_string(%q{[#bond]#007#})
assert_equal '<span id="bond">007</span>', para.sub_quotes(para.source)
end
test 'quoted text with id and role shorthand' do
para = block_from_string(%q{[#bond.white.red-background]#007#})
assert_equal '<span id="bond" class="white red-background">007</span>', para.sub_quotes(para.source)
end
test 'quoted text with id and role shorthand using docbook backend' do
para = block_from_string(%q{[#bond.white.red-background]#007#}, backend: 'docbook')
assert_equal '<anchor xml:id="bond" xreflabel="007"/><phrase role="white red-background">007</phrase>', para.sub_quotes(para.source)
end
test 'should ignore attributes after comma' do
para = block_from_string(%q{[red, foobar]#alert#})
assert_equal '<span class="red">alert</span>', para.sub_quotes(para.source)
end
test 'inline passthrough with id and role set using shorthand' do
%w(#idname.rolename .rolename#idname).each do |attrlist|
para = block_from_string %([#{attrlist}]+pass+)
assert_equal '<span id="idname" class="rolename">pass</span>', para.content
end
end
test 'should not assign role attribute if shorthand style has no roles' do
para = block_from_string '[#idname]*blah*'
assert_equal '<strong id="idname">blah</strong>', para.content
end
end
context 'Macros' do
test 'a single-line link macro should be interpreted as a link' do
para = block_from_string('link:/home.html[]')
assert_equal %q{<a href="/home.html" class="bare">/home.html</a>}, para.sub_macros(para.source)
end
test 'a single-line link macro with text should be interpreted as a link' do
para = block_from_string('link:/home.html[Home]')
assert_equal %q{<a href="/home.html">Home</a>}, para.sub_macros(para.source)
end
test 'a mailto macro should be interpreted as a mailto link' do
para = block_from_string('mailto:[email protected][]')
assert_equal %q{<a href="mailto:[email protected]">[email protected]</a>}, para.sub_macros(para.source)
end
test 'a mailto macro with text should be interpreted as a mailto link' do
para = block_from_string('mailto:[email protected][Doc Writer]')
assert_equal %q{<a href="mailto:[email protected]">Doc Writer</a>}, para.sub_macros(para.source)
end
test 'a mailto macro with text and subject should be interpreted as a mailto link' do
para = block_from_string('mailto:[email protected][Doc Writer, Pull request]')
assert_equal %q{<a href="mailto:[email protected]?subject=Pull+request">Doc Writer</a>}, para.sub_macros(para.source)
end
test 'a mailto macro with text, subject and body should be interpreted as a mailto link' do
para = block_from_string('mailto:[email protected][Doc Writer, Pull request, Please accept my pull request]')
assert_equal %q{<a href="mailto:[email protected]?subject=Pull+request&body=Please+accept+my+pull+request">Doc Writer</a>}, para.sub_macros(para.source)
end
test 'a mailto macro with subject and body only should use e-mail as text' do
para = block_from_string('mailto:[email protected][,Pull request,Please accept my pull request]')
assert_equal %q{<a href="mailto:[email protected]?subject=Pull+request&body=Please+accept+my+pull+request">[email protected]</a>}, para.sub_macros(para.source)
end
test 'a mailto macro supports id and role attributes' do
para = block_from_string('mailto:[email protected][,id=contact,role=icon]')
assert_equal %q{<a href="mailto:[email protected]" id="contact" class="icon">[email protected]</a>}, para.sub_macros(para.source)
end
test 'should recognize inline email addresses' do
%w(
joe_bloggs@mail_server.com
).each do |input|
para = block_from_string input
assert_equal %(<a href="mailto:#{input}">#{input}</a>), (para.sub_macros para.source)
end
end
test 'should recognize inline email address containing an ampersand' do
para = block_from_string('bert&[email protected]')
assert_equal %q{<a href="mailto:bert&[email protected]">bert&[email protected]</a>}, para.apply_subs(para.source)
end
test 'should recognize inline email address surrounded by angle brackets' do
para = block_from_string('<[email protected]>')
assert_equal %q{<<a href="mailto:[email protected]">[email protected]</a>>}, para.apply_subs(para.source)
end
test 'should ignore escaped inline email address' do
para = block_from_string(%(#{BACKSLASH}[email protected]))
assert_equal %q{[email protected]}, para.sub_macros(para.source)
end
test 'a single-line raw url should be interpreted as a link' do
para = block_from_string('http://google.com')
assert_equal %q{<a href="http://google.com" class="bare">http://google.com</a>}, para.sub_macros(para.source)
end
test 'a single-line raw url with text should be interpreted as a link' do
para = block_from_string('http://google.com[Google]')
assert_equal %q{<a href="http://google.com">Google</a>}, para.sub_macros(para.source)
end
test 'a multi-line raw url with text should be interpreted as a link' do
para = block_from_string("http://google.com[Google\nHomepage]")
assert_equal %{<a href="http://google.com">Google\nHomepage</a>}, para.sub_macros(para.source)
end
test 'a single-line raw url with attribute as text should be interpreted as a link with resolved attribute' do
para = block_from_string("http://google.com[{google_homepage}]")
para.document.attributes['google_homepage'] = 'Google Homepage'
assert_equal %q{<a href="http://google.com">Google Homepage</a>}, para.sub_macros(para.sub_attributes(para.source))
end
test 'should not resolve an escaped attribute in link text' do
{
'http://google.com' => "http://google.com[#{BACKSLASH}{google_homepage}]",
'http://google.com?q=,' => "link:http://google.com?q=,[#{BACKSLASH}{google_homepage}]",
}.each do |uri, macro|
para = block_from_string macro
para.document.attributes['google_homepage'] = 'Google Homepage'
assert_equal %(<a href="#{uri}">{google_homepage}</a>), para.sub_macros(para.sub_attributes(para.source))
end
end
test 'a single-line escaped raw url should not be interpreted as a link' do
para = block_from_string(%(#{BACKSLASH}http://google.com))
assert_equal %q{http://google.com}, para.sub_macros(para.source)
end
test 'a comma separated list of links should not include commas in links' do
para = block_from_string('http://foo.com, http://bar.com, http://example.org')
assert_equal %q{<a href="http://foo.com" class="bare">http://foo.com</a>, <a href="http://bar.com" class="bare">http://bar.com</a>, <a href="http://example.org" class="bare">http://example.org</a>}, para.sub_macros(para.source)
end
test 'a single-line image macro should be interpreted as an image' do
para = block_from_string('image:tiger.png[]')
assert_equal %{<span class="image"><img src="tiger.png" alt="tiger"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should replace underscore and hyphen with space in generated alt text for an inline image' do
para = block_from_string('image:tiger-with-family_1.png[]')
assert_equal %{<span class="image"><img src="tiger-with-family_1.png" alt="tiger with family 1"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'a single-line image macro with text should be interpreted as an image with alt text' do
para = block_from_string('image:tiger.png[Tiger]')
assert_equal %{<span class="image"><img src="tiger.png" alt="Tiger"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should encode special characters in alt text of inline image' do
input = 'A tiger\'s "roar" is < a bear\'s "growl"'
expected = 'A tiger’s "roar" is < a bear’s "growl"'
output = (convert_inline_string %(image:tiger-roar.png[#{input}])).gsub(/>\s+</, '><')
assert_equal %(<span class="image"><img src="tiger-roar.png" alt="#{expected}"></span>), output
end
test 'an image macro with SVG image and text should be interpreted as an image with alt text' do
para = block_from_string('image:tiger.svg[Tiger]')
assert_equal %{<span class="image"><img src="tiger.svg" alt="Tiger"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an image macro with an interactive SVG image and alt text should be converted to an object element' do
para = block_from_string('image:tiger.svg[Tiger,opts=interactive]', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'imagesdir' => 'images' })
assert_equal %{<span class="image"><object type="image/svg+xml" data="images/tiger.svg"><span class="alt">Tiger</span></object></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an image macro with an interactive SVG image, fallback and alt text should be converted to an object element' do
para = block_from_string('image:tiger.svg[Tiger,fallback=tiger.png,opts=interactive]', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'imagesdir' => 'images' })
assert_equal %{<span class="image"><object type="image/svg+xml" data="images/tiger.svg"><img src="images/tiger.png" alt="Tiger"></object></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an image macro with an inline SVG image should be converted to an svg element' do
para = block_from_string('image:circle.svg[Tiger,100,opts=inline]', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'imagesdir' => 'fixtures', 'docdir' => testdir })
result = para.sub_macros(para.source).gsub(/>\s+</, '><')
assert_match(/<svg\s[^>]*width="100"[^>]*>/, result)
refute_match(/<svg\s[^>]*width="500"[^>]*>/, result)
refute_match(/<svg\s[^>]*height="500"[^>]*>/, result)
refute_match(/<svg\s[^>]*style="[^>]*>/, result)
end
test 'an image macro with an inline SVG image should be converted to an svg element even when data-uri is set' do
para = block_from_string('image:circle.svg[Tiger,100,opts=inline]', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'data-uri' => '', 'imagesdir' => 'fixtures', 'docdir' => testdir })
assert_match(/<svg\s[^>]*width="100">/, para.sub_macros(para.source).gsub(/>\s+</, '><'))
end
test 'an image macro with an SVG image should not use an object element when safe mode is secure' do
para = block_from_string('image:tiger.svg[Tiger,opts=interactive]', attributes: { 'imagesdir' => 'images' })
assert_equal %{<span class="image"><img src="images/tiger.svg" alt="Tiger"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'a single-line image macro with text containing escaped square bracket should be interpreted as an image with alt text' do
para = block_from_string(%(image:tiger.png[[Another#{BACKSLASH}] Tiger]))
assert_equal %{<span class="image"><img src="tiger.png" alt="[Another] Tiger"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'a single-line image macro with text and dimensions should be interpreted as an image with alt text and dimensions' do
para = block_from_string('image:tiger.png[Tiger, 200, 100]')
assert_equal %{<span class="image"><img src="tiger.png" alt="Tiger" width="200" height="100"></span>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'a single-line image macro with text and dimensions should be interpreted as an image with alt text and dimensions in docbook' do
para = block_from_string 'image:tiger.png[Tiger, 200, 100]', backend: 'docbook'
assert_equal %{<inlinemediaobject><imageobject><imagedata fileref="tiger.png" contentwidth="200" contentdepth="100"/></imageobject><textobject><phrase>Tiger</phrase></textobject></inlinemediaobject>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should pass through role on image macro to DocBook output' do
para = block_from_string 'image:tiger.png[Tiger,200,role=animal]', backend: 'docbook'
result = para.sub_macros(para.source)
assert_includes result, '<inlinemediaobject role="animal">'
end
test 'a single-line image macro with text and link should be interpreted as a linked image with alt text' do
para = block_from_string('image:tiger.png[Tiger, link="http://en.wikipedia.org/wiki/Tiger"]')
assert_equal %{<span class="image"><a class="image" href="http://en.wikipedia.org/wiki/Tiger"><img src="tiger.png" alt="Tiger"></a></span>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'rel=noopener should be added to an image with a link that targets the _blank window' do
para = block_from_string 'image:tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,window=_blank]'
assert_equal %{<span class="image"><a class="image" href="http://en.wikipedia.org/wiki/Tiger" target="_blank" rel="noopener"><img src="tiger.png" alt="Tiger"></a></span>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'rel=noopener should be added to an image with a link that targets a named window when the noopener option is set' do
para = block_from_string 'image:tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,window=name,opts=noopener]'
assert_equal %{<span class="image"><a class="image" href="http://en.wikipedia.org/wiki/Tiger" target="name" rel="noopener"><img src="tiger.png" alt="Tiger"></a></span>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'rel=nofollow should be added to an image with a link when the nofollow option is set' do
para = block_from_string 'image:tiger.png[Tiger,link=http://en.wikipedia.org/wiki/Tiger,opts=nofollow]'
assert_equal %{<span class="image"><a class="image" href="http://en.wikipedia.org/wiki/Tiger" rel="nofollow"><img src="tiger.png" alt="Tiger"></a></span>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'a multi-line image macro with text and dimensions should be interpreted as an image with alt text and dimensions' do
para = block_from_string(%(image:tiger.png[Another\nAwesome\nTiger, 200,\n100]))
assert_equal %{<span class="image"><img src="tiger.png" alt="Another Awesome Tiger" width="200" height="100"></span>},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an inline image macro with a url target should be interpreted as an image' do
para = block_from_string %(Beware of the image:http://example.com/images/tiger.png[tiger].)
assert_equal %{Beware of the <span class="image"><img src="http://example.com/images/tiger.png" alt="tiger"></span>.},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an inline image macro with a float attribute should be interpreted as a floating image' do
para = block_from_string %(image:http://example.com/images/tiger.png[tiger, float="right"] Beware of the tigers!)
assert_equal %{<span class="image right"><img src="http://example.com/images/tiger.png" alt="tiger"></span> Beware of the tigers!},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should prepend value of imagesdir attribute to inline image target if target is relative path' do
para = block_from_string %(Beware of the image:tiger.png[tiger].), attributes: { 'imagesdir' => './images' }
assert_equal %{Beware of the <span class="image"><img src="./images/tiger.png" alt="tiger"></span>.},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should not prepend value of imagesdir attribute to inline image target if target is absolute path' do
para = block_from_string %(Beware of the image:/tiger.png[tiger].), attributes: { 'imagesdir' => './images' }
assert_equal %{Beware of the <span class="image"><img src="/tiger.png" alt="tiger"></span>.},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should not prepend value of imagesdir attribute to inline image target if target is url' do
para = block_from_string %(Beware of the image:http://example.com/images/tiger.png[tiger].), attributes: { 'imagesdir' => './images' }
assert_equal %{Beware of the <span class="image"><img src="http://example.com/images/tiger.png" alt="tiger"></span>.},
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should match an inline image macro if target contains a space character' do
para = block_from_string(%(Beware of the image:big cats.png[] around here.))
assert_equal %(Beware of the <span class="image"><img src="big%20cats.png" alt="big cats"></span> around here.),
para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'should not match an inline image macro if target contains a newline character' do
para = block_from_string(%(Fear not. There are no image:big\ncats.png[] around here.))
result = para.sub_macros(para.source)
refute_includes result, '<img '
assert_includes result, %(image:big\ncats.png[])
end
test 'should not match an inline image macro if target begins or ends with space character' do
['image: big cats.png[]', 'image:big cats.png []'].each do |input|
para = block_from_string %(Fear not. There are no #{input} around here.)
result = para.sub_macros(para.source)
refute_includes result, '<img '
assert_includes result, input
end
end
test 'should not detect a block image macro found inline' do
para = block_from_string(%(Not an inline image macro image::tiger.png[].))
result = para.sub_macros(para.source)
refute_includes result, '<img '
assert_includes result, 'image::tiger.png[]'
end
# NOTE this test verifies attributes get substituted eagerly in target of image in title
test 'should substitute attributes in target of inline image in section title' do
input = '== image:{iconsdir}/dot.gif[dot] Title'
using_memory_logger do |logger|
sect = block_from_string input, attributes: { 'data-uri' => '', 'iconsdir' => 'fixtures', 'docdir' => testdir }, safe: :server, catalog_assets: true
assert 1, sect.document.catalog[:images].size
assert_equal 'fixtures/dot.gif', sect.document.catalog[:images][0].to_s
assert_nil sect.document.catalog[:images][0].imagesdir
assert logger.empty?
end
end
test 'an icon macro should be interpreted as an icon if icons are enabled' do
para = block_from_string 'icon:github[]', attributes: { 'icons' => '' }
assert_equal %{<span class="icon"><img src="./images/icons/github.png" alt="github"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro should be interpreted as alt text if icons are disabled' do
para = block_from_string 'icon:github[]'
assert_equal %{<span class="icon">[github]</span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro should output alt text if icons are disabled and alt is given' do
para = block_from_string 'icon:github[alt="GitHub"]'
assert_equal %{<span class="icon">[GitHub]</span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro should be interpreted as a font-based icon when icons=font' do
para = block_from_string 'icon:github[]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fa fa-github"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro with a size should be interpreted as a font-based icon with a size when icons=font' do
para = block_from_string 'icon:github[4x]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fa fa-github fa-4x"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro with a role and title should be interpreted as a font-based icon with a class and title when icons=font' do
para = block_from_string 'icon:heart[role="red", title="Heart me"]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon red"><i class="fa fa-heart" title="Heart me"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'a single-line footnote macro should be registered and output as a footnote' do
para = block_from_string('Sentence text footnote:[An example footnote.].')
assert_equal %(Sentence text <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote = para.document.catalog[:footnotes].first
assert_equal 1, footnote.index
assert_nil footnote.id
assert_equal 'An example footnote.', footnote.text
end
test 'a multi-line footnote macro should be registered and output as a footnote without newline' do
para = block_from_string("Sentence text footnote:[An example footnote\nwith wrapped text.].")
assert_equal %(Sentence text <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote = para.document.catalog[:footnotes].first
assert_equal 1, footnote.index
assert_nil footnote.id
assert_equal "An example footnote with wrapped text.", footnote.text
end
test 'an escaped closing square bracket in a footnote should be unescaped when converted' do
para = block_from_string(%(footnote:[a #{BACKSLASH}] b].))
assert_equal %(<sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote = para.document.catalog[:footnotes].first
assert_equal "a ] b", footnote.text
end
test 'a footnote macro can be directly adjacent to preceding word' do
para = block_from_string('Sentence textfootnote:[An example footnote.].')
assert_equal %(Sentence text<sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>.), para.sub_macros(para.source)
end
test 'a footnote macro may contain an escaped backslash' do
para = block_from_string("footnote:[\\]]\nfootnote:[a \\] b]\nfootnote:[a \\]\\] b]")
para.sub_macros(para.source)
assert_equal 3, para.document.catalog[:footnotes].size
footnote1 = para.document.catalog[:footnotes][0]
assert_equal ']', footnote1.text
footnote2 = para.document.catalog[:footnotes][1]
assert_equal 'a ] b', footnote2.text
footnote3 = para.document.catalog[:footnotes][2]
assert_equal 'a ]] b', footnote3.text
end
test 'a footnote macro may contain a link macro' do
para = block_from_string('Share your code. footnote:[https://github.com[GitHub]]')
assert_equal %(Share your code. <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>), para.sub_macros(para.source)
assert_equal 1, para.document.catalog[:footnotes].size
footnote1 = para.document.catalog[:footnotes][0]
assert_equal '<a href="https://github.com">GitHub</a>', footnote1.text
end
test 'a footnote macro may contain a plain URL' do
para = block_from_string %(the JLine footnote:[https://github.com/jline/jline2]\nlibrary.)
result = para.sub_macros para.source
assert_equal %(the JLine <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>\nlibrary.), result
assert_equal 1, para.document.catalog[:footnotes].size
fn1 = para.document.catalog[:footnotes].first
assert_equal '<a href="https://github.com/jline/jline2" class="bare">https://github.com/jline/jline2</a>', fn1.text
end
test 'a footnote macro followed by a semi-colon may contain a plain URL' do
para = block_from_string %(the JLine footnote:[https://github.com/jline/jline2];\nlibrary.)
result = para.sub_macros para.source
assert_equal %(the JLine <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>;\nlibrary.), result
assert_equal 1, para.document.catalog[:footnotes].size
fn1 = para.document.catalog[:footnotes].first
assert_equal '<a href="https://github.com/jline/jline2" class="bare">https://github.com/jline/jline2</a>', fn1.text
end
test 'a footnote macro may contain a shorthand xref' do
# specialcharacters escaping is simulated
para = block_from_string('text footnote:[<<_install,install>>]')
doc = para.document
doc.register :refs, ['_install', (Asciidoctor::Inline.new doc, :anchor, 'Install', type: :ref, target: '_install'), 'Install']
catalog = doc.catalog
assert_equal %(text <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>), para.sub_macros(para.source)
assert_equal 1, catalog[:footnotes].size
footnote1 = catalog[:footnotes][0]
assert_equal '<a href="#_install">install</a>', footnote1.text
end
test 'a footnote macro may contain an xref macro' do
para = block_from_string('text footnote:[xref:_install[install]]')
doc = para.document
doc.register :refs, ['_install', (Asciidoctor::Inline.new doc, :anchor, 'Install', type: :ref, target: '_install'), 'Install']
catalog = doc.catalog
assert_equal %(text <sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup>), para.sub_macros(para.source)
assert_equal 1, catalog[:footnotes].size
footnote1 = catalog[:footnotes][0]
assert_equal '<a href="#_install">install</a>', footnote1.text
end