Class: Asciidoctor::Converter::CompositeConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/asciidoctor/converter/composite.rb

Overview

identifies itself as the handler for a given transform.

Constant Summary

Constants included from DefaultFactory

DefaultFactory::PROVIDED

Instance Attribute Summary collapse

Attributes included from Asciidoctor::Converter

#backend

Instance Method Summary collapse

Methods inherited from Base

#content_only, #handles?, #skip

Methods included from Asciidoctor::Converter

derive_backend_traits, #handles?

Methods included from DefaultFactory

#for, #register, #unregister_all

Methods included from Factory

create, #create, default, #for, new, #register

Methods included from Logging

#logger, #message_with_context

Constructor Details

#initialize(backend, *converters, backend_traits_source: nil) ⇒ CompositeConverter

Returns a new instance of CompositeConverter.



10
11
12
13
14
15
# File 'lib/asciidoctor/converter/composite.rb', line 10

def initialize backend, *converters, backend_traits_source: nil
  @backend = backend
  (@converters = converters).each {|converter| converter.composed self if converter.respond_to? :composed }
  init_backend_traits backend_traits_source.backend_traits if backend_traits_source
  @converter_cache = ::Hash.new {|hash, key| hash[key] = find_converter key }
end

Instance Attribute Details

#convertersObject (readonly)

Get the Array of Converter objects in the chain



8
9
10
# File 'lib/asciidoctor/converter/composite.rb', line 8

def converters
  @converters
end

Instance Method Details

#convert(node, transform = nil, opts = nil) ⇒ Object

Delegates to the first converter that identifies itself as the handler for the given transform. The optional Hash is passed as the last option to the delegate’s convert method.

Parameters:

  • node

    the AbstractNode to convert

  • transform (defaults to: nil)

    the optional String transform, or the name of the node if no transform is specified. (default: nil)

  • opts (defaults to: nil)

    an optional Hash that is passed to the delegate’s convert method. (default: nil)

Returns:

  • the String result returned from the delegate’s convert method



27
28
29
# File 'lib/asciidoctor/converter/composite.rb', line 27

def convert node, transform = nil, opts = nil
  (converter_for transform ||= node.node_name).convert node, transform, opts
end

#converter_for(transform) ⇒ Converter

Retrieve the converter for the specified transform.

Returns:

  • (Converter)

    Returns the matching Converter object



34
35
36
# File 'lib/asciidoctor/converter/composite.rb', line 34

def converter_for transform
  @converter_cache[transform]
end

#find_converter(transform) ⇒ Converter

Find the converter for the specified transform.

Returns:

  • (Converter)

    Returns the matching Converter object

Raises:

  • Raise an exception if no converter is found.



42
43
44
45
# File 'lib/asciidoctor/converter/composite.rb', line 42

def find_converter transform
  @converters.each {|candidate| return candidate if candidate.handles? transform }
  raise %(Could not find a converter to handle transform: #{transform})
end