Compound Block Processor Example

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.

Example 1. sample-with-collapsible-block.adoc
.Show JSON
[collapsible,json]
----
{
   "foo": "bar"
}
----
Example 2. collapsible-block.rb
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
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.