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],
"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] 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_registry(data, obj, exact_mime, fuzzy_mime)
default_renderers = if obj.respond_to?(:to_iruby_mimebundle)
{"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?
next if mime && data.key?(mime)
method = Array(methods).find {|m| obj.respond_to?(m) }
next if method.nil?
result = obj.send(method)
case mime
when nil case result
when nil
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
if exact_mime && !data.key?(exact_mime)
data[exact_mime] = protect(exact_mime, obj)
end
data
end
|