Skip to content

Commit bc9b6ef

Browse files
TotalVerbstevengj
authored andcommitted
Make application/julia a text MIME (#18441)
* Make application/julia a text MIME * Use trait instead of istextmime * Add a @pure annotation * Run genstdlib * Move deprecation to deprecated.jl * Use boolean instead of traits * Fix deprecated @textmime * Fix @textmime depwarn message * Undo accidental re-helpdbification
1 parent d2db55a commit bc9b6ef

File tree

4 files changed

+66
-62
lines changed

4 files changed

+66
-62
lines changed

base/deprecated.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,4 +1009,14 @@ export @vectorize_1arg, @vectorize_2arg
10091009
@deprecate abs(M::SymTridiagonal) abs.(M)
10101010
@deprecate abs(x::AbstractSparseVector) abs.(x)
10111011

1012+
# Deprecate @textmime into the Multimedia module, #18441
1013+
eval(Multimedia, :(macro textmime(mime)
1014+
Base.depwarn(string("`@textmime \"mime\"` is deprecated; use ",
1015+
"`Base.Multimedia.istextmime(::MIME\"mime\") = true` instead"
1016+
), :textmime)
1017+
quote
1018+
Base.Multimedia.istextmime(::MIME{$(Meta.quot(Symbol(mime)))}) = true
1019+
end
1020+
end))
1021+
10121022
# End deprecations scheduled for 0.6

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,22 +2624,6 @@ the current exception (if called within a `catch` block).
26242624
"""
26252625
rethrow
26262626

2627-
"""
2628-
reprmime(mime, x)
2629-
2630-
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of `x` in the
2631-
requested `mime` type, as written by `show` (throwing a `MethodError` if no appropriate
2632-
`show` is available). An `AbstractString` is returned for MIME types with textual
2633-
representations (such as `"text/html"` or `"application/postscript"`), whereas binary data
2634-
is returned as `Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
2635-
treats a given `mime` type as text.)
2636-
2637-
As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
2638-
`Vector{UInt8}` (for binary MIME types), the `reprmime` function assumes that `x` is already
2639-
in the requested `mime` format and simply returns `x`.
2640-
"""
2641-
reprmime
2642-
26432627
"""
26442628
!(x)
26452629
@@ -3263,13 +3247,6 @@ processes completed successfully.
32633247
"""
32643248
exit
32653249

3266-
"""
3267-
istextmime(m::MIME)
3268-
3269-
Determine whether a MIME type is text data.
3270-
"""
3271-
istextmime
3272-
32733250
"""
32743251
skipchars(stream, predicate; linecomment::Char)
32753252

base/multimedia.jl

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,73 @@ 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).
49-
5036
verbose_show(io, m, x) = show(IOContext(io,limit=false), m, x)
5137

52-
macro textmime(mime)
53-
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)
66-
end
67-
end
68-
69-
istextmime(::MIME) = false
70-
function reprmime(m::MIME, x)
38+
"""
39+
reprmime(mime, x)
40+
41+
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of
42+
`x` in the requested `mime` type, as written by `show` (throwing a
43+
`MethodError` if no appropriate `show` is available). An `AbstractString` is
44+
returned for MIME types with textual representations (such as `"text/html"` or
45+
`"application/postscript"`), whereas binary data is returned as
46+
`Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
47+
treats a given `mime` type as text.)
48+
49+
As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
50+
`Vector{UInt8}` (for binary MIME types), the `reprmime` function assumes that
51+
`x` is already in the requested `mime` format and simply returns `x`. This
52+
special case does not apply to the `"text/plain"` MIME type. This is useful so
53+
that raw data can be passed to `display(m::MIME, x)`.
54+
"""
55+
reprmime(m::MIME, x) = istextmime(m) ? _textreprmime(m, x) : _binreprmime(m, x)
56+
57+
# strings are shown escaped for text/plain
58+
_textreprmime(m::MIME, x) = sprint(verbose_show, m, x)
59+
_textreprmime(::MIME, x::AbstractString) = x
60+
_textreprmime(m::MIME"text/plain", x::AbstractString) =
61+
sprint(verbose_show, m, x)
62+
63+
function _binreprmime(m::MIME, x)
7164
s = IOBuffer()
7265
verbose_show(s, m, x)
7366
takebuf_array(s)
7467
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)
68+
_binreprmime(m::MIME, x::Vector{UInt8}) = x
69+
70+
"""
71+
stringmime(mime, x)
72+
73+
Returns an `AbstractString` containing the representation of `x` in the
74+
requested `mime` type. This is similar to [`reprmime`](:func:`reprmime`) except
75+
that binary data is base64-encoded as an ASCII string.
76+
"""
77+
stringmime(m::MIME, x) = istextmime(m) ? reprmime(m, x) : _binstringmime(m, x)
78+
79+
_binstringmime(m::MIME, x) = base64encode(verbose_show, m, x)
80+
_binstringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x)
81+
82+
"""
83+
istextmime(m::MIME)
84+
85+
Determine whether a MIME type is text data. MIME types are assumed to be binary
86+
data except for a set of types known to be text data (possibly Unicode).
87+
"""
88+
istextmime(m::MIME) = startswith(string(m), "text/")
7889

7990
# it is convenient to accept strings instead of ::MIME
8091
istextmime(m::AbstractString) = istextmime(MIME(m))
8192
reprmime(m::AbstractString, x) = reprmime(MIME(m), x)
8293
stringmime(m::AbstractString, x) = stringmime(MIME(m), x)
8394

84-
for mime in ["text/vnd.graphviz", "text/latex", "text/calendar", "text/n3", "text/richtext", "text/x-setext", "text/sgml", "text/tab-separated-values", "text/x-vcalendar", "text/x-vcard", "text/cmd", "text/css", "text/csv", "text/html", "text/javascript", "text/markdown", "text/plain", "text/vcard", "text/xml", "application/atom+xml", "application/ecmascript", "application/json", "application/rdf+xml", "application/rss+xml", "application/xml-dtd", "application/postscript", "image/svg+xml", "application/x-latex", "application/xhtml+xml", "application/javascript", "application/xml", "model/x3d+xml", "model/x3d+vrml", "model/vrml"]
85-
@eval @textmime $mime
95+
for mime in ["application/atom+xml", "application/ecmascript",
96+
"application/javascript", "application/julia",
97+
"application/json", "application/postscript",
98+
"application/rdf+xml", "application/rss+xml",
99+
"application/x-latex", "application/xhtml+xml", "application/xml",
100+
"application/xml-dtd", "image/svg+xml", "model/vrml",
101+
"model/x3d+vrml", "model/x3d+xml"]
102+
istextmime(::MIME{Symbol(mime)}) = true
86103
end
87104

88105
###########################################################################

doc/stdlib/io-network.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ Julia environments (such as the IPython-based IJulia notebook).
782782
783783
Returns an ``AbstractString`` or ``Vector{UInt8}`` containing the representation of ``x`` in the requested ``mime`` type, as written by ``show`` (throwing a ``MethodError`` if no appropriate ``show`` is available). An ``AbstractString`` is returned for MIME types with textual representations (such as ``"text/html"`` or ``"application/postscript"``\ ), whereas binary data is returned as ``Vector{UInt8}``\ . (The function ``istextmime(mime)`` returns whether or not Julia treats a given ``mime`` type as text.)
784784

785-
As a special case, if ``x`` is an ``AbstractString`` (for textual MIME types) or a ``Vector{UInt8}`` (for binary MIME types), the ``reprmime`` function assumes that ``x`` is already in the requested ``mime`` format and simply returns ``x``\ .
785+
As a special case, if ``x`` is an ``AbstractString`` (for textual MIME types) or a ``Vector{UInt8}`` (for binary MIME types), the ``reprmime`` function assumes that ``x`` is already in the requested ``mime`` format and simply returns ``x``\ . This special case does not apply to the ``"text/plain"`` MIME type. This is useful so that raw data can be passed to ``display(m::MIME, x)``\ .
786786

787787
.. function:: stringmime(mime, x)
788788

0 commit comments

Comments
 (0)