Class: IRuby::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/iruby/display.rb

Constant Summary collapse

SUPPORTED_MIMES =
%w(
text/plain
text/html
text/latex
application/json
application/javascript
image/png
image/jpeg
image/svg+xml)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, options) ⇒ Display

Returns a new instance of Display.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/iruby/display.rb', line 15

def initialize(obj, options)
  @data = { 'text/plain' => obj.inspect }
  if options[:mime]
    add(options[:mime], obj)
  elsif obj.respond_to?(:to_iruby)
    add(*obj.to_iruby)
  elsif obj.respond_to?(:to_html)
    add('text/html', obj.to_html)
  elsif obj.respond_to?(:to_svg)
    obj.render if defined?(Rubyvis) && Rubyvis::Mark === obj
    add('image/svg+xml', obj.to_svg)
  elsif obj.respond_to?(:to_latex)
    add('text/latex', obj.to_latex)
  elsif defined?(Gruff::Base) && Gruff::Base === obj
    add('image/png', obj.to_blob)
  elsif (defined?(Magick::Image) && Magick::Image === obj) ||
       (defined?(MiniMagick::Image) && MiniMagick::Image === obj)
    format = obj.format || 'PNG'
    add(format == 'PNG' ? 'image/png' : 'image/jpeg', obj.to_blob {|i| i.format = format })
  elsif obj.respond_to?(:path) && File.readable?(obj.path)
    mime = MimeMagic.by_path(obj.path).to_s
    add(mime, File.read(obj.path)) if SUPPORTED_MIMES.include?(mime)
  elsif defined?(Gnuplot::Plot) && Gnuplot::Plot === obj
    Tempfile.open('plot') do |f|
      obj.terminal 'svg enhanced'
      obj.output f.path
      Gnuplot.open do |io|
        io << obj.to_gplot
        io << obj.store_datasets
      end
      add('image/svg+xml', File.read(f.path))
    end
  elsif defined?(Matrix) && Matrix === obj
    add('text/latex', format_matrix(obj, obj.row_count, obj.column_count))
  elsif defined?(GSL::Matrix) && GSL::Matrix === obj
    add('text/latex', format_matrix(obj, obj.size1, obj.size2))
  elsif defined?(GSL::Vector) && GSL::Vector === obj
    add('text/latex', format_vector(obj.to_a))
  elsif defined?(GSL::Complex) && GSL::Complex === obj
    add('text/latex', "$#{obj.re}+#{obj.im}i$")
  elsif defined?(NArray) && NArray === obj
    add('text/latex', obj.dim == 2 ?
        format_matrix(obj.transpose, obj.shape[1], obj.shape[0]) :
        format_vector(obj.to_a))
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/iruby/display.rb', line 3

def data
  @data
end