"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/converter/composite.rb" (1 Jun 2019, 1986 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 module Asciidoctor
    3 # A {Converter} implementation that delegates to the chain of {Converter}
    4 # objects passed to the constructor. Selects the first {Converter} that
    5 # identifies itself as the handler for a given transform.
    6 class Converter::CompositeConverter < Converter::Base
    7   # Get the Array of Converter objects in the chain
    8   attr_reader :converters
    9 
   10   def initialize backend, *converters, backend_traits_source: nil
   11     @backend = backend
   12     (@converters = converters).each {|converter| converter.composed self if converter.respond_to? :composed }
   13     init_backend_traits backend_traits_source.backend_traits if backend_traits_source
   14     @converter_cache = ::Hash.new {|hash, key| hash[key] = find_converter key }
   15   end
   16 
   17   # Public: Delegates to the first converter that identifies itself as the
   18   # handler for the given transform. The optional Hash is passed as the last
   19   # option to the delegate's convert method.
   20   #
   21   # node      - the AbstractNode to convert
   22   # transform - the optional String transform, or the name of the node if no
   23   #             transform is specified. (default: nil)
   24   # opts      - an optional Hash that is passed to the delegate's convert method. (default: nil)
   25   #
   26   # Returns the String result returned from the delegate's convert method
   27   def convert node, transform = nil, opts = nil
   28     (converter_for transform ||= node.node_name).convert node, transform, opts
   29   end
   30 
   31   # Public: Retrieve the converter for the specified transform.
   32   #
   33   # Returns the matching [Converter] object
   34   def converter_for transform
   35     @converter_cache[transform]
   36   end
   37 
   38   # Public: Find the converter for the specified transform.
   39   # Raise an exception if no converter is found.
   40   #
   41   # Returns the matching [Converter] object
   42   def find_converter transform
   43     @converters.each {|candidate| return candidate if candidate.handles? transform }
   44     raise %(Could not find a converter to handle transform: #{transform})
   45   end
   46 end
   47 end