Sha256: 7d05da2e0a823079efa03d668ab4693ed3b539f8d411ad160ed0c33456d9c400

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module DMark
  class Translator
    class UnhandledNode < StandardError
      attr_reader :node

      def initialize(node)
        @node = node
      end

      def message
        case @node
        when String
          'Unhandled string node'
        when DMark::ElementNode
          "Unhandled element node #{@node.name.inspect}"
        else
          "Unhandled node #{@node.inspect}"
        end
      end
    end

    def self.translate(nodes, context = {})
      new.translate(nodes, context)
    end

    def translate(nodes, context = {})
      [nodes.map { |node| handle(node, context) }].flatten.join('')
    end

    def handle(node, context = {})
      case node
      when String
        handle_string(node, context)
      when DMark::ElementNode
        handle_element(node, context)
      else
        raise ArgumentError, "Cannot handle #{node.class}"
      end
    end

    # @abstract
    def handle_string(string, _context)
      raise DMark::Translator::UnhandledNode.new(string)
    end

    # @abstract
    def handle_element(element, _context)
      raise DMark::Translator::UnhandledNode.new(element)
    end

    private

    def handle_children(node, context)
      node.children.map { |child| handle(child, context) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
d-mark-1.0.0b2 lib/d-mark/translator.rb
d-mark-1.0.0b1 lib/d-mark/translator.rb
d-mark-1.0.0a4 lib/d-mark/translator.rb
d-mark-1.0.0a3 lib/d-mark/translator.rb