class TeXMath::Converter

A `Converter` represents a single `texmath` command to which data can be piped. For example, a `Converter` that converts LaTeX to MathML can be reused for different input expressions, although all would have to be valid LaTeX.

Constants

READERS

The available readers and their corresponding names.

WRITERS

The available writers and their corresponding names.

Attributes

executable[R]
reader[R]
writer[R]

Public Class Methods

new(options = {}) click to toggle source

Create a new Converter. @option options [String] :executable ('texmath') the executable path @option options [Symbol] :from (:tex) the source format @option options [Symbol] :to (:mathml) the destination format

# File lib/texmath/converter.rb, line 36
def initialize(options = {})
  @executable = options.fetch(:executable, 'texmath')
  self.reader = options.fetch(:from, :tex)
  self.writer = options.fetch(:to, :mathml)
end

Public Instance Methods

convert(data) click to toggle source

Convert `data` between formats. @return [String] the converted data

# File lib/texmath/converter.rb, line 47
def convert(data)
  Open3.popen3(command) do |stdin, stdout, stderr| 
    stdin.puts(data)
    stdin.close
    output = stdout.read
    error = stderr.read
    raise ConversionError, error unless error.empty?
    return output.strip
  end
rescue Errno::ENOENT
  raise NoExecutableError, "Can't find the '#{executable}' executable."
end
reader=(format) click to toggle source
# File lib/texmath/converter.rb, line 62
def reader=(format)
  return @reader = format if READERS.key?(format.to_s)
  raise ArgumentError, "Can't find '#{format}' reader."
end
writer=(format) click to toggle source
# File lib/texmath/converter.rb, line 67
def writer=(format)
  return @writer = format if WRITERS.key?(format.to_s)
  raise ArgumentError, "Can't find '#{format}' writer."
end

Private Instance Methods

command() click to toggle source
# File lib/texmath/converter.rb, line 74
def command
  "#{executable} -f #{reader} -t #{writer}"
end