Skip to content

Commit 1dfd237

Browse files
author
cnliao
committed
improve upon JuliaLang#755 and add new mime for vega4
1 parent 048f73b commit 1dfd237

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

src/execute_request.jl

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import Base.Libc: flush_cstdio
66
import Pkg
77

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

1010
"""
1111
A vector of MIME types (or vectors of MIME types) that IJulia will try to
@@ -20,9 +20,19 @@ is displayed. Since markdown and latex are specified within a sub-vector, IJulia
2020
will always try to render "text/markdown", and will only try to render
2121
"text/latex" if markdown isn't possible.
2222
"""
23-
const ijulia_mime_types = Vector{Union{MIME, AbstractVector{MIME}}}()
23+
const ijulia_mime_types = Vector{Union{MIME, Vector{<:MIME}}}([
24+
MIME("text/plain"),
25+
MIME("image/svg+xml"),
26+
[MIME("image/png"),MIME("image/jpeg")],
27+
[
28+
MIME("text/markdown"),
29+
MIME("text/html"),
30+
MIME("text/latex"), # Jupyter expects this
31+
MIME("application/x-latex"), # but this is more standard?
32+
],
33+
])
2434

25-
register_mime(x::Union{M, AbstractVector{M}}) where {M <: MIME} = push!(ijulia_mime_types, x)
35+
register_mime(x::Union{M, Vector{M}}) where {M <: MIME} = push!(ijulia_mime_types, x)
2636

2737
include("magics.jl")
2838

@@ -36,7 +46,15 @@ Generate the preferred MIME representation of x.
3646
Returns a tuple with the selected MIME type and the representation of the data
3747
using that MIME type.
3848
"""
39-
function display_mimestring(mime_array::AbstractVector{MIME}, x)
49+
function display_mimestring end
50+
51+
abstract type MIMEStringType end
52+
struct RawMIMEString <: MIMEStringType end
53+
mimestringtype(m::MIME) = RawMIMEString
54+
display_mimestring(::Type{T}, m::MIME, x) where{T<:MIMEStringType} = display_mimestring(T(), m, x)
55+
display_mimestring(::RawMIMEString, m::MIME, x) = (m, limitstringmime(m, x))
56+
display_mimestring(m::MIME, x) = display_mimestring(mimestringtype(m), m, x)
57+
function display_mimestring(mime_array::Vector{<:MIME}, x)
4058
for m in mime_array
4159
if showable(mime_array, x)
4260
return display_mimestring(m, x)
@@ -45,56 +63,41 @@ function display_mimestring(mime_array::AbstractVector{MIME}, x)
4563
error("No displayable MIME types in mime array.")
4664
end
4765

48-
abstract type MIMEStringType end
49-
display_mimestring(m::MIME, x) = display_mimestring(m, mimestringtype(m), x)
50-
5166
"""
5267
If false then data of the mime type should be sent to ipython in b64-encoded string.
5368
If true then it will be sent as is.
5469
Defaults to the value of istextmime.
70+
Use a private name to avoid type piracy.
5571
"""
5672
_istextmime(m::MIME) = istextmime(m)
5773

58-
struct RawMIMEString <: MIMEStringType end
59-
for m in [
60-
MIME("text/plain"),
61-
MIME("image/svg+xml"),
62-
[MIME("image/png"),MIME("image/jpeg")],
63-
[
64-
MIME("text/markdown"),
65-
MIME("text/html"),
66-
MIME("text/latex"), # Jupyter expects this
67-
MIME("application/x-latex"), # but this is more standard?
68-
],
69-
]
70-
register_mime(m)
71-
if m isa MIME
72-
@eval mimestringtype(::$m) = RawMIMEString
73-
else
74-
for _m in m
75-
@eval mimestringtype(::$m) = RawMIMEString
76-
end
77-
end
78-
end
79-
display_mimestring(m::MIME, ::RawMIMEString, x) = (m, limitstringmime(m, x))
80-
74+
"""
75+
To add a new MIME type that require special treatment before sending to ipython,
76+
follow the example of JSONMIMEString to do the following:
77+
0. define a singleton type inherited from MIMEStringType. This is a trait type.
78+
1. register_mime
79+
2. specialize mimestringtype on the new MIME returning the new trait.
80+
3. (optinal) specialize _istextmime to return true if the new MIME should be send as text.
81+
4. specialize display_mimestring to implement your special treatment.
82+
"""
8183
struct JSONMIMEString <: MIMEStringType end
82-
for m in [
83-
[MIME("application/vnd.vegalite.v2+json"), MIME("application/vnd.vega.v3+json")],
84-
MIME("application/vnd.dataresource+json")
85-
]
86-
register_mime(m)
84+
for mime in [[MIME("application/vnd.vegalite.v2+json"), MIME("application/vnd.vega.v3+json")],
85+
MIME("application/vnd.vega.v4+json"),
86+
MIME("application/vnd.dataresource+json")]
87+
register_mime(mime)
88+
m = typeof(mime)
8789
if m <: MIME
8890
@eval mimestringtype(::$m) = JSONMIMEString
8991
@eval _istextmime(::$m) = true
9092
else
91-
for _m in m
93+
for _m in mime
94+
m = typeof(_m)
9295
@eval mimestringtype(::$m) = JSONMIMEString
9396
@eval _istextmime(::$m) = true
9497
end
9598
end
9699
end
97-
display_mimestring(m::MIME, ::JSONMIMEString, x) = (m, JSON.JSONText(limitstringmime(m, x)))
100+
display_mimestring(::JSONMIMEString, m::MIME, x) = (m, JSON.JSONText(limitstringmime(m, x)))
98101

99102
"""
100103
Generate a dictionary of `mime_type => data` pairs for all registered MIME

0 commit comments

Comments
 (0)