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
|
= Compound Block Processor Example
:navtitle: Compound Block Processor
Purpose::
Register a custom block named `collapsible` that transforms a listing block into a compound block composed of the following:
* an example block with the collapsible option enabled
* the original listing block
* the listing block is promoted to a source block if a language is specified using the second positional attribute.
.sample-with-collapsible-block.adoc
[source,asciidoc]
....
.Show JSON
[collapsible,json]
----
{
"foo": "bar"
}
----
....
.collapsible-block.rb
[source,ruby]
----
class CollapsibleBlock < Asciidoctor::Extensions::BlockProcessor
enable_dsl
on_context :listing
positional_attributes 'language'
def process parent, reader, attrs
lang = attrs.delete 'language'
attrs['title'] ||= 'Show Listing'
example = create_example_block parent, [], attrs, content_model: :compound
example.set_option 'collapsible'
listing = create_listing_block example, reader.readlines, nil
if lang
listing.style = 'source'
listing.set_attr 'language', lang
listing.commit_subs
end
example << listing
example
end
end
Asciidoctor::Extensions.register do
block CollapsibleBlock, :collapsible
end
----
.Usage
$ asciidoctor -r ./collapsible-block.rb sample-with-collapsible-block.adoc
NOTE: This extension mimics the builtin `collapsible` option on the example block, but consolidates it to a single block.
The purpose of this extension is to show how to assemble a compound block in an extension.
|