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:

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
        %Q(<#{transform}>\n)
      end
    end
  end
end

puts Asciidoctor.convert_file 'sample.adoc', backend: :text

Public Class Methods

new(backend, opts = {}) click to toggle source

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

convert(node, transform = nil) click to toggle source
# Public: Invoked when this converter is added to the chain of converters in a {CompositeConverter}.
#
# owner - The CompositeConverter instance
#
# Returns nothing
def composed owner
end

# Public: Converts an {AbstractNode} using the specified transform. 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) # # Returns the [String] result

# File lib/asciidoctor/converter.rb, line 179
def convert node, transform = nil
  raise ::NotImplementedError
end
convert_with_options(node, transform = nil, opts = {}) click to toggle source

Public: Converts an {AbstractNode} using the specified transform along with additional options. Delegates to {#convert} without options by default.

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.

Returns the [String] result

# File lib/asciidoctor/converter.rb, line 195
def convert_with_options node, transform = nil, opts = {}
  convert node, transform
end
included(converter) click to toggle source

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