Sha256: 90c58e17b7d2e65b7567a2214a0714b49ce0b4ca4cc7607efcc8a326336ab77c

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'ssmd/processors'

module SSMD
  class Converter
    attr_reader :input, :skip

    def initialize(input, skip: [])
      @input = input
      @skip = Array(skip)

      processors.delete_if do |processor|
        self.skip.any? { |name| processor.name =~ /\ASSMD::Processors::#{name}Processor\Z/i }
      end
    end

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

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

    def strip
      processors.inject(input) do |text, processor|
        process processor.new(processor_options), text, strip: true
      end
    end

    def processors
      @processors ||= begin
        p = SSMD::Processors

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

    def process(processor, input, strip: false)
      if processor.matches? input
        result = strip ? processor.strip_ssmd(input) : processor.substitute(input)
        process processor, result, strip: strip
      else
        input
      end
    end

    def processor_options
      { skip: skip }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ssmd-0.5.0 lib/ssmd/converter.rb