https://discuss.asciidoctor.org/How-to-introduce-a-new-Slim-template-tp3248p3249.html
Sebastien,
You still have to use the extension API to register the new block, but you can put all the logic in the template.
Here's how you register the block (shout-extension.rb):
require 'asciidoctor/extensions'
Asciidoctor::Extensions.register do
block do
named :shout
on_context :paragraph
name_attributes 'vol'
process do |parent, reader, attrs|
create_block parent, 'shout', reader.lines, attrs
end
end
end
The key line is this:
create_block parent, 'shout', reader.lines, attrs
That introduces a new type of block which you can then handle using a custom template.
Here's the template (shout.html.slim):
= content.upcase
= '!' * (attr 'vol', 1).to_i
This will handle the following AsciiDoc:
[shout, 5]
Just do it
Now you load it using:
$ asciidoctor -r ./shout-extension.rb -T /path/to/templates doc.adoc
Of course, you could introduce a whole family of custom blocks by reading the name of the block from an attribute. For instance, let's say you make the second positional attribute the block name and the style something generic.
[custom, shout]
Just do it
Then the extension would change to:
Asciidoctor::Extensions.register do
block do
named :custom
on_context :paragraph
name_attributes 'name'
process do |parent, reader, attrs|
create_block parent, attrs['name'], reader.lines, attrs
end
end
end
Now, you only need one extension to cover an arbitrary number of custom blocks.
Keep in mind you'll need to change the value of `on_context` (or `on_contexts`) if you want to apply this to another type of block, such as an open block.
I hope that gets you started.
Cheers,
-Dan