Login  Register

Re: How to introduce a new Slim template

Posted by mojavelinux on May 20, 2015; 11:42pm
URL: 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

On Wed, May 20, 2015 at 4:02 PM, torngat [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi everyone,

I thought I should start a new thread for this topic instead of continuing to overload another.  In essence, I'm still not clear on how I can create a new Slim template such that it is recognized by Asciidoctor as something valid (and used when running via the Maven plugin and not CLI).

How could I reproduce the "yell" example from the user manual if I want to use a Slim template approach instead of a Java based extension?  How can/should "yell" get registered?  Not being sure how to go about it, I created a Slim template called "block_yell.html.slim" and tried variations like this:

----
[yell]
Hey! Ho! Let's go!

[yell]
--
Hey! Ho! Let's go!
--

[name=yell]
Hey! Ho! Let's go!
----

The first two warn of invalid styles.  The third simply outputs the string and doesn't use the template.  I have also tried other variations, but to no avail.   Is this possible?

Thanks!

Sebastien





If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/How-to-introduce-a-new-Slim-template-tp3248.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--