The best way to get started developing converters is to extend a registered converter and play around with changing its behavior.
To create a custom converter, you define a Ruby class in a Ruby source file that you pass to Asciidoctor when you run it.
To get started, create a file named my-html5-converter.rb and open it.
The Ruby code in this file will run in the context of Asciidoctor, so you don’t need to add a require statement to use the Ruby APIs from Asciidoctor.
To extend a registered converter, you first need to get a reference to it.
That’s the purpose of the {url-api-gems}/asciidoctor/{release-version}/Asciidoctor/Converter/Factory#for-instance_method[Asciidoctor::Converter.for] method.
This method will resolve the class of the converter that’s currently registered for a backend.
If we’re looking for the converter for the html5 backend (i.e., the HTML 5 converter), we pass in the string html5.
Asciidoctor::Converter.for 'html5'
# => Asciidoctor::Converter::Html5Converter
Next, we want to extend this class.
To extend a class in Ruby, you declare the class, then use the < operator to indicate the class from which to extend.
class MyHtml5Converter < (Asciidoctor::Converter.for 'html5')
end
Congratulations!
You’ve created your first custom converter.
But wait, it’s not yet registered, which means it isn’t going to be used.
Let’s fix that.
To register the converter class, you need to declare the backend you want it to be mapped to.
In order to customize the HTML that Asciidoctor produces, you declare the backend as html5 using the register_for method.
In doing so, this registers the custom converter over the built-in converter, effectively replacing it.
class MyHtml5Converter < (Asciidoctor::Converter.for 'html5')
register_for 'html5'
end
Although we haven’t changed any of the behavior, this converter can be used…almost.
The final step is to tell Asciidoctor to load this file when it starts.
You can do that by passing the file’s path to the -r CLI option as follows.
$ asciidoctor -r ./my-html5-converter.rb doc.adoc
When Asciidoctor starts, it will tell Ruby to evaluate the Ruby source file.
When it does, Ruby will define the MyHtml5Converter class.
While defining the class, it will call the register_for method, which will register the class with the html5 backend (replacing the built-in converter).
This means Asciidoctor is now using your custom converter.
Now that you’ve configured Asciidoctor to use your custom converter, it’s time to get it to do something different.
Let’s say that you want to simplify the HTML that the built-in converter produces for a paragraph down to a single <p> element.
A custom converter is exactly the tool you need to accomplish this goal.
In this case, we’ll be overriding the convert_paragraph method.
When extending a built-in converter (or any converter that extends {url-api-gems}/asciidoctor/{release-version}/Asciidoctor/Converter/Base[Asciidoctor::Converter::Base]), the name of the convert method for a node (i.e., block or inline element) in the parsed document model is the context of the node (e.g., paragraph) prefixed with convert_.
That’s how we arrive at the method name convert_paragraph for a paragraph.
You can find a list of all such methods in contexts-ref.html.
The converter method accepts the node as the first parameter.
For blocks, the node is an instance of {url-api-gems}/asciidoctor/{release-version}/Asciidoctor/Block[Asciidoctor::Block].
Let’s add the convert_paragraph method to our custom converter to provide a custom implementation.
class MyHtml5Converter < (Asciidoctor::Converter.for 'html5')
register_for 'html5'
def convert_paragraph node
logger.warn 'Converting a paragraph...' (1)
super
end
end
-
The base converter automatically includes the Logging module, which gives your converter access to Asciidoctor’s logger.
So far, all we’ve done is print an intent to convert a paragraph, then delegate back to the super method (i.e., the original implementation).
If you run Asciidoctor as before:
$ asciidoctor -r ./my-html5-converter.rb doc.adoc
you should now see the following message in your terminal window:
asciidoctor: WARNING: Converting a paragraph...
Showing how to delegate to the super method is important as it demonstrates that you can still use the built-in logic in certain cases (or even decorate the HTML it produces).
But let’s replace it with our own logic instead.
class MyHtml5Converter < (Asciidoctor::Converter.for 'html5')
register_for 'html5'
def convert_paragraph node
%(<p>#{node.content}</p>)
end
end
If you run Asciidoctor as before, you should now see that paragraphs are converted to a simple <p> element.
<p>Content of paragraph.</p>
But we’re missing some things, such as the ID, the role, and the title.
Let’s fill in those gaps.
class MyHtml5Converter < (Asciidoctor::Converter.for 'html5')
register_for 'html5'
def convert_paragraph node
attributes = []
attributes << %( id="#{node.id}") if node.id
attributes << %( class="#{node.role}") if node.role
title = node.title? ? %(<span class="title">#{node.title}</span> ) : ''
%(<p#{attributes.join}>#{title}#{node.content}</p>)
end
end
Assuming the paragraph has an ID, role, and title, here’s the output this converter will produce:
<p id="intro" class="summary"><span class="title">What is a wolpertinger?</span> A wolpertinger is a ravenous beast.</p>
You’ve not only created your first custom converter, but you’re well on your way to customizing the HTML that Asciidoctor produces to suit your own needs!
Now that you’ve successfully extended and replaced a registered converter, let’s look at how to create a converter from scratch.