forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions_test.rb
2229 lines (1984 loc) · 70.3 KB
/
extensions_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'
class ExtensionsInitTest < Minitest::Test
def test_autoload
doc = empty_document
refute doc.extensions?, 'Extensions should not be enabled by default'
Asciidoctor::Extensions.register do
# trigger extensions to autoload by registering empty group
end
doc = empty_document
assert doc.extensions?, 'Extensions should be enabled after being autoloaded'
self.class.remove_tests self.class
ensure
Asciidoctor::Extensions.unregister_all
end
self
end.new(nil).test_autoload
class SamplePreprocessor < Asciidoctor::Extensions::Preprocessor
def process doc, reader
nil
end
end
class SampleIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
end
class SampleDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor
end
# NOTE intentionally using the deprecated name
class SampleTreeprocessor < Asciidoctor::Extensions::Treeprocessor
def process document
nil
end
end
SampleTreeProcessor = SampleTreeprocessor
class SamplePostprocessor < Asciidoctor::Extensions::Postprocessor
end
class SampleBlock < Asciidoctor::Extensions::BlockProcessor
end
class SampleBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
end
class SampleInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
end
class ScrubHeaderPreprocessor < Asciidoctor::Extensions::Preprocessor
def process doc, reader
lines = reader.lines
skipped = []
while !lines.empty? && !lines.first.start_with?('=')
skipped << lines.shift
reader.advance
end
doc.set_attr 'skipped', (skipped * "\n")
reader
end
end
class BoilerplateTextIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
def handles? target
target.end_with? '.txt'
end
def process document, reader, target, attributes
case target
when 'lorem-ipsum.txt'
content = ["Lorem ipsum dolor sit amet...\n"]
reader.push_include content, target, target, 1, attributes
end
end
end
class ReplaceAuthorTreeProcessor < Asciidoctor::Extensions::TreeProcessor
def process document
document.attributes['firstname'] = 'Ghost'
document.attributes['author'] = 'Ghost Writer'
document
end
end
class ReplaceTreeTreeProcessor < Asciidoctor::Extensions::TreeProcessor
def process document
if document.doctitle == 'Original Document'
Asciidoctor.load %(== Replacement Document\nReplacement Author\n\ncontent)
else
document
end
end
end
class SelfSigningTreeProcessor < Asciidoctor::Extensions::TreeProcessor
def process document
document << (create_paragraph document, self.class.name, {})
nil
end
end
class StripAttributesPostprocessor < Asciidoctor::Extensions::Postprocessor
def process document, output
output.gsub %r/<(\w+).*?>/m, '<\\1>'
end
end
class UppercaseBlock < Asciidoctor::Extensions::BlockProcessor
use_dsl
named :yell
on_context :paragraph
name_positional_attributes 'chars'
parse_content_as :simple
def process parent, reader, attributes
if (chars = attributes['chars'])
upcase_chars = chars.upcase
create_paragraph parent, reader.lines.map {|l| l.downcase.tr chars, upcase_chars }, attributes
else
create_paragraph parent, reader.lines.map(&:upcase), attributes
end
end
end
class SnippetMacro < Asciidoctor::Extensions::BlockMacroProcessor
def process parent, target, attributes
create_pass_block parent, %(<script src="http://example.com/#{target}.js?_mode=#{attributes['mode']}"></script>), {}, content_model: :raw
end
end
class LegacyPosAttrsBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
option :pos_attrs, %w(target format)
def process parent, _, attrs
create_image_block parent, { 'target' => %(#{attrs['target']}.#{attrs['format']}) }
end
end
class TemperatureMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :degrees
resolve_attributes '1:units', 'precision=1'
def process parent, target, attributes
units = attributes['units'] || (parent.document.attr 'temperature-unit', 'C')
precision = attributes['precision'].to_i
c = target.to_f
case units
when 'C'
create_inline parent, :quoted, %(#{c.round precision} °C), type: :unquoted
when 'F'
create_inline parent, :quoted, %(#{(c * 1.8 + 32).round precision} °F), type: :unquoted
else
raise ArgumentError, %(Unknown temperature units: #{units})
end
end
end
class MetaRobotsDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor
def process document
'<meta name="robots" content="index,follow">'
end
end
class MetaAppDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor
use_dsl
at_location :head
def process document
'<meta name="application-name" content="Asciidoctor App">'
end
end
class SampleExtensionGroup < Asciidoctor::Extensions::Group
def activate registry
registry.document.attributes['activate-method-called'] = ''
registry.preprocessor SamplePreprocessor
end
end
def create_cat_in_sink_block_macro
Asciidoctor::Extensions.create do
block_macro do
named :cat_in_sink
process do |parent, target, attrs|
image_attrs = {}
unless target.nil_or_empty?
image_attrs['target'] = %(cat-in-sink-day-#{target}.png)
end
if (title = attrs.delete 'title')
image_attrs['title'] = title
end
if (alt = attrs.delete 1)
image_attrs['alt'] = alt
end
create_image_block parent, image_attrs
end
end
end
end
def create_santa_list_block_macro
Asciidoctor::Extensions.create do
block_macro do
named :santa_list
process do |parent, target|
list = create_list parent, target
guillaume = (create_list_item list, 'Guillaume')
guillaume.add_role 'friendly'
guillaume.id = 'santa-list-guillaume'
list << guillaume
robert = (create_list_item list, 'Robert')
robert.add_role 'kind'
robert.add_role 'contributor'
robert.add_role 'java'
list << robert
pepijn = (create_list_item list, 'Pepijn')
pepijn.id = 'santa-list-pepijn'
list << pepijn
dan = (create_list_item list, 'Dan')
dan.add_role 'naughty'
dan.id = 'santa-list-dan'
list << dan
sarah = (create_list_item list, 'Sarah')
list << sarah
list
end
end
end
end
context 'Extensions' do
context 'Register' do
test 'should not activate registry if no extension groups are registered' do
assert defined? Asciidoctor::Extensions
doc = empty_document
refute doc.extensions?, 'Extensions should not be enabled if not groups are registered'
end
test 'should register extension group class' do
begin
Asciidoctor::Extensions.register :sample, SampleExtensionGroup
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
assert_equal SampleExtensionGroup, Asciidoctor::Extensions.groups[:sample]
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should self register extension group class' do
begin
SampleExtensionGroup.register :sample
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
assert_equal SampleExtensionGroup, Asciidoctor::Extensions.groups[:sample]
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should register extension group from class name' do
begin
Asciidoctor::Extensions.register :sample, 'SampleExtensionGroup'
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
assert_equal SampleExtensionGroup, Asciidoctor::Extensions.groups[:sample]
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should register extension group from instance' do
begin
Asciidoctor::Extensions.register :sample, SampleExtensionGroup.new
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
assert_kind_of SampleExtensionGroup, Asciidoctor::Extensions.groups[:sample]
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should register extension block' do
begin
Asciidoctor::Extensions.register :sample do
# this space intentionally left blank
end
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
assert_kind_of Proc, Asciidoctor::Extensions.groups[:sample]
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should coerce group name to symbol when registering' do
begin
Asciidoctor::Extensions.register 'sample', SampleExtensionGroup
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
assert_equal SampleExtensionGroup, Asciidoctor::Extensions.groups[:sample]
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should unregister extension group by symbol name' do
begin
Asciidoctor::Extensions.register :sample, SampleExtensionGroup
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
Asciidoctor::Extensions.unregister :sample
assert_equal 0, Asciidoctor::Extensions.groups.size
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should unregister extension group by string name' do
begin
Asciidoctor::Extensions.register :sample, SampleExtensionGroup
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
Asciidoctor::Extensions.unregister 'sample'
assert_equal 0, Asciidoctor::Extensions.groups.size
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should unregister multiple extension groups by name' do
begin
Asciidoctor::Extensions.register :sample1, SampleExtensionGroup
Asciidoctor::Extensions.register :sample2, SampleExtensionGroup
refute_nil Asciidoctor::Extensions.groups
assert_equal 2, Asciidoctor::Extensions.groups.size
Asciidoctor::Extensions.unregister :sample1, :sample2
assert_equal 0, Asciidoctor::Extensions.groups.size
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should not fail to unregister extension group if not registered' do
refute_nil Asciidoctor::Extensions.groups
assert_equal 0, Asciidoctor::Extensions.groups.size
Asciidoctor::Extensions.unregister :sample
assert_equal 0, Asciidoctor::Extensions.groups.size
end
test 'should not fail to unregister extension group if extension groups are not initialized' do
Asciidoctor::Extensions.remove_instance_variable :@groups
Asciidoctor::Extensions.unregister :sample
assert_equal 0, Asciidoctor::Extensions.groups.size
end
test 'should raise NameError if extension class cannot be resolved from string' do
begin
Asciidoctor::Extensions.register do
block 'foobar'
end
ex = assert_raises NameError do
empty_document
end
assert_match %r/^Could not resolve class for name: foobar$/, ex.message
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should allow standalone registry to be created but not registered' do
registry = Asciidoctor::Extensions.create 'sample' do
block do
named :whisper
on_context :paragraph
parse_content_as :simple
def process parent, reader, attributes
create_paragraph parent, reader.lines.map(&:downcase), attributes
end
end
end
assert_instance_of Asciidoctor::Extensions::Registry, registry
refute_nil registry.groups
assert_equal 1, registry.groups.size
assert_equal 'sample', registry.groups.keys.first
assert_equal 0, Asciidoctor::Extensions.groups.size
end
end
context 'Activate' do
test 'should call activate on extension group class' do
begin
doc = Asciidoctor::Document.new
Asciidoctor::Extensions.register :sample, SampleExtensionGroup
registry = Asciidoctor::Extensions::Registry.new
registry.activate doc
assert doc.attr? 'activate-method-called'
assert registry.preprocessors?
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should reset registry if activate is called again' do
begin
Asciidoctor::Extensions.register :sample, SampleExtensionGroup
doc = Asciidoctor::Document.new
registry = Asciidoctor::Extensions::Registry.new
registry.activate doc
assert doc.attr? 'activate-method-called'
assert registry.preprocessors?
assert_equal 1, registry.preprocessors.size
assert_same doc, registry.document
doc = Asciidoctor::Document.new
registry.activate doc
assert doc.attr? 'activate-method-called'
assert registry.preprocessors?
assert_equal 1, registry.preprocessors.size
assert_same doc, registry.document
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke extension block' do
begin
doc = Asciidoctor::Document.new
Asciidoctor::Extensions.register do
@document.attributes['block-called'] = ''
preprocessor SamplePreprocessor
end
registry = Asciidoctor::Extensions::Registry.new
registry.activate doc
assert doc.attr? 'block-called'
assert registry.preprocessors?
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should create registry in Document if extensions are loaded' do
begin
SampleExtensionGroup.register
doc = Asciidoctor::Document.new
assert doc.extensions?
assert_kind_of Asciidoctor::Extensions::Registry, doc.extensions
ensure
Asciidoctor::Extensions.unregister_all
end
end
end
context 'Instantiate' do
test 'should instantiate preprocessors' do
registry = Asciidoctor::Extensions::Registry.new
registry.preprocessor SamplePreprocessor
registry.activate Asciidoctor::Document.new
assert registry.preprocessors?
extensions = registry.preprocessors
assert_equal 1, extensions.size
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extensions.first
assert_kind_of SamplePreprocessor, extensions.first.instance
assert_kind_of Method, extensions.first.process_method
end
test 'should instantiate include processors' do
registry = Asciidoctor::Extensions::Registry.new
registry.include_processor SampleIncludeProcessor
registry.activate Asciidoctor::Document.new
assert registry.include_processors?
extensions = registry.include_processors
assert_equal 1, extensions.size
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extensions.first
assert_kind_of SampleIncludeProcessor, extensions.first.instance
assert_kind_of Method, extensions.first.process_method
end
test 'should instantiate docinfo processors' do
registry = Asciidoctor::Extensions::Registry.new
registry.docinfo_processor SampleDocinfoProcessor
registry.activate Asciidoctor::Document.new
assert registry.docinfo_processors?
assert registry.docinfo_processors?(:head)
extensions = registry.docinfo_processors
assert_equal 1, extensions.size
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extensions.first
assert_kind_of SampleDocinfoProcessor, extensions.first.instance
assert_kind_of Method, extensions.first.process_method
end
# NOTE intentionally using the legacy names
test 'should instantiate tree processors' do
registry = Asciidoctor::Extensions::Registry.new
registry.treeprocessor SampleTreeprocessor
registry.activate Asciidoctor::Document.new
assert registry.treeprocessors?
extensions = registry.treeprocessors
assert_equal 1, extensions.size
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extensions.first
assert_kind_of SampleTreeprocessor, extensions.first.instance
assert_kind_of Method, extensions.first.process_method
end
test 'should instantiate postprocessors' do
registry = Asciidoctor::Extensions::Registry.new
registry.postprocessor SamplePostprocessor
registry.activate Asciidoctor::Document.new
assert registry.postprocessors?
extensions = registry.postprocessors
assert_equal 1, extensions.size
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extensions.first
assert_kind_of SamplePostprocessor, extensions.first.instance
assert_kind_of Method, extensions.first.process_method
end
test 'should instantiate block processor' do
registry = Asciidoctor::Extensions::Registry.new
registry.block SampleBlock, :sample
registry.activate Asciidoctor::Document.new
assert registry.blocks?
assert registry.registered_for_block? :sample, :paragraph
extension = registry.find_block_extension :sample
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extension
assert_kind_of SampleBlock, extension.instance
assert_kind_of Method, extension.process_method
end
test 'should not match block processor for unsupported context' do
registry = Asciidoctor::Extensions::Registry.new
registry.block SampleBlock, :sample
registry.activate Asciidoctor::Document.new
refute registry.registered_for_block? :sample, :sidebar
end
test 'should instantiate block macro processor' do
registry = Asciidoctor::Extensions::Registry.new
registry.block_macro SampleBlockMacro, 'sample'
registry.activate Asciidoctor::Document.new
assert registry.block_macros?
assert registry.registered_for_block_macro? 'sample'
extension = registry.find_block_macro_extension 'sample'
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extension
assert_kind_of SampleBlockMacro, extension.instance
assert_kind_of Method, extension.process_method
end
test 'should instantiate inline macro processor' do
registry = Asciidoctor::Extensions::Registry.new
registry.inline_macro SampleInlineMacro, 'sample'
registry.activate Asciidoctor::Document.new
assert registry.inline_macros?
assert registry.registered_for_inline_macro? 'sample'
extension = registry.find_inline_macro_extension 'sample'
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extension
assert_kind_of SampleInlineMacro, extension.instance
assert_kind_of Method, extension.process_method
end
test 'should allow processors to be registered by a string name' do
registry = Asciidoctor::Extensions::Registry.new
registry.preprocessor 'SamplePreprocessor'
registry.activate Asciidoctor::Document.new
assert registry.preprocessors?
extensions = registry.preprocessors
assert_equal 1, extensions.size
assert_kind_of Asciidoctor::Extensions::ProcessorExtension, extensions.first
end
end
context 'Integration' do
test 'can provide extension registry as an option' do
registry = Asciidoctor::Extensions.create do
tree_processor SampleTreeProcessor
end
doc = document_from_string %(= Document Title\n\ncontent), extension_registry: registry
refute_nil doc.extensions
assert_equal 1, doc.extensions.groups.size
assert doc.extensions.tree_processors?
assert_equal 1, doc.extensions.tree_processors.size
assert_equal 0, Asciidoctor::Extensions.groups.size
end
# NOTE I'm not convinced we want to continue to support this use case
test 'can provide extension registry created without any groups as option' do
registry = Asciidoctor::Extensions.create
registry.tree_processor SampleTreeProcessor
doc = document_from_string %(= Document Title\n\ncontent), extension_registry: registry
refute_nil doc.extensions
assert_equal 0, doc.extensions.groups.size
assert doc.extensions.tree_processors?
assert_equal 1, doc.extensions.tree_processors.size
assert_equal 0, Asciidoctor::Extensions.groups.size
end
test 'can provide extensions proc as option' do
doc = document_from_string %(= Document Title\n\ncontent), extensions: proc { tree_processor SampleTreeProcessor }
refute_nil doc.extensions
assert_equal 1, doc.extensions.groups.size
assert doc.extensions.tree_processors?
assert_equal 1, doc.extensions.tree_processors.size
assert_equal 0, Asciidoctor::Extensions.groups.size
end
test 'should not activate global registry if :extensions option is false' do
begin
Asciidoctor::Extensions.register :sample do
# this space intentionally left blank
end
refute_nil Asciidoctor::Extensions.groups
assert_equal 1, Asciidoctor::Extensions.groups.size
doc = empty_document extensions: false
refute doc.extensions?, 'Extensions should not be enabled if :extensions option is false'
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke preprocessors before parsing document' do
input = <<~'EOS'
junk line
= Document Title
sample content
EOS
begin
Asciidoctor::Extensions.register do
preprocessor ScrubHeaderPreprocessor
end
doc = document_from_string input
assert doc.attr? 'skipped'
assert_equal 'junk line', (doc.attr 'skipped').strip
assert doc.has_header?
assert_equal 'Document Title', doc.doctitle
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke include processor to process include directive' do
input = <<~'EOS'
before
include::lorem-ipsum.txt[]
after
EOS
begin
Asciidoctor::Extensions.register do
include_processor BoilerplateTextIncludeProcessor
end
# a custom include processor is not affected by the safe mode
result = convert_string input, safe: :secure
assert_css '.paragraph > p', result, 3
assert_includes result, 'before'
assert_includes result, 'Lorem ipsum'
assert_includes result, 'after'
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke include processor if it offers to handle include directive' do
input = <<~'EOS'
include::skip-me.adoc[]
line after skip
include::include-file.adoc[]
include::fixtures/grandchild-include.adoc[]
last line
EOS
registry = Asciidoctor::Extensions.create do
include_processor do
handles? do |target|
target == 'skip-me.adoc'
end
process do |_doc, _reader, _target, _attributes|
nil
end
end
include_processor do
handles? do |target|
target == 'include-file.adoc'
end
process do |_doc, reader, target, attributes|
# demonstrates that push_include normalizes newlines
content = [
%(found include target '#{target}' at line #{reader.cursor_at_prev_line.lineno}\r\n),
%(\r\n),
%(middle line\r\n),
]
reader.push_include content, target, target, 1, attributes
end
end
end
# safe mode only required for built-in include processor
document = empty_document base_dir: testdir, extension_registry: registry, safe: :safe
reader = Asciidoctor::PreprocessorReader.new document, input, nil, normalize: true
lines = []
lines << reader.read_line
assert_equal 'line after skip', lines.last
lines << reader.read_line
lines << reader.read_line
assert_equal 'found include target \'include-file.adoc\' at line 4', lines.last
assert_equal 'include-file.adoc: line 2', reader.line_info
lines << reader.read_line while reader.has_more_lines?
source = lines * Asciidoctor::LF
assert_match(/^found include target 'include-file.adoc' at line 4$/, source)
assert_match(/^middle line$/, source)
assert_match(/^last line of grandchild$/, source)
assert_match(/^last line$/, source)
end
test 'should invoke tree processors after parsing document' do
input = <<~'EOS'
= Document Title
Doc Writer
content
EOS
begin
Asciidoctor::Extensions.register do
tree_processor ReplaceAuthorTreeProcessor
end
doc = document_from_string input
assert_equal 'Ghost Writer', doc.author
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should set source_location on document before invoking tree processors' do
begin
Asciidoctor::Extensions.register do
tree_processor do
process do |doc|
para = create_paragraph doc.blocks.last.parent, %(file: #{doc.file}, lineno: #{doc.lineno}), {}
doc << para
end
end
end
sample_doc = fixture_path 'sample.adoc'
doc = Asciidoctor.load_file sample_doc, sourcemap: true
assert_includes doc.convert, 'file: sample.adoc, lineno: 1'
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should allow tree processor to replace tree' do
input = <<~'EOS'
= Original Document
Doc Writer
content
EOS
begin
Asciidoctor::Extensions.register do
tree_processor ReplaceTreeTreeProcessor
end
doc = document_from_string input
assert_equal 'Replacement Document', doc.doctitle
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should honor block title assigned in tree processor' do
input = <<~'EOS'
= Document Title
:!example-caption:
.Old block title
====
example block content
====
EOS
old_title = nil
begin
Asciidoctor::Extensions.register do
tree_processor do
process do |doc|
ex = (doc.find_by context: :example)[0]
old_title = ex.title
ex.title = 'New block title'
end
end
end
doc = document_from_string input
assert_equal 'Old block title', old_title
assert_equal 'New block title', (doc.find_by context: :example)[0].title
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should be able to register preferred tree processor' do
begin
Asciidoctor::Extensions.register do
tree_processor do
process do |doc|
doc << (create_paragraph doc, 'd', {})
nil
end
end
tree_processor do
prefer
process do |doc|
doc << (create_paragraph doc, 'c', {})
nil
end
end
prefer :tree_processor do
process do |doc|
doc << (create_paragraph doc, 'b', {})
nil
end
end
prefer (tree_processor do
process do |doc|
doc << (create_paragraph doc, 'a', {})
nil
end
end)
prefer :tree_processor, SelfSigningTreeProcessor
end
(doc = empty_document).convert
assert_equal %w(SelfSigningTreeProcessor a b c d), doc.blocks.map(&:lines).map(&:first)
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke postprocessors after converting document' do
input = <<~'EOS'
* one
* two
* three
EOS
begin
Asciidoctor::Extensions.register do
postprocessor StripAttributesPostprocessor
end
output = convert_string input
refute_match(/<div class="ulist">/, output)
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should yield to document processor block if block has non-zero arity' do
input = <<~'EOS'
hi!
EOS
begin
Asciidoctor::Extensions.register do
tree_processor do |processor|
processor.process do |doc|
doc << (create_paragraph doc, 'bye!', {})
end
end
end
output = convert_string_to_embedded input
assert_xpath '//p', output, 2
assert_xpath '//p[text()="hi!"]', output, 1
assert_xpath '//p[text()="bye!"]', output, 1
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke processor for custom block' do
input = <<~'EOS'
[yell]
Hi there!
[yell,chars=aeiou]
Hi there!
EOS
begin
Asciidoctor::Extensions.register do
block UppercaseBlock
end
output = convert_string_to_embedded input
assert_xpath '//p', output, 2
assert_xpath '(//p)[1][text()="HI THERE!"]', output, 1
assert_xpath '(//p)[2][text()="hI thErE!"]', output, 1
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should invoke processor for custom block in an AsciiDoc table cell' do
input = <<~'EOS'
|===
a|
[yell]
Hi there!
|===
EOS
begin
Asciidoctor::Extensions.register do
block UppercaseBlock
end
output = convert_string_to_embedded input
assert_xpath '/table//p', output, 1
assert_xpath '/table//p[text()="HI THERE!"]', output, 1
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should yield to syntax processor block if block has non-zero arity' do
input = <<~'EOS'
[eval]
....
'yolo' * 5
....
EOS
begin
Asciidoctor::Extensions.register do
block :eval do |processor|
processor.on_context :literal
processor.process do |parent, reader, _attrs|
create_paragraph parent, (eval reader.read_lines[0]), {} # rubocop:disable Security/Eval
end
end
end
output = convert_string_to_embedded input
assert_xpath '//p[text()="yoloyoloyoloyoloyolo"]', output, 1
ensure
Asciidoctor::Extensions.unregister_all
end
end
test 'should pass cloaked context in attributes passed to process method of custom block' do
input = <<~'EOS'
[custom]
****
sidebar
****
EOS
cloaked_context = nil
begin
Asciidoctor::Extensions.register do
block :custom do
on_context :sidebar
process do |_doc, _reader, attrs|
cloaked_context = attrs['cloaked-context']
nil
end
end
end
convert_string_to_embedded input
assert_equal :sidebar, cloaked_context
ensure
Asciidoctor::Extensions.unregister_all
end
end