Class: Mathematical

Inherits:
Object
  • Object
show all
Includes:
Corrections
Defined in:
lib/mathematical.rb,
lib/mathematical/version.rb,
lib/mathematical/corrections.rb,
ext/mathematical/mathematical.c

Defined Under Namespace

Modules: Corrections Classes: DocumentCreationError, DocumentReadError, MaxsizeError, ParseError, Process

Constant Summary collapse

FORMAT_TYPES =
[:svg, :png, :mathml]
DEFAULT_OPTS =
{
  :ppi => 72.0,
  :zoom => 1.0,
  :base64 => false,
  :maxsize => 0,
  :format => :svg
}
XML_HEADER =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
VERSION =
'1.0.0'

Instance Method Summary collapse

Methods included from Corrections

#adjust_lt_gt, #apply_corrections

Constructor Details

#initialize(opts = {}) ⇒ Mathematical

Returns a new instance of Mathematical.



23
24
25
26
27
28
29
30
31
# File 'lib/mathematical.rb', line 23

def initialize(opts = {})
  @config = DEFAULT_OPTS.merge(opts)

  validate_config

  @config[:formatInt] = FORMAT_TYPES.index(@config[:format])

  @processer = Mathematical::Process.new(@config)
end

Instance Method Details

#render(maths) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mathematical.rb', line 33

def render(maths)
  maths = validate_content(maths)

  begin
    data = @processer.process(maths)
    fail RuntimeError if data.nil? || (!data.is_a?(Hash) && !data.is_a?(Array))

    if data.is_a? Array
      data.map { |d| format_data(d) }
    else
      format_data(data)
    end
  rescue ParseError, DocumentCreationError, DocumentReadError => e
    # an error in the C code, probably a bad TeX parse
    $stderr.puts "#{e.message}: #{maths}"
    maths
  end
end

#validate_content(maths) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mathematical.rb', line 52

def validate_content(maths)
  if !maths.is_a?(String) && !maths.is_a?(Array)
    fail(TypeError, 'input must be a string or an array!')
  end

  if maths.is_a? String
    validate_string(maths)
  else
    validate_array(maths)
  end
end