You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB

4 years ago
////
Included in:
- user-manual: Extensions: Compound Block Processor Example
////
Purpose::
Register a custom block named `collapsiblelisting` 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-collapsiblelisting-block.adoc
[source]
....
.Show JSON
[collapsiblelisting,json]
----
{
"foo": "bar"
}
----
....
.CollapsibleListingBlock
[source,ruby]
----
class CollapsibleListingBlock < 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 CollapsibleListingBlock, :collapsiblelisting
end
----
.Usage
$ asciidoctor -r ./collapsiblelisting.rb sample-with-collapsiblelisting-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 extenesion.