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(options = {}) ⇒ Converter

Create a new Converter.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :executable (String) — default: 'texmath'

    the executable path

  • :from (Symbol) — default: :tex

    the source format

  • :to (Symbol) — default: :mathml

    the destination format



36
37
38
39
40
# 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

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, error unless error.empty?
    return output.strip
  end
rescue Errno::ENOENT
  raise NoExecutableError, "Can't find the '#{executable}' executable."
end