Class: TeXMath::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/texmath/converter.rb

Overview

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.

Constant Summary collapse

READERS =

The available readers and their corresponding names.

{
  'tex' => 'LaTeX',
  'mathml' => 'MathML',
  'omml' => 'Office Math Markup Language',
  'native' => 'texmath native'
}
WRITERS =

The available writers and their corresponding names.

{
  'tex' => 'LaTeX',
  'mathml' => 'MathML',
  'omml' => 'Office Math Markup Language',
  'xhtml' => 'XHTML',
  'pandoc' => 'pandoc native',
  'native' => 'texmath native'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable = 'texmath', from: :tex, to: :mathml) ⇒ Converter

Create a new Converter.

Parameters:

  • executable (String) (defaults to: 'texmath')

    the executable path

  • from (Symbol) (defaults to: :tex)

    the source format

  • to (Symbol) (defaults to: :mathml)

    the destination format



36
37
38
39
40
# File 'lib/texmath/converter.rb', line 36

def initialize(executable = 'texmath', from: :tex, to: :mathml)
  @executable = executable
  self.reader = from
  self.writer = to
end

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



42
43
44
# File 'lib/texmath/converter.rb', line 42

def executable
  @executable
end

#readerObject

Returns the value of attribute reader.



60
61
62
# File 'lib/texmath/converter.rb', line 60

def reader
  @reader
end

#writerObject

Returns the value of attribute writer.



60
61
62
# File 'lib/texmath/converter.rb', line 60

def writer
  @writer
end

Instance Method Details

#convert(data) ⇒ String

Convert data between formats.

Returns:

  • (String)

    the converted data



47
48
49
50
51
52
53
54
55
56
57
58
# 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.new(error) unless error.empty?
    return output.strip
  end
rescue Errno::ENOENT
  raise NoExecutableError.new("Can't find the '#{executable}' executable.")
end