Skip to content

Commit c30e797

Browse files
committed
Use trait instead of istextmime
1 parent 557b67c commit c30e797

File tree

2 files changed

+69
-60
lines changed

2 files changed

+69
-60
lines changed

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,22 +2480,6 @@ the current exception (if called within a `catch` block).
24802480
"""
24812481
rethrow
24822482

2483-
"""
2484-
reprmime(mime, x)
2485-
2486-
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of `x` in the
2487-
requested `mime` type, as written by `show` (throwing a `MethodError` if no appropriate
2488-
`show` is available). An `AbstractString` is returned for MIME types with textual
2489-
representations (such as `"text/html"` or `"application/postscript"`), whereas binary data
2490-
is returned as `Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
2491-
treats a given `mime` type as text.)
2492-
2493-
As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
2494-
`Vector{UInt8}` (for binary MIME types), the `reprmime` function assumes that `x` is already
2495-
in the requested `mime` format and simply returns `x`.
2496-
"""
2497-
reprmime
2498-
24992483
"""
25002484
!(x)
25012485
@@ -2975,14 +2959,6 @@ Print strings in a color specified as a symbol.
29752959
"""
29762960
print_with_color
29772961

2978-
"""
2979-
stringmime(mime, x)
2980-
2981-
Returns an `AbstractString` containing the representation of `x` in the requested `mime`
2982-
type. This is similar to [`reprmime`](:func:`reprmime`) except that binary data is base64-encoded as an ASCII string.
2983-
"""
2984-
stringmime
2985-
29862962
"""
29872963
zero(x)
29882964

base/multimedia.jl

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,89 @@ mimewritable{mime}(::MIME{mime}, x) =
3333
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)
3434
mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x)
3535

36-
###########################################################################
37-
# MIME types are assumed to be binary data except for a set of types known
38-
# to be text data (possibly Unicode). istextmime(m) returns whether
39-
# m::MIME is text data, and reprmime(m, x) returns x written to either
40-
# a string (for text m::MIME) or a Vector{UInt8} (for binary m::MIME),
41-
# assuming the corresponding write_mime method exists. stringmime
42-
# is like reprmime except that it always returns a string, which in the
43-
# case of binary data is Base64-encoded.
44-
#
45-
# Also, if reprmime is passed a AbstractString for a text type or Vector{UInt8} for
46-
# a binary type, the argument is assumed to already be in the corresponding
47-
# format and is returned unmodified. This is useful so that raw data can be
48-
# passed to display(m::MIME, x).
36+
abstract MIMETypeType
37+
38+
immutable IsText <: MIMETypeType end
39+
immutable IsBytes <: MIMETypeType end
4940

5041
verbose_show(io, m, x) = show(IOContext(io,limit=false), m, x)
5142

43+
"""
44+
MIME types are assumed to be binary data except for a set of types known to be
45+
text data (possibly Unicode). `mimetypetype(m)` returns `Multimedia.IsText` or
46+
`Multimedia.IsBytes` for text or binary data respectively.
47+
"""
48+
mimetypetype{M}(::MIME{M}) =
49+
startswith(string(M), "text/") ? IsText() : IsBytes()
50+
51+
"""
52+
reprmime(mime, x)
53+
54+
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of
55+
`x` in the requested `mime` type, as written by `show` (throwing a
56+
`MethodError` if no appropriate `show` is available). An `AbstractString` is
57+
returned for MIME types with textual representations (such as `"text/html"` or
58+
`"application/postscript"`), whereas binary data is returned as
59+
`Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
60+
treats a given `mime` type as text.)
61+
62+
As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
63+
`Vector{UInt8}` (for binary MIME types), the `reprmime` function assumes that
64+
`x` is already in the requested `mime` format and simply returns `x`. This
65+
special case does not apply to the `"text/plain"` MIME type. This is useful so
66+
that raw data can be passed to `display(m::MIME, x)`.
67+
"""
68+
reprmime(m::MIME, x) = reprmime(mimetypetype(m), m, x)
69+
reprmime(::IsText, m::MIME, x) = sprint(verbose_show, m, x)
70+
71+
# strings are shown escaped for text/plain
72+
reprmime(::IsText, ::MIME, x::AbstractString) = x
73+
reprmime(::IsText, m::MIME"text/plain", x::AbstractString) =
74+
sprint(verbose_show, m, x)
75+
76+
function reprmime(::IsBytes, m::MIME, x)
77+
s = IOBuffer()
78+
verbose_show(s, m, x)
79+
takebuf_array(s)
80+
end
81+
reprmime(::IsBytes, m::MIME, x::Vector{UInt8}) = x
82+
83+
"""
84+
stringmime(mime, x)
85+
86+
Returns an `AbstractString` containing the representation of `x` in the
87+
requested `mime` type. This is similar to [`reprmime`](:func:`reprmime`) except
88+
that binary data is base64-encoded as an ASCII string.
89+
"""
90+
stringmime(m::MIME, x) = stringmime(mimetypetype(m), m, x)
91+
stringmime(::IsText, m::MIME, x) = reprmime(m, x)
92+
stringmime(::IsBytes, m::MIME, x) = base64encode(verbose_show, m, x)
93+
stringmime(::IsBytes, m::MIME, x::Vector{UInt8}) = base64encode(write, x)
94+
5295
macro textmime(mime)
96+
Base.depwarn(string("`@textmime mime` is deprecated; use ",
97+
"`Base.Multimedia.mimetypetype(::MIME{mime}) = ",
98+
"Base.Multimedia.IsText` instead."))
5399
quote
54-
mimeT = MIME{Symbol($mime)}
55-
# avoid method ambiguities with the general definitions below:
56-
# (Q: should we treat Vector{UInt8} as a String?)
57-
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_show, m, x)
58-
Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x)
59-
60-
Base.Multimedia.istextmime(::mimeT) = true
61-
if $(mime != "text/plain") # strings are shown escaped for text/plain
62-
Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x
63-
end
64-
Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_show, m, x)
65-
Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x)
100+
Base.Multimedia.mimetypetype(::MIME{$(Symbol(mime))}) =
101+
Base.Multimedia.IsText()
66102
end
67103
end
68104

69-
istextmime(::MIME) = false
70-
function reprmime(m::MIME, x)
71-
s = IOBuffer()
72-
verbose_show(s, m, x)
73-
takebuf_array(s)
74-
end
75-
reprmime(m::MIME, x::Vector{UInt8}) = x
76-
stringmime(m::MIME, x) = base64encode(verbose_show, m, x)
77-
stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x)
105+
istextmime(m::MIME) = isa(mimetypetype(m), IsText)
78106

79107
# it is convenient to accept strings instead of ::MIME
80108
istextmime(m::AbstractString) = istextmime(MIME(m))
81109
reprmime(m::AbstractString, x) = reprmime(MIME(m), x)
82110
stringmime(m::AbstractString, x) = stringmime(MIME(m), x)
83111

84-
for mime in ["application/atom+xml", "application/ecmascript", "application/javascript", "application/julia", "application/json", "application/postscript", "application/rdf+xml", "application/rss+xml", "application/x-latex", "application/xhtml+xml", "application/xml", "application/xml-dtd", "image/svg+xml", "model/vrml", "model/x3d+vrml", "model/x3d+xml", "text/calendar", "text/cmd", "text/css", "text/csv", "text/html", "text/javascript", "text/latex", "text/markdown", "text/n3", "text/plain", "text/richtext", "text/sgml", "text/tab-separated-values", "text/vcard", "text/vnd.graphviz", "text/x-setext", "text/x-vcalendar", "text/x-vcard", "text/xml"]
85-
@eval @textmime $mime
112+
for mime in ["application/atom+xml", "application/ecmascript",
113+
"application/javascript", "application/julia", "application/json",
114+
"application/postscript", "application/rdf+xml", "application/rss+xml",
115+
"application/x-latex", "application/xhtml+xml", "application/xml",
116+
"application/xml-dtd", "image/svg+xml", "model/vrml", "model/x3d+vrml",
117+
"model/x3d+xml"]
118+
mimetypetype(::MIME{Symbol(mime)}) = IsText()
86119
end
87120

88121
###########################################################################

0 commit comments

Comments
 (0)