Sha256: de81b8f01af021017e5673a474e2be4c63857563e4354d0efbc76f45582f1a26

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 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, p::BreakProcessor
        ]
      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

6 entries across 6 versions & 1 rubygems

Version Path
ssmd-0.7.3 lib/ssmd/converter.rb
ssmd-0.7.2 lib/ssmd/converter.rb
ssmd-0.7.1 lib/ssmd/converter.rb
ssmd-0.7.0 lib/ssmd/converter.rb
ssmd-0.6.1 lib/ssmd/converter.rb
ssmd-0.6.0 lib/ssmd/converter.rb