module Asciidoctor::Converter
A base module for defining converters that can be used to convert {AbstractNode} objects in a parsed AsciiDoc document to a backend format such as HTML or DocBook.
Implementing a converter involves:
-
including this module in a {Converter} implementation class
-
overriding the {Converter#convert} method
-
optionally associating the converter with one or more backends using the {#register_for} DSL method imported by the {Config Converter::Config} module
Examples
class TextConverter include Asciidoctor::Converter register_for 'text' def initialize backend, opts super outfilesuffix '.txt' end def convert node, transform = nil case (transform ||= node.node_name) when 'document' node.content when 'section' [node.title, node.content] * "\n\n" when 'paragraph' node.content.tr("\n", ' ') << "\n" else if transform.start_with? 'inline_' node.text else %(<#{transform}>\n) end end end end puts Asciidoctor.convert_file 'sample.adoc', backend: :text
Public Class Methods
Mixes the {Config Converter::Config} module into any class that includes the {Converter} module.
converter - The Class that includes the {Converter} module
Returns nothing
# File lib/asciidoctor/converter.rb, line 134 def included converter converter.extend Config end
Public: Creates a new instance of Converter
backend - The String backend format to which this converter converts. opts - An options Hash (optional, default: {})
Returns a new instance of [Converter]
# File lib/asciidoctor/converter.rb, line 148 def initialize backend, opts = {} @backend = backend setup_backend_info end
Public Instance Methods
Public: Converts an {AbstractNode} using the specified transform along with additional options. If a transform is not specified, implementations typically derive one from the {AbstractNode#node_name} property.
Implementations are free to decide how to carry out the conversion. In the case of the built-in converters, the tranform value is used to dispatch to a handler method. The {TemplateConverter} uses the value of the transform to select a template to render.
node - The concrete instance of AbstractNode to convert transform - An optional String transform that hints at which transformation
should be applied to this node. If a transform is not specified, the transform is typically derived from the value of the node's node_name property. (optional, default: nil)
opts - An optional Hash of options that provide additional hints about
how to convert the node. (optional, default: {})
Returns the [String] result
# File lib/asciidoctor/converter.rb, line 181 def convert node, transform = nil, opts = {} raise ::NotImplementedError end
Alias for backward compatibility.