Sha256: ff25ec9c23447c6b6313642b90e9b2cf013cd34560c5bb27f4736bb5db484aa0

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'ssmd/processors'

module SSMD
  class Converter
    attr_reader :input

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

      processors.delete_if do |processor|
        Array(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, text
      end

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

    def strip
      processors.inject(input) do |text, processor|
        process processor.new, 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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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