Class: RubyDocx::Elements::OleObject

Inherits:
Element
  • Object
show all
Defined in:
lib/ruby_docx/elements/ole_object.rb

Instance Attribute Summary collapse

Attributes inherited from Element

#doc, #grid, #node

Instance Method Summary collapse

Methods inherited from Element

#elements, #initialize, #inspect, #to_xml

Constructor Details

This class inherits a constructor from RubyDocx::Elements::Element

Instance Attribute Details

#image_pathObject

Returns the value of attribute image_path.



27
28
29
# File 'lib/ruby_docx/elements/ole_object.rb', line 27

def image_path
  @image_path
end

Returns the value of attribute link.



26
27
28
# File 'lib/ruby_docx/elements/ole_object.rb', line 26

def link
  @link
end

#pathObject

Returns the value of attribute path.



26
27
28
# File 'lib/ruby_docx/elements/ole_object.rb', line 26

def path
  @path
end

#zipObject

Returns the value of attribute zip.



26
27
28
# File 'lib/ruby_docx/elements/ole_object.rb', line 26

def zip
  @zip
end

Instance Method Details

#base64_dataObject



69
70
71
# File 'lib/ruby_docx/elements/ole_object.rb', line 69

def base64_data
  "data:image/png;base64,#{Base64.strict_encode64(self.to_png)}"
end

#dataObject



65
66
67
# File 'lib/ruby_docx/elements/ole_object.rb', line 65

def data
  self.to_png
end

#relation_idObject



38
39
40
41
42
43
44
45
# File 'lib/ruby_docx/elements/ole_object.rb', line 38

def relation_id
  element = @node.xpath(".//o:OLEObject").first
  if element && element.attributes.keys.index("id")
    element.attributes["id"].value
  else
    nil
  end
end

#replace(lnk) ⇒ Object



79
80
81
# File 'lib/ruby_docx/elements/ole_object.rb', line 79

def replace(lnk)
  @link = lnk
end

#save(path) ⇒ Object



73
74
75
76
77
# File 'lib/ruby_docx/elements/ole_object.rb', line 73

def save(path)
  file = File.new(path, "wb")
  file.write(self.to_png)
  file.close
end

#styleObject



29
30
31
32
33
34
35
36
# File 'lib/ruby_docx/elements/ole_object.rb', line 29

def style
  element = @node.xpath(".//v:shape").first
  if element && element.attributes.keys.index("style")
    element.attributes["style"].value
  else
    nil
  end
end

#to_htmlObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ruby_docx/elements/ole_object.rb', line 83

def to_html
  if self.style
    if @link
      "<img src='#{@link}' data-latex=\"#{self.to_latex}\" data-mathml=\"#{self.to_mathml}\" style='#{self.style}' />"
    else
      "<img src='#{self.base64_data}' data-latex=\"#{self.to_latex}\" data-mathml=\"#{self.to_mathml}\" style='#{self.style}' />"
    end

  else
    if @link
      "<img src='#{@link}' data-latex=\"#{self.to_latex}\" data-mathml=\"#{self.to_mathml}\" style='height: 13px;' />"
    else
      "<img src='#{self.base64_data}' data-latex=\"#{self.to_latex}\" data-mathml=\"#{self.to_mathml}\" style='height: 13px;' />"
    end
  end

end

#to_latexObject



56
57
58
# File 'lib/ruby_docx/elements/ole_object.rb', line 56

def to_latex
  TeXMath.convert("#{self.to_mathml}", :from => :mathml, :to => :tex)
end

#to_mathmlObject



47
48
49
50
51
52
53
54
# File 'lib/ruby_docx/elements/ole_object.rb', line 47

def to_mathml
  io = StringIO.new(@zip.read("word/" + @path))
  # xml = Mathtype::Converter.new(io)
  # # puts xml.to_xml
  # xml.to_xml

  mathml = MathTypeToMathML::Converter.new(io).convert
end

#to_pngObject



60
61
62
63
# File 'lib/ruby_docx/elements/ole_object.rb', line 60

def to_png
  @image_path ||= Calculus::Expression.new("#{self.to_latex}", :parse => false).to_png
  File.read(@image_path)
end

#to_sObject



101
102
103
# File 'lib/ruby_docx/elements/ole_object.rb', line 101

def to_s
  self.to_latex.to_s
end