Class: Mathematical

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

Defined Under Namespace

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

Constant Summary collapse

DEFAULT_OPTS =
{
  :ppi => 72.0,
  :zoom => 1.0,
  :base64 => false,
  :maxsize => 0,
  :format => :svg,
  :delimiter => [:DOLLAR, :DOUBLE]
}
XML_HEADER =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
MATH_MATCH =
%r{<math xmlns.+?</math>}m
VERSION =
'1.6.12'.freeze

Constants included from Validator

Validator::FORMAT_TYPES, Validator::RENDER_TYPES

Instance Method Summary collapse

Methods included from Validator

#validate_config, #validate_content, #validate_string

Methods included from Corrections

#adjust_lt_gt, #apply_corrections

Constructor Details

#initialize(options = {}) ⇒ Mathematical

Returns a new instance of Mathematical.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mathematical.rb', line 27

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

  validate_config(@config)

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

  if @config[:delimiter].is_a?(Symbol)
    @config[:delimiter] = Configuration::Delimiters.to_h[@config[:delimiter]]
  else
    @config[:delimiter] = @config[:delimiter].map do |delim|
      Configuration::Delimiters.to_h[delim]
    end.inject(0, :|)
  end

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

Instance Method Details

#filter(maths) ⇒ Object



53
54
55
56
57
# File 'lib/mathematical.rb', line 53

def filter(maths)
  maths = validate_content(maths)
  result_data = @processer.process(maths, RENDER_TYPES.find_index(:filter))
  result(result_data)
end

#parse(maths) ⇒ Object Also known as: render



45
46
47
48
49
# File 'lib/mathematical.rb', line 45

def parse(maths)
  maths = validate_content(maths)
  result_data = @processer.process(maths, RENDER_TYPES.find_index(:parse))
  result(result_data)
end

#result(result_data) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/mathematical.rb', line 87

def result(result_data)
  fail RuntimeError if !result_data.is_a?(Hash) && !result_data.is_a?(Array)

  if result_data.is_a? Array
    result_data.map { |d| format_data(d) }
  else
    format_data(result_data)
  end
end

#strict_filter(maths) ⇒ Object



81
82
83
84
85
# File 'lib/mathematical.rb', line 81

def strict_filter(maths)
  maths = validate_content(maths)
  result_data = @processer.process(maths, RENDER_TYPES.find_index(:strict_filter))
  result(result_data)
end

#text_filter(maths) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mathematical.rb', line 59

def text_filter(maths)
  maths = validate_content(maths)
  widths = []
  heights = []
  result_data = @processer.process(maths, RENDER_TYPES.find_index(:text_filter))
  # TODO: can/should be optimized to not do two calls here, but I am thinking
  # about moving to Rust and don't have time to write safe C...
  if result_data[:data] && @config[:format] != :mathml
    result_data[:data].gsub!(MATH_MATCH) do |match|
      result = @processer.process(maths, RENDER_TYPES.find_index(:parse))
      widths << result[:width]
      heights << result[:height]
      result[:data]
    end

    result_data[:width] = widths
    result_data[:height] = heights
  end

  result(result_data)
end