Posted by
mojavelinux on
Aug 16, 2013; 9:19am
URL: https://discuss.asciidoctor.org/Finally-a-draft-of-the-Asciidoctor-extension-API-has-landed-tp455p457.html
When you are creating a custom macro, it's pretty typical to also require a custom rendering template. You can, of course, create one using a template language supported Tilt, put it into a directory, then pass that directory to the `template_dir` option of Asciidoctor. However, that would mean that anyone using your extension would need to remember to do this (for now).
A better approach is to include the template with the extension code. It's possible to define a template in a block and register that with the document when the extension is initialized. Here's an example that uses a custom "p" template.
```ruby
require 'tilt'
block_p_template = Tilt.new('block_p.html.slim') do |t|
"p=content"
end
# Sample content
#
# [simple]
# Simple paragraph
#
class SimpleParaBlock < Asciidoctor::Extensions::BlockProcessor
option :contexts, [:paragraph]
option :content_model, :simple
def initialize(document)
super
document.renderer.register_view 'block_p', block_p_template
end
def process parent, reader, attributes
Asciidoctor::Block.new(parent, :p, :source => reader.lines)
end
end
Extensions.register do |document|
block :simple, SimpleParaBlock
end
Asciidoctor.render_file('sample-with-simple-para.ad', :safe => :safe,
:in_place => true)
```
--
Dan Allen |
http://google.com/profiles/dan.j.allen