-
Notifications
You must be signed in to change notification settings - Fork 1
Can't selectively apply a template based on a role name #13
Comments
I think this use case should be solved by a custom extension. You should check what @oncletom has done with RunKit: https://github.com/oncletom/asciidoctor-extension-interactive-runner |
Thank you Guillaume for your reply, I will take a look at that this WE. Something like the |
I might be wrong but I think it's exactly the purpose of the
Basically you configure a list of converters and the composite converter will delegate to the first converter that identifies itself as the handler. |
:) I was also running into a very similar problem just a day or two ago - you can have custom templates for everything, but if you want to have a custom block type you have to write it as an extension. I was trying to think if there was some way I could jerry-rig something to auto-register an extension whenever templates were passed in that weren't already part of the AsciiDoctor core. So if you did: [custom_block]
----
Some content here
---- You could just include a template called <div id="my-element"></div>
<script>var notebook = RunKit.createNotebook({
// the parent element for the new notebook
element: document.getElementById("my-element"),
// specify the source of the notebook
source: "{{node.content}}"
})</script> I didn't get the time to actually prototype anything or do any additional research though, so there are likely concerns I didn't think about. |
@Mogztter
I need finer granularity than that. As I understand it, the CompositeConverter will delegate to a given "child" converter depending on the node name. As a matter of fact, I even think the handler is cached based on that name of the node here (as far as I understand the Ruby syntax :/ ) So I can't see how/if it could be used to, say, override the default handler only if a given role or attribute is set for a node. @danShumway |
That's a really good point - you should be able to (for the most part) take the same content and render anywhere. |
Indeed
You could extend the default Pseudo-code def listing node
if node.role? 'runkit'
# ... do something special
else
super node # fallback to the default implementation
end
end But I still think the best approach is to use an extension. Do you have any constraints for not using an extension ? If you want to use a template then you could extend the default Pseudo-code def convert node, template_name = nil, opts = {}
if node.node_name == 'listing' and node.role? 'runkit'
template_name = 'listing_runkit'
end
super node, template_name, opts
end Just throwing some ideas 😉 NOTE: A generic solution could be to create a method to resolve the template name from a node (in Asciidoctor core): def resolve_template_name node
node.node_name # default implementation
end @mojavelinux What do you think about this idea ? |
Thanks for your suggestions Guillaume. Basically, what I have now is (pseudo JS):
I think this gives me a lot of freedom, while working fully at JS level without writing a line of Ruby/Opal code. What do you think of that solution? |
FWIW, with the latest version of
And in the source document, I just had to add the "runkit" role to the code block:
And here is the result: Cool, isn't it? |
I love that idea. It could even be something we introduce into the Ruby version. The idea of the handles? / converter_for is to find the top converter for a transform (aka node name) in the chain. While overriding that logic is possible, it incurs a large cost for 99% of the nodes. Your next() idea is going to be much more efficient as it only has to be called for the exception cases. |
I have an interesting use case I cannot solve using the template system (neither asciidoctor-template.js nor my own asciidoctor.js-pug implementation):
I am using Asciidoctor right now to write a blog post. It contains several blocks of code. Some of them need to be processed differently as I want to replace them by RunKit notebooks. My idea was to add a custom role on the corresponding blocks:
The problem is I can't see how I could write a custom template to process blocks of code with the runkit role while still using the default HTML5 convert() method for the other blocks of code.
I hope I'm clear enough. Any idea?
The text was updated successfully, but these errors were encountered: