Sha256: da93d5fffc95853f0947a141a56064f53338cd894f356966b2fb72716b6a648b

Contents?: true

Size: 994 Bytes

Versions: 2

Compression:

Stored size: 994 Bytes

Contents

require 'ssmd/processors'
require 'rexml/document'

module SSMD
  class Converter
    attr_reader :input

    def initialize(input)
      @input = input
    end

    def convert
      result = processors.inject(input) do |text, processor|
        process processor.new, text
      end

      "<speak>#{result.strip}</speak>"
    end

    def strip
      doc = ::REXML::Document.new input

      if doc.root
        xml_text doc.root
      else
        input # no XML so we assume it is already stripped
      end
    end

    def xml_text(node)
      if node.is_a? REXML::Text
        node.to_s
      else
        node.children.map { |c| xml_text c }.join
      end
    end

    def processors
      p = SSMD::Processors

      [
        p::EmphasisProcessor, p::AnnotationProcessor, p::MarkProcessor
      ]
    end

    def process(processor, input)
      if processor.matches? input
        process processor, processor.substitute(input)
      else
        input
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ssmd-0.2.2 lib/ssmd/converter.rb
ssmd-0.2.1 lib/ssmd/converter.rb