Class: Mathtype::Converter
- Inherits:
-
Object
- Object
- Mathtype::Converter
- Defined in:
- lib/mathtype.rb
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#initialize(equation) ⇒ Converter
constructor
A new instance of Converter.
- #process(element: "mtef", object:) ⇒ Object
- #process_final_element(element, object) ⇒ Object
- #set_parser(equation) ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(equation) ⇒ Converter
Returns a new instance of Converter.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mathtype.rb', line 15 def initialize(equation) set_parser(equation) raise ::NotImplementedError, "Only .wmf and .bin (OLE.-Object) currently supported, name supplied: #{equation}" unless @parser.equation @version = @parser.equation[0].unpack('C')[0].to_i raise ::NotImplementedError, "Only MTEF Version 3 and 5 currently supported, version is #{version}" unless (version==3 or version==5) case @version when 3 data = Mathtype3::Equation.read(@parser.equation).snapshot when 5 data = Mathtype5::Equation.read(@parser.equation).snapshot end @builder = Nokogiri::XML::Builder.new do |xml| @xml = xml xml.root do process(object: data) end end end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
13 14 15 |
# File 'lib/mathtype.rb', line 13 def builder @builder end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
11 12 13 |
# File 'lib/mathtype.rb', line 11 def parser @parser end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
14 15 16 |
# File 'lib/mathtype.rb', line 14 def version @version end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
12 13 14 |
# File 'lib/mathtype.rb', line 12 def xml @xml end |
Instance Method Details
#process(element: "mtef", object:) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mathtype.rb', line 46 def process(element: "mtef", object:) if object.is_a? Hash case @version when 3 name = Mathtype3::RECORD_NAMES[object[:record_type]] else name = Mathtype5::RECORD_NAMES[object[:record_type]] end if name xml.send(name) do (object[:payload] || {}).each do |k, v| process(element: k, object: v) end end else xml.send(element) do object.each do |k, v| process(element: k, object: v) end end end elsif object.is_a? Array object.each do |a| process(element: element, object: a) end else process_final_element(element, object) end end |
#process_final_element(element, object) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/mathtype.rb', line 76 def process_final_element(element, object) if object.is_a? Hash xml.send(element, object) else xml.send(element) { xml.text object } end end |
#set_parser(equation) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/mathtype.rb', line 34 def set_parser(equation) if equation.end_with?(".bin") @parser = Mathtype::OleFileParser.new equation else equation.end_with?(".wmf") @parser = Mathtype::WmfFileParser.new equation end end |
#to_xml ⇒ Object
42 43 44 |
# File 'lib/mathtype.rb', line 42 def to_xml @builder.to_xml end |