class SSMD::Converter

Attributes

input[R]
skip[R]

Public Class Methods

new(input, skip: []) click to toggle source
# File lib/ssmd/converter.rb, line 8
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

Public Instance Methods

convert() click to toggle source
# File lib/ssmd/converter.rb, line 17
def convert
  result = processors.inject(escape_xml(input)) do |text, processor|
    process processor.new(processor_options), text
  end

  "<speak>#{result.strip}</speak>"
end
escape_xml(text) click to toggle source

Substitutes special characters with their XML entity. E.g. it substitutes “<” with “&lt;”.

# File lib/ssmd/converter.rb, line 60
def escape_xml(text)
  text.encode(xml: :text)
end
process(processor, input, strip: false) click to toggle source
# File lib/ssmd/converter.rb, line 44
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
processor_options() click to toggle source
# File lib/ssmd/converter.rb, line 53
def processor_options
  { skip: skip }
end
processors() click to toggle source
# File lib/ssmd/converter.rb, line 33
def processors
  @processors ||= begin
    p = SSMD::Processors

    [
      p::EmphasisProcessor, p::AnnotationProcessor, p::MarkProcessor,
      p::ProsodyProcessor, p::ParagraphProcessor, p::BreakProcessor
    ]
  end
end
strip() click to toggle source
# File lib/ssmd/converter.rb, line 25
def strip
  result = processors.inject(escape_xml(input)) do |text, processor|
    process processor.new(processor_options), text, strip: true
  end

  unescape_xml result
end
unescape_xml(text) click to toggle source

Substitutes back XML entities with their plain text equivalent. E.g. it substitutes “&lt;” with “<”.

@TODO Find alternative which applies to XML in general. Not sure if it even makes a difference in this case, though.

# File lib/ssmd/converter.rb, line 70
def unescape_xml(text)
  CGI.unescape_html text
end