forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_test.rb
2225 lines (1846 loc) · 80.7 KB
/
document_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'
BUILT_IN_ELEMENTS = %w(admonition audio colist dlist document embedded example floating_title image inline_anchor inline_break inline_button inline_callout inline_footnote inline_image inline_indexterm inline_kbd inline_menu inline_quoted listing literal stem olist open page_break paragraph pass preamble quote section sidebar table thematic_break toc ulist verse video)
context 'Document' do
context 'Example document' do
test 'document title' do
doc = example_document :asciidoc_index
assert_equal 'AsciiDoc Home Page', doc.doctitle
assert_equal 'AsciiDoc Home Page', doc.name
refute_nil doc.header
assert_equal :section, doc.header.context
assert_equal 'header', doc.header.sectname
assert_equal 14, doc.blocks.size
assert_equal :preamble, doc.blocks[0].context
assert_equal :section, doc.blocks[1].context
# verify compat-mode is set when atx-style doctitle is used
result = doc.blocks[0].convert
assert_xpath '//em[text()="Stuart Rackham"]', result, 1
end
end
context 'Default settings' do
test 'safe mode level set to SECURE by default' do
doc = empty_document
assert_equal Asciidoctor::SafeMode::SECURE, doc.safe
end
test 'safe mode level set using string' do
doc = empty_document safe: 'server'
assert_equal Asciidoctor::SafeMode::SERVER, doc.safe
doc = empty_document safe: 'foo'
assert_equal Asciidoctor::SafeMode::SECURE, doc.safe
end
test 'safe mode level set using symbol' do
doc = empty_document safe: :server
assert_equal Asciidoctor::SafeMode::SERVER, doc.safe
doc = empty_document safe: :foo
assert_equal Asciidoctor::SafeMode::SECURE, doc.safe
end
test 'safe mode level set using integer' do
doc = empty_document safe: 10
assert_equal Asciidoctor::SafeMode::SERVER, doc.safe
doc = empty_document safe: 100
assert_equal 100, doc.safe
end
test 'safe mode attributes are set on document' do
doc = empty_document
assert_equal Asciidoctor::SafeMode::SECURE, doc.attr('safe-mode-level')
assert_equal 'secure', doc.attr('safe-mode-name')
assert doc.attr?('safe-mode-secure')
refute doc.attr?('safe-mode-unsafe')
refute doc.attr?('safe-mode-safe')
refute doc.attr?('safe-mode-server')
end
test 'safe mode level can be set in the constructor' do
doc = Asciidoctor::Document.new [], safe: Asciidoctor::SafeMode::SAFE
assert_equal Asciidoctor::SafeMode::SAFE, doc.safe
end
test 'safe model level cannot be modified' do
assert_raises NoMethodError, 'safe mode property of Asciidoctor::Document should not be writable!' do
empty_document.safe = Asciidoctor::SafeMode::UNSAFE
end
end
test 'toc and sectnums should be enabled by default in DocBook backend' do
doc = document_from_string 'content', backend: 'docbook', parse: true
assert doc.attr?('toc')
assert doc.attr?('sectnums')
result = doc.convert
assert_match '<?asciidoc-toc?>', result
assert_match '<?asciidoc-numbered?>', result
end
test 'maxdepth attribute should be set on asciidoc-toc and asciidoc-numbered processing instructions in DocBook backend' do
doc = document_from_string 'content', backend: 'docbook', parse: true, attributes: { 'toclevels' => '1', 'sectnumlevels' => '1' }
assert doc.attr?('toc')
assert doc.attr?('sectnums')
result = doc.convert
assert_match '<?asciidoc-toc maxdepth="1"?>', result
assert_match '<?asciidoc-numbered maxdepth="1"?>', result
end
test 'should be able to disable toc and sectnums in document header in DocBook backend' do
input = <<~'EOS'
= Document Title
:toc!:
:sectnums!:
EOS
doc = document_from_string input, backend: 'docbook'
refute doc.attr?('toc')
refute doc.attr?('sectnums')
end
test 'noheader attribute should suppress info element when converting to DocBook' do
input = <<~'EOS'
= Document Title
:noheader:
content
EOS
result = convert_string input, backend: 'docbook'
assert_xpath '/article', result, 1
assert_xpath '/article/info', result, 0
end
test 'should be able to disable section numbering using numbered attribute in document header in DocBook backend' do
input = <<~'EOS'
= Document Title
:numbered!:
EOS
doc = document_from_string input, backend: 'docbook'
refute doc.attr?('sectnums')
end
end
context 'Docinfo files' do
test 'should include docinfo files for html backend' do
sample_input_path = fixture_path 'basic.adoc'
cases = {
'docinfo' => { head_script: 1, meta: 0, top_link: 0, footer_script: 1, navbar: 1 },
'docinfo=private' => { head_script: 1, meta: 0, top_link: 0, footer_script: 1, navbar: 1 },
'docinfo1' => { head_script: 0, meta: 1, top_link: 1, footer_script: 0, navbar: 0 },
'docinfo=shared' => { head_script: 0, meta: 1, top_link: 1, footer_script: 0, navbar: 0 },
'docinfo2' => { head_script: 1, meta: 1, top_link: 1, footer_script: 1, navbar: 1 },
'docinfo docinfo2' => { head_script: 1, meta: 1, top_link: 1, footer_script: 1, navbar: 1 },
'docinfo=private,shared' => { head_script: 1, meta: 1, top_link: 1, footer_script: 1, navbar: 1 },
'docinfo=private-head' => { head_script: 1, meta: 0, top_link: 0, footer_script: 0, navbar: 0 },
'docinfo=private-header' => { head_script: 0, meta: 0, top_link: 0, footer_script: 0, navbar: 1 },
'docinfo=shared-head' => { head_script: 0, meta: 1, top_link: 0, footer_script: 0, navbar: 0 },
'docinfo=private-footer' => { head_script: 0, meta: 0, top_link: 0, footer_script: 1, navbar: 0 },
'docinfo=shared-footer' => { head_script: 0, meta: 0, top_link: 1, footer_script: 0, navbar: 0 },
'docinfo=private-head\ ,\ shared-footer' => { head_script: 1, meta: 0, top_link: 1, footer_script: 0, navbar: 0 },
}
cases.each do |attr_val, markup|
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: %(linkcss copycss! #{attr_val})
refute_empty output
assert_css 'script[src="modernizr.js"]', output, markup[:head_script]
assert_css 'meta[http-equiv="imagetoolbar"]', output, markup[:meta]
assert_css 'body > a#top', output, markup[:top_link]
assert_css 'body > script', output, markup[:footer_script]
assert_css 'body > nav.navbar', output, markup[:navbar]
assert_css 'body > nav.navbar + #header', output, markup[:navbar]
end
end
test 'should include docinfo header even if noheader attribute is set' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => 'private-header', 'noheader' => '' }
refute_empty output
assert_css 'body > nav.navbar', output, 1
assert_css 'body > nav.navbar + #content', output, 1
end
test 'should include docinfo footer even if nofooter attribute is set' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo1' => '', 'nofooter' => '' }
refute_empty output
assert_css 'body > a#top', output, 1
end
test 'should include user docinfo after built-in docinfo' do
sample_input_path = fixture_path 'basic.adoc'
attrs = { 'docinfo' => 'shared', 'source-highlighter' => 'highlight.js', 'linkcss' => '', 'copycss' => nil }
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: :safe, attributes: attrs
assert_css 'link[rel=stylesheet] + meta[http-equiv=imagetoolbar]', output, 1
assert_css 'meta[http-equiv=imagetoolbar] + *', output, 0
assert_css 'script + a#top', output, 1
assert_css 'a#top + *', output, 0
end
test 'should include docinfo files for html backend with custom docinfodir' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => '', 'docinfodir' => 'custom-docinfodir' }
refute_empty output
assert_css 'script[src="bootstrap.js"]', output, 1
assert_css 'meta[name="robots"]', output, 0
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo1' => '', 'docinfodir' => 'custom-docinfodir' }
refute_empty output
assert_css 'script[src="bootstrap.js"]', output, 0
assert_css 'meta[name="robots"]', output, 1
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo2' => '', 'docinfodir' => './custom-docinfodir' }
refute_empty output
assert_css 'script[src="bootstrap.js"]', output, 1
assert_css 'meta[name="robots"]', output, 1
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo2' => '', 'docinfodir' => 'custom-docinfodir/subfolder' }
refute_empty output
assert_css 'script[src="bootstrap.js"]', output, 0
assert_css 'meta[name="robots"]', output, 0
end
test 'should include docinfo files in docbook backend' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => '' }
refute_empty output
assert_css 'productname', output, 0
assert_css 'copyright', output, 1
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo1' => '' }
refute_empty output
assert_css 'productname', output, 1
assert_xpath '//xmlns:productname[text()="Asciidoctor™"]', output, 1
assert_css 'edition', output, 1
assert_xpath '//xmlns:edition[text()="1.0"]', output, 1 # verifies substitutions are performed
assert_css 'copyright', output, 0
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo2' => '' }
refute_empty output
assert_css 'productname', output, 1
assert_xpath '//xmlns:productname[text()="Asciidoctor™"]', output, 1
assert_css 'edition', output, 1
assert_xpath '//xmlns:edition[text()="1.0"]', output, 1 # verifies substitutions are performed
assert_css 'copyright', output, 1
end
test 'should use header docinfo in place of default header' do
output = Asciidoctor.convert_file fixture_path('sample.adoc'),
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => 'private-header', 'noheader' => '' }
refute_empty output
assert_css 'article > info', output, 1
assert_css 'article > info > title', output, 1
assert_css 'article > info > revhistory', output, 1
assert_css 'article > info > revhistory > revision', output, 2
end
test 'should include docinfo footer files for html backend' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => '' }
refute_empty output
assert_css 'body script', output, 1
assert_css 'a#top', output, 0
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo1' => '' }
refute_empty output
assert_css 'body script', output, 0
assert_css 'a#top', output, 1
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo2' => '' }
refute_empty output
assert_css 'body script', output, 1
assert_css 'a#top', output, 1
end
test 'should include docinfo footer files in DocBook backend' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => '' }
refute_empty output
assert_css 'article > revhistory', output, 1
assert_xpath '/xmlns:article/xmlns:revhistory/xmlns:revision/xmlns:revnumber[text()="1.0"]', output, 1 # verifies substitutions are performed
assert_css 'glossary', output, 0
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo1' => '' }
refute_empty output
assert_css 'article > revhistory', output, 0
assert_css 'glossary[xml|id="_glossary"]', output, 1
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo2' => '' }
refute_empty output
assert_css 'article > revhistory', output, 1
assert_xpath '/xmlns:article/xmlns:revhistory/xmlns:revision/xmlns:revnumber[text()="1.0"]', output, 1 # verifies substitutions are performed
assert_css 'glossary[xml|id="_glossary"]', output, 1
end
# WARNING this test manipulates runtime settings; should probably be run in forked process
test 'should force encoding of docinfo files to UTF-8' do
old_external = Encoding.default_external
old_internal = Encoding.default_internal
old_verbose = $VERBOSE
begin
$VERBOSE = nil # disable warnings since we have to modify constants
Encoding.default_external = Encoding.default_internal = Encoding::IBM437
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docinfo' => 'private,shared' }
refute_empty output
assert_css 'productname', output, 1
assert_includes output, '<productname>Asciidoctor™</productname>'
assert_css 'edition', output, 1
assert_xpath '//xmlns:edition[text()="1.0"]', output, 1 # verifies substitutions are performed
assert_css 'copyright', output, 1
ensure
Encoding.default_external = old_external
Encoding.default_internal = old_internal
$VERBOSE = old_verbose
end
end
test 'should not include docinfo files by default' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, safe: Asciidoctor::SafeMode::SERVER
refute_empty output
assert_css 'script[src="modernizr.js"]', output, 0
assert_css 'meta[http-equiv="imagetoolbar"]', output, 0
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', safe: Asciidoctor::SafeMode::SERVER
refute_empty output
assert_css 'productname', output, 0
assert_css 'copyright', output, 0
end
test 'should not include docinfo files if safe mode is SECURE or greater' do
sample_input_path = fixture_path 'basic.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, attributes: { 'docinfo2' => '' }
refute_empty output
assert_css 'script[src="modernizr.js"]', output, 0
assert_css 'meta[http-equiv="imagetoolbar"]', output, 0
output = Asciidoctor.convert_file sample_input_path,
to_file: false, standalone: true, backend: 'docbook', attributes: { 'docinfo2' => '' }
refute_empty output
assert_css 'productname', output, 0
assert_css 'copyright', output, 0
end
test 'should substitute attributes in docinfo files by default' do
sample_input_path = fixture_path 'subs.adoc'
using_memory_logger do |logger|
output = Asciidoctor.convert_file sample_input_path,
to_file: false,
standalone: true,
safe: :server,
attributes: { 'docinfo' => '', 'bootstrap-version' => nil, 'linkcss' => '', 'attribute-missing' => 'drop-line' }
refute_empty output
assert_css 'script', output, 0
assert_xpath %(//meta[@name="copyright"][@content="(C) OpenDevise"]), output, 1
assert_message logger, :INFO, 'dropping line containing reference to missing attribute: bootstrap-version'
end
end
test 'should apply explicit substitutions to docinfo files' do
sample_input_path = fixture_path 'subs.adoc'
output = Asciidoctor.convert_file sample_input_path,
to_file: false,
standalone: true,
safe: :server,
attributes: { 'docinfo' => '', 'docinfosubs' => 'attributes,replacements', 'linkcss' => '' }
refute_empty output
assert_css 'script[src="bootstrap.3.2.0.min.js"]', output, 1
assert_xpath %(//meta[@name="copyright"][@content="#{decode_char 169} OpenDevise"]), output, 1
end
end
context 'MathJax' do
test 'should add MathJax script to HTML head if stem attribute is set' do
output = convert_string '', attributes: { 'stem' => '' }
assert_match '<script type="text/x-mathjax-config">', output
assert_match 'inlineMath: [["\\\\(", "\\\\)"]]', output
assert_match 'displayMath: [["\\\\[", "\\\\]"]]', output
assert_match 'delimiters: [["\\\\$", "\\\\$"]]', output
end
end
context 'Converter' do
test 'convert methods on built-in converter are registered by default' do
doc = document_from_string ''
assert_equal 'html5', doc.attributes['backend']
assert doc.attributes.key? 'backend-html5'
assert_equal 'html', doc.attributes['basebackend']
assert doc.attributes.key? 'basebackend-html'
converter = doc.converter
assert_kind_of Asciidoctor::Converter::Html5Converter, converter
BUILT_IN_ELEMENTS.each do |element|
assert_respond_to converter, %(convert_#{element})
end
end
test 'convert methods on built-in converter are registered when backend is docbook5' do
doc = document_from_string '', attributes: { 'backend' => 'docbook5' }
converter = doc.converter
assert_equal 'docbook5', doc.attributes['backend']
assert doc.attributes.key? 'backend-docbook5'
assert_equal 'docbook', doc.attributes['basebackend']
assert doc.attributes.key? 'basebackend-docbook'
converter = doc.converter
assert_kind_of Asciidoctor::Converter::DocBook5Converter, converter
BUILT_IN_ELEMENTS.each do |element|
assert_respond_to converter, %(convert_#{element})
end
end
test 'should add favicon if favicon attribute is set' do
{
'' => %w(favicon.ico image/x-icon),
'/favicon.ico' => %w(/favicon.ico image/x-icon),
'/img/favicon.png' => %w(/img/favicon.png image/png),
}.each do |val, (href, type)|
result = convert_string '= Untitled', attributes: { 'favicon' => val }
assert_css 'link[rel="icon"]', result, 1
assert_css %(link[rel="icon"][href="#{href}"]), result, 1
assert_css %(link[rel="icon"][type="#{type}"]), result, 1
end
end
end
context 'Structure' do
test 'document with no doctitle' do
doc = document_from_string 'Snorf'
assert_nil doc.doctitle
assert_nil doc.name
refute doc.has_header?
assert_nil doc.header
end
test 'should enable compat mode for document with legacy doctitle' do
input = <<~'EOS'
Document Title
==============
+content+
EOS
doc = document_from_string input
assert doc.attr? 'compat-mode'
result = doc.convert
assert_xpath '//code[text()="content"]', result, 1
end
test 'should not enable compat mode for document with legacy doctitle if compat mode disable by header' do
input = <<~'EOS'
Document Title
==============
:compat-mode!:
+content+
EOS
doc = document_from_string input
assert_nil doc.attr 'compat-mode'
result = doc.convert
assert_xpath '//code[text()="content"]', result, 0
end
test 'should not enable compat mode for document with legacy doctitle if compat mode is locked by API' do
input = <<~'EOS'
Document Title
==============
+content+
EOS
doc = document_from_string input, attributes: { 'compat-mode' => nil }
assert doc.attribute_locked? 'compat-mode'
assert_nil doc.attr 'compat-mode'
result = doc.convert
assert_xpath '//code[text()="content"]', result, 0
end
test 'should apply max-width to each top-level container' do
input = <<~'EOS'
= Document Title
contentfootnote:[placeholder]
EOS
output = convert_string input, attributes: { 'max-width' => '70em' }
assert_css 'body[style]', output, 0
assert_css '#header[style="max-width: 70em;"]', output, 1
assert_css '#content[style="max-width: 70em;"]', output, 1
assert_css '#footnotes[style="max-width: 70em;"]', output, 1
assert_css '#footer[style="max-width: 70em;"]', output, 1
end
test 'title partition API with default separator' do
title = Asciidoctor::Document::Title.new 'Main Title: And More: Subtitle'
assert_equal 'Main Title: And More', title.main
assert_equal 'Subtitle', title.subtitle
end
test 'title partition API with custom separator' do
title = Asciidoctor::Document::Title.new 'Main Title:: And More:: Subtitle', separator: '::'
assert_equal 'Main Title:: And More', title.main
assert_equal 'Subtitle', title.subtitle
end
test 'document with subtitle' do
input = <<~'EOS'
= Main Title: *Subtitle*
Author Name
content
EOS
doc = document_from_string input
title = doc.doctitle partition: true, sanitize: true
assert title.subtitle?
assert title.sanitized?
assert_equal 'Main Title', title.main
assert_equal 'Subtitle', title.subtitle
end
test 'document with subtitle and custom separator' do
input = <<~'EOS'
[separator=::]
= Main Title:: *Subtitle*
Author Name
content
EOS
doc = document_from_string input
title = doc.doctitle partition: true, sanitize: true
assert title.subtitle?
assert title.sanitized?
assert_equal 'Main Title', title.main
assert_equal 'Subtitle', title.subtitle
end
test 'should not honor custom separator for doctitle if attribute is locked by API' do
input = <<~'EOS'
[separator=::]
= Main Title - *Subtitle*
Author Name
content
EOS
doc = document_from_string input, attributes: { 'title-separator' => ' -' }
title = doc.doctitle partition: true, sanitize: true
assert title.subtitle?
assert title.sanitized?
assert_equal 'Main Title', title.main
assert_equal 'Subtitle', title.subtitle
end
test 'document with doctitle defined as attribute entry' do
input = <<~'EOS'
:doctitle: Document Title
preamble
== First Section
EOS
doc = document_from_string input
assert_equal 'Document Title', doc.doctitle
assert doc.has_header?
assert_equal 'Document Title', doc.header.title
assert_equal 'Document Title', doc.first_section.title
end
test 'document with doctitle defined as attribute entry followed by block with title' do
input = <<~'EOS'
:doctitle: Document Title
.Block title
Block content
EOS
doc = document_from_string input
assert_equal 'Document Title', doc.doctitle
assert doc.has_header?
assert_equal 1, doc.blocks.size
assert_equal :paragraph, doc.blocks[0].context
assert_equal 'Block title', doc.blocks[0].title
end
test 'document with title attribute entry overrides doctitle' do
input = <<~'EOS'
= Document Title
:title: Override
{doctitle}
== First Section
EOS
doc = document_from_string input
assert_equal 'Override', doc.doctitle
assert_equal 'Override', doc.title
assert doc.has_header?
assert_equal 'Document Title', doc.header.title
assert_equal 'Document Title', doc.first_section.title
assert_xpath '//*[@id="preamble"]//p[text()="Document Title"]', doc.convert, 1
end
test 'document with blank title attribute entry overrides doctitle' do
input = <<~'EOS'
= Document Title
:title:
{doctitle}
== First Section
EOS
doc = document_from_string input
assert_equal '', doc.doctitle
assert_equal '', doc.title
assert doc.has_header?
assert_equal 'Document Title', doc.header.title
assert_equal 'Document Title', doc.first_section.title
assert_xpath '//*[@id="preamble"]//p[text()="Document Title"]', doc.convert, 1
end
test 'document header can reference intrinsic doctitle attribute' do
input = <<~'EOS'
= ACME Documentation
:intro: Welcome to the {doctitle}!
{intro}
EOS
doc = document_from_string input
assert_equal 'Welcome to the ACME Documentation!', (doc.attr 'intro')
assert_xpath '//p[text()="Welcome to the ACME Documentation!"]', doc.convert, 1
end
test 'document with title attribute entry overrides doctitle attribute entry' do
input = <<~'EOS'
= Document Title
:snapshot: {doctitle}
:doctitle: doctitle
:title: Override
{snapshot}, {doctitle}
== First Section
EOS
doc = document_from_string input
assert_equal 'Override', doc.doctitle
assert_equal 'Override', doc.title
assert doc.has_header?
assert_equal 'doctitle', doc.header.title
assert_equal 'doctitle', doc.first_section.title
assert_xpath '//*[@id="preamble"]//p[text()="Document Title, doctitle"]', doc.convert, 1
end
test 'document with doctitle attribute entry overrides implicit doctitle' do
input = <<~'EOS'
= Document Title
:snapshot: {doctitle}
:doctitle: Override
{snapshot}, {doctitle}
== First Section
EOS
doc = document_from_string input
assert_equal 'Override', doc.doctitle
assert_nil doc.attributes['title']
assert doc.has_header?
assert_equal 'Override', doc.header.title
assert_equal 'Override', doc.first_section.title
assert_xpath '//*[@id="preamble"]//p[text()="Document Title, Override"]', doc.convert, 1
end
test 'doctitle attribute entry above header overrides implicit doctitle' do
input = <<~'EOS'
:doctitle: Override
= Document Title
{doctitle}
== First Section
EOS
doc = document_from_string input
assert_equal 'Override', doc.doctitle
assert_nil doc.attributes['title']
assert doc.has_header?
assert_equal 'Override', doc.header.title
assert_equal 'Override', doc.first_section.title
assert_xpath '//*[@id="preamble"]//p[text()="Override"]', doc.convert, 1
end
test 'should apply header substitutions to value of the doctitle attribute assigned from implicit doctitle' do
input = <<~'EOS'
= <Foo> {plus} <Bar>
The name of the game is {doctitle}.
EOS
doc = document_from_string input
assert_equal '<Foo> + <Bar>', (doc.attr 'doctitle')
assert_includes doc.blocks[0].content, '<Foo> + <Bar>'
end
test 'should substitute attribute reference in implicit document title for attribute defined earlier in header' do
using_memory_logger do |logger|
input = <<~'EOS'
:project-name: ACME
= {project-name} Docs
{doctitle}
EOS
doc = document_from_string input, attributes: { 'attribute-missing' => 'warn' }
assert_empty logger
assert_equal 'ACME Docs', (doc.attr 'doctitle')
assert_equal 'ACME Docs', doc.doctitle
assert_xpath '//p[text()="ACME Docs"]', doc.convert, 1
end
end
test 'should not warn if implicit document title contains attribute reference for attribute defined later in header' do
using_memory_logger do |logger|
input = <<~'EOS'
= {project-name} Docs
:project-name: ACME
{doctitle}
EOS
doc = document_from_string input, attributes: { 'attribute-missing' => 'warn' }
assert_empty logger
assert_equal '{project-name} Docs', (doc.attr 'doctitle')
assert_equal 'ACME Docs', doc.doctitle
assert_xpath '//p[text()="{project-name} Docs"]', doc.convert, 1
end
end
test 'should recognize document title when preceded by blank lines' do
input = <<~'EOS'
= Title
preamble
== Section 1
text
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css '#header h1', output, 1
assert_css '#content h1', output, 0
end
test 'should recognize document title when preceded by blank lines introduced by a preprocessor conditional' do
input = <<~'EOS'
ifdef::sectids[]
:foo: bar
endif::[]
= Title
preamble
== Section 1
text
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css '#header h1', output, 1
assert_css '#content h1', output, 0
end
test 'should recognize document title when preceded by blank lines after an attribute entry' do
input = <<~'EOS'
:doctype: book
= Title
preamble
== Section 1
text
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE
assert_css '#header h1', output, 1
assert_css '#content h1', output, 0
end
test 'should recognize document title in include file when preceded by blank lines' do
input = <<~'EOS'
include::fixtures/include-with-leading-blank-line.adoc[]
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_xpath '//h1[text()="Document Title"]', output, 1
assert_css '#toc', output, 1
end
test 'should include specified lines even when leading lines are skipped' do
input = <<~'EOS'
include::fixtures/include-with-leading-blank-line.adoc[lines=6]
EOS
output = convert_string input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => testdir }
assert_xpath '//h2[text()="Section"]', output, 1
end
test 'document with multiline attribute entry but only one line should not crash' do
input = ':foo: bar' + Asciidoctor::LINE_CONTINUATION
doc = document_from_string input
assert_equal 'bar', doc.attributes['foo']
end
test 'should sanitize contents of HTML title element' do
input = <<~'EOS'
= *Document* image:logo.png[] _Title_ image:another-logo.png[another logo]
content
EOS
output = convert_string input
assert_xpath '/html/head/title[text()="Document Title"]', output, 1
nodes = xmlnodes_at_xpath '//*[@id="header"]/h1', output
assert_equal 1, nodes.size
assert_match '<h1><strong>Document</strong> <span class="image"><img src="logo.png" alt="logo"></span> <em>Title</em> <span class="image"><img src="another-logo.png" alt="another logo"></span></h1>', output
end
test 'should not choke on empty source' do
doc = Asciidoctor::Document.new ''
assert_empty doc.blocks
assert_nil doc.doctitle
refute doc.has_header?
assert_nil doc.header
end
test 'should not choke on nil source' do
doc = Asciidoctor::Document.new nil
assert_empty doc.blocks
assert_nil doc.doctitle
refute doc.has_header?
assert_nil doc.header
end
test 'with metadata' do
input = <<~'EOS'
= AsciiDoc
Stuart Rackham <[email protected]>
v8.6.8, 2012-07-12: See changelog.
:description: AsciiDoc user guide
:keywords: asciidoc,documentation
:copyright: Stuart Rackham
== Version 8.6.8
more info...
EOS
output = convert_string input
assert_xpath '//meta[@name="author"][@content="Stuart Rackham"]', output, 1
assert_xpath '//meta[@name="description"][@content="AsciiDoc user guide"]', output, 1
assert_xpath '//meta[@name="keywords"][@content="asciidoc,documentation"]', output, 1
assert_xpath '//meta[@name="copyright"][@content="Stuart Rackham"]', output, 1
assert_xpath '//*[@id="header"]/*[@class="details"]/span[@id="author"][text()="Stuart Rackham"]', output, 1
assert_xpath '//*[@id="header"]/*[@class="details"]/span[@id="email"]/a[@href="mailto:[email protected]"][text()="[email protected]"]', output, 1
assert_xpath '//*[@id="header"]/*[@class="details"]/span[@id="revnumber"][text()="version 8.6.8,"]', output, 1
assert_xpath '//*[@id="header"]/*[@class="details"]/span[@id="revdate"][text()="2012-07-12"]', output, 1
assert_xpath '//*[@id="header"]/*[@class="details"]/span[@id="revremark"][text()="See changelog."]', output, 1
end
test 'should parse revision line if date is empty' do
input = <<~'EOS'
= Document Title
Author Name
v1.0.0,:remark
content
EOS
doc = document_from_string input
assert_equal '1.0.0', doc.attributes['revnumber']
assert_nil doc.attributes['revdate']
assert_equal 'remark', doc.attributes['revremark']
end
test 'should include revision history in DocBook output if revdate and revnumber is set' do
input = <<~'EOS'
= Document Title
Author Name
:revdate: 2011-11-11
:revnumber: 1.0
content
EOS
output = convert_string input, backend: 'docbook'
assert_css 'revhistory', output, 1
assert_css 'revhistory > revision', output, 1
assert_css 'revhistory > revision > date', output, 1
assert_css 'revhistory > revision > revnumber', output, 1
end
test 'should include revision history in DocBook output if revdate and revremark is set' do
input = <<~'EOS'
= Document Title
Author Name
:revdate: 2011-11-11
:revremark: features!
content
EOS
output = convert_string input, backend: 'docbook'
assert_css 'revhistory', output, 1
assert_css 'revhistory > revision', output, 1
assert_css 'revhistory > revision > date', output, 1
assert_css 'revhistory > revision > revremark', output, 1
end
test 'should not include revision history in DocBook output if revdate is not set' do
input = <<~'EOS'
= Document Title
Author Name
:revnumber: 1.0
content
EOS
output = convert_string input, backend: 'docbook'
assert_css 'revhistory', output, 0
end
test 'with metadata to DocBook 5' do
input = <<~'EOS'
= AsciiDoc
Stuart Rackham <[email protected]>
== Version 8.6.8
more info...
EOS
output = convert_string input, backend: 'docbook5'
assert_xpath '/article/info', output, 1
assert_xpath '/article/info/title[text()="AsciiDoc"]', output, 1
assert_xpath '/article/info/author/personname', output, 1
assert_xpath '/article/info/author/personname/firstname[text()="Stuart"]', output, 1
assert_xpath '/article/info/author/personname/surname[text()="Rackham"]', output, 1
assert_xpath '/article/info/author/email[text()="[email protected]"]', output, 1
assert_css 'article:root:not([xml|id])', output, 1
assert_css 'article:root[xml|lang="en"]', output, 1
end
test 'with document ID to Docbook 5' do
input = <<~'EOS'
[[document-id]]
= Document Title
more info...
EOS
output = convert_string input, backend: 'docbook', keep_namespaces: true
assert_css 'article:root[xml|id="document-id"]', output, 1
end
test 'with author defined using attribute entry to DocBook' do
input = <<~'EOS'
= Document Title
:author: Doc Writer
:email: [email protected]
content
EOS
output = convert_string input, backend: 'docbook'
assert_xpath '/article/info/author', output, 1
assert_xpath '/article/info/author/personname/firstname[text()="Doc"]', output, 1
assert_xpath '/article/info/author/personname/surname[text()="Writer"]', output, 1
assert_xpath '/article/info/author/email[text()="[email protected]"]', output, 1
assert_xpath '/article/info/authorinitials[text()="DW"]', output, 1
end
test 'should substitute replacements in author names in HTML output' do
input = <<~'EOS'
= Document Title
Stephen O'Grady <[email protected]>
content
EOS
output = convert_string input
assert_xpath %(//meta[@name="author"][@content="Stephen O#{decode_char 8217}Grady"]), output, 1
assert_xpath %(//span[@id="author"][text()="Stephen O#{decode_char 8217}Grady"]), output, 1
end
test 'should substitute replacements in author names in DocBook output' do