Module: IRuby::Display

Defined in:
lib/iruby/display.rb

Defined Under Namespace

Modules: Registry Classes: FormatMatcher, Renderer, Representation, RespondToFormatMatcher, TypeFormatMatcher

Constant Summary collapse

DEFAULT_MIME_TYPE_FORMAT_METHODS =
{
  "text/html" => :to_html,
  "text/markdown" => :to_markdown,
  "image/svg+xml" => :to_svg,
  "image/png" => :to_png,
  "appliation/pdf" => :to_pdf,
  "image/jpeg" => :to_jpeg,
  "text/latex" => [:to_latex, :to_tex],
  # NOTE: Do not include the entry of "application/json" because
  #       all objects can respond to `to_json` due to json library
  # "application/json" => :to_json,
  "application/javascript" => :to_javascript,
  nil => :to_iruby,
  "text/plain" => :inspect
}.freeze

Class Method Summary collapse

Class Method Details

.clear_output(wait = false) ⇒ Object



95
96
97
# File 'lib/iruby/display.rb', line 95

def clear_output(wait = false)
  IRuby::Kernel.instance.session.send(:publish, :clear_output, wait: wait)
end

.convert(obj, options) ⇒ Object



23
24
25
# File 'lib/iruby/display.rb', line 23

def convert(obj, options)
  Representation.new(obj, options)
end

.display(obj, options = {}) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/iruby/display.rb', line 28

def display(obj, options = {})
  obj = convert(obj, options)
  options = obj.options
  obj = obj.object

  fuzzy_mime = options[:format] # Treated like a fuzzy mime type
  unless !fuzzy_mime || String === fuzzy_mime
    raise 'Invalid argument :format'
  end

  if exact_mime = options[:mime]
    raise 'Invalid argument :mime' unless String === exact_mime
    raise 'Invalid mime type' unless exact_mime.include?('/')
  end

  data = if obj.respond_to?(:to_iruby_mimebundle)
           render_mimebundle(obj, exact_mime, fuzzy_mime)
         else
           {}
         end

  # Render by additional formatters
  render_by_registry(data, obj, exact_mime, fuzzy_mime)

  # Render by to_xxx methods
  default_renderers = if obj.respond_to?(:to_iruby_mimebundle)
                        # Do not use Hash#slice for Ruby < 2.5
                        {"text/plain" => DEFAULT_MIME_TYPE_FORMAT_METHODS["text/plain"]}
                      else
                        DEFAULT_MIME_TYPE_FORMAT_METHODS
                      end
  default_renderers.each do |mime, methods|
    next if mime.nil? && !data.empty? # for to_iruby

    next if mime && data.key?(mime)   # do not overwrite

    method = Array(methods).find {|m| obj.respond_to?(m) }
    next if method.nil?

    result = obj.send(method)
    case mime
    when nil # to_iruby
      case result
      when nil
        # do nothing
        next
      when Array
        mime, result = result
      else
        warn(("Ignore the result of to_iruby method of %p because " +
              "it does not return a pair of mime-type and formatted representation") % obj)
        next
      end
    end
    data[mime] = result
  end

  # As a last resort, interpret string representation of the object
  # as the given mime type.
  if exact_mime && !data.key?(exact_mime)
    data[exact_mime] = protect(exact_mime, obj)
  end

  data
end