Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/execute_request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Base.Libc: flush_cstdio
import Pkg

Base.showable(a::Vector{MIME}, x) = any(m -> showable(m, x), a)
Base.showable(a::AbstractVector{<:MIME}, x) = any(m -> showable(m, x), a)

"""
A vector of MIME types (or vectors of MIME types) that IJulia will try to
Expand All @@ -20,7 +20,7 @@ is displayed. Since markdown and latex are specified within a sub-vector, IJulia
will always try to render "text/markdown", and will only try to render
"text/latex" if markdown isn't possible.
"""
const ijulia_mime_types = Vector{Union{MIME, Vector{MIME}}}([
const ijulia_mime_types = Vector{Union{MIME, AbstractVector{MIME}}}([
MIME("text/plain"),
MIME("image/svg+xml"),
[MIME("image/png"),MIME("image/jpeg")],
Expand All @@ -39,13 +39,13 @@ MIME types that when rendered (via stringmime) return JSON data. See
This is necessary to embed the JSON as is in the displaydata bundle (rather than
as stringify'd JSON).
"""
const ijulia_jsonmime_types = Vector{Union{MIME, Vector{MIME}}}([
const ijulia_jsonmime_types = Vector{Union{MIME, AbstractVector{MIME}}}([
[MIME("application/vnd.vegalite.v2+json"), MIME("application/vnd.vega.v3+json")],
MIME("application/vnd.dataresource+json"),
])

register_mime(x::Union{MIME, Vector{MIME}}) = push!(ijulia_mime_types, x)
register_jsonmime(x::Union{MIME, Vector{MIME}}) = push!(ijulia_jsonmime_types, x)
register_mime(x::Union{M, AbstractVector{M}}) where {M <: MIME} = push!(ijulia_mime_types, x)
register_jsonmime(x::Union{M, AbstractVector{M}}) where {M <: MIME} = push!(ijulia_jsonmime_types, x)

include("magics.jl")

Expand All @@ -59,7 +59,7 @@ Generate the preferred MIME representation of x.
Returns a tuple with the selected MIME type and the representation of the data
using that MIME type.
"""
function display_mimestring(mime_array::Vector{MIME}, x)
function display_mimestring(mime_array::AbstractVector{MIME}, x)
for m in mime_array
if showable(mime_array, x)
return display_mimestring(m, x)
Expand All @@ -76,7 +76,7 @@ Generate the preferred json-MIME representation of x.
Returns a tuple with the selected MIME type and the representation of the data
using that MIME type (as a `JSONText`).
"""
function display_mimejson(mime_array::Vector{MIME}, x)
function display_mimejson(mime_array::AbstractVector{MIME}, x)
for m in mime_array
if showable(mime_array, x)
return display_mimejson(m, x)
Expand Down
12 changes: 12 additions & 0 deletions test/execute_request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ end
Base.show(io, ::JSON_MIME_TYPE, x::FriendlyData) = write(io, JSON.json(Dict("name" => x.name)))
IJulia.register_jsonmime(JSON_MIME)

FRIENDLY_MIME_TYPE_1 = MIME"application/vnd.ijulia.friendly-text-1"
FRIENDLY_MIME_TYPE_2 = MIME"application/vnd.ijulia.friendly-text-2"
FRIENDLY_MIME_1 = FRIENDLY_MIME_TYPE_1()
FRIENDLY_MIME_2 = FRIENDLY_MIME_TYPE_2()
FRIENDLY_MIME_TYPE_UNION = Union{FRIENDLY_MIME_TYPE_1, FRIENDLY_MIME_TYPE_2}
Base.Multimedia.istextmime(::FRIENDLY_MIME_TYPE_UNION) = true
Base.show(io, ::FRIENDLY_MIME_TYPE_UNION, x::FriendlyData) = write(io, "Hello, $(x.name)!")
IJulia.register_mime([FRIENDLY_MIME_1, FRIENDLY_MIME_2])

# We stringify then re-parse the dict so that JSONText's are parsed as
# actual JSON objects and we can index into them.
data = JSON.parse(JSON.json(IJulia.display_dict(friend)))
@test data[string(FRIENDLY_MIME)] == "Hello, world!"
@test data[string(BINARY_MIME)] == base64encode("Hello, world!")
@test data[string(JSON_MIME)]["name"] == "world"
@test data[string(FRIENDLY_MIME_1)] == "Hello, world!"
@test !haskey(data, string(FRIENDLY_MIME_2))


end