Skip to content

Commit a4f717a

Browse files
committed
Do not call default renderers when to_iruby_mimebundle is available
1 parent 8df37a0 commit a4f717a

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

lib/iruby/display.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,23 @@ def display(obj, options = {})
4040
raise 'Invalid mime type' unless exact_mime.include?('/')
4141
end
4242

43-
data = render_mimebundle(obj, exact_mime, fuzzy_mime)
43+
data = if obj.respond_to?(:to_iruby_mimebundle)
44+
render_mimebundle(obj, exact_mime, fuzzy_mime)
45+
else
46+
{}
47+
end
4448

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

4852
# Render by to_xxx methods
49-
DEFAULT_MIME_TYPE_FORMAT_METHODS.each do |mime, methods|
53+
default_renderers = if obj.respond_to?(:to_iruby_mimebundle)
54+
# Do not use Hash#slice for Ruby < 2.5
55+
{"text/plain" => DEFAULT_MIME_TYPE_FORMAT_METHODS["text/plain"]}
56+
else
57+
DEFAULT_MIME_TYPE_FORMAT_METHODS
58+
end
59+
default_renderers.each do |mime, methods|
5060
next if mime.nil? && !data.empty? # for to_iruby
5161

5262
next if mime && data.key?(mime) # do not overwrite
@@ -109,13 +119,11 @@ def ascii?(mime)
109119

110120
private def render_mimebundle(obj, exact_mime, fuzzy_mime)
111121
data = {}
112-
if obj.respond_to?(:to_iruby_mimebundle)
113-
include_mime = [exact_mime].compact
114-
formats, metadata = obj.to_iruby_mimebundle(include: include_mime)
115-
formats.each do |mime, value|
116-
if fuzzy_mime.nil? || mime.include?(fuzzy_mime)
117-
data[mime] = value
118-
end
122+
include_mime = [exact_mime].compact
123+
formats, metadata = obj.to_iruby_mimebundle(include: include_mime)
124+
formats.each do |mime, value|
125+
if fuzzy_mime.nil? || mime.include?(fuzzy_mime)
126+
data[mime] = value
119127
end
120128
end
121129
data

test/iruby/display_test.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,12 @@ class << @object
5959
def to_iruby_mimebundle(include: [])
6060
@to_iruby_mimebundle_called = true
6161
mimes = if include.empty?
62-
["text/html", "text/markdown", "application/json"]
62+
["text/markdown", "application/json"]
6363
else
6464
include
6565
end
6666
formats = mimes.map { |mime|
6767
result = case mime
68-
when "text/html"
69-
"<i>html</i>"
7068
when "text/markdown"
7169
"**markdown**"
7270
when "application/json"
@@ -170,7 +168,6 @@ def setup
170168
def test_display
171169
assert_iruby_display({
172170
result: {
173-
"text/html" => "<i>html</i>",
174171
"text/markdown" => "**markdown**",
175172
"application/json" => %Q[{"mimebundle": "json"}],
176173
"text/plain" => "!!! inspect !!!"

0 commit comments

Comments
 (0)