Skip to content

Commit

Permalink
Use trait instead of istextmime
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Sep 13, 2016
1 parent 557b67c commit c30e797
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 60 deletions.
24 changes: 0 additions & 24 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2480,22 +2480,6 @@ the current exception (if called within a `catch` block).
"""
rethrow

"""
reprmime(mime, x)
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.)
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`.
"""
reprmime

"""
!(x)
Expand Down Expand Up @@ -2975,14 +2959,6 @@ Print strings in a color specified as a symbol.
"""
print_with_color

"""
stringmime(mime, x)
Returns an `AbstractString` containing the representation of `x` in the requested `mime`
type. This is similar to [`reprmime`](:func:`reprmime`) except that binary data is base64-encoded as an ASCII string.
"""
stringmime

"""
zero(x)
Expand Down
105 changes: 69 additions & 36 deletions base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,89 @@ mimewritable{mime}(::MIME{mime}, x) =
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)
mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x)

###########################################################################
# MIME types are assumed to be binary data except for a set of types known
# to be text data (possibly Unicode). istextmime(m) returns whether
# m::MIME is text data, and reprmime(m, x) returns x written to either
# a string (for text m::MIME) or a Vector{UInt8} (for binary m::MIME),
# assuming the corresponding write_mime method exists. stringmime
# is like reprmime except that it always returns a string, which in the
# case of binary data is Base64-encoded.
#
# Also, if reprmime is passed a AbstractString for a text type or Vector{UInt8} for
# a binary type, the argument is assumed to already be in the corresponding
# format and is returned unmodified. This is useful so that raw data can be
# passed to display(m::MIME, x).
abstract MIMETypeType

immutable IsText <: MIMETypeType end
immutable IsBytes <: MIMETypeType end

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

"""
MIME types are assumed to be binary data except for a set of types known to be
text data (possibly Unicode). `mimetypetype(m)` returns `Multimedia.IsText` or
`Multimedia.IsBytes` for text or binary data respectively.
"""
mimetypetype{M}(::MIME{M}) =
startswith(string(M), "text/") ? IsText() : IsBytes()

"""
reprmime(mime, x)
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.)
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)`.
"""
reprmime(m::MIME, x) = reprmime(mimetypetype(m), m, x)
reprmime(::IsText, m::MIME, x) = sprint(verbose_show, m, x)

# strings are shown escaped for text/plain
reprmime(::IsText, ::MIME, x::AbstractString) = x
reprmime(::IsText, m::MIME"text/plain", x::AbstractString) =
sprint(verbose_show, m, x)

function reprmime(::IsBytes, m::MIME, x)
s = IOBuffer()
verbose_show(s, m, x)
takebuf_array(s)
end
reprmime(::IsBytes, m::MIME, x::Vector{UInt8}) = x

"""
stringmime(mime, x)
Returns an `AbstractString` containing the representation of `x` in the
requested `mime` type. This is similar to [`reprmime`](:func:`reprmime`) except
that binary data is base64-encoded as an ASCII string.
"""
stringmime(m::MIME, x) = stringmime(mimetypetype(m), m, x)
stringmime(::IsText, m::MIME, x) = reprmime(m, x)
stringmime(::IsBytes, m::MIME, x) = base64encode(verbose_show, m, x)
stringmime(::IsBytes, m::MIME, x::Vector{UInt8}) = base64encode(write, x)

macro textmime(mime)
Base.depwarn(string("`@textmime mime` is deprecated; use ",
"`Base.Multimedia.mimetypetype(::MIME{mime}) = ",
"Base.Multimedia.IsText` instead."))
quote
mimeT = MIME{Symbol($mime)}
# avoid method ambiguities with the general definitions below:
# (Q: should we treat Vector{UInt8} as a String?)
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_show, m, x)
Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x)

Base.Multimedia.istextmime(::mimeT) = true
if $(mime != "text/plain") # strings are shown escaped for text/plain
Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x
end
Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_show, m, x)
Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x)
Base.Multimedia.mimetypetype(::MIME{$(Symbol(mime))}) =
Base.Multimedia.IsText()
end
end

istextmime(::MIME) = false
function reprmime(m::MIME, x)
s = IOBuffer()
verbose_show(s, m, x)
takebuf_array(s)
end
reprmime(m::MIME, x::Vector{UInt8}) = x
stringmime(m::MIME, x) = base64encode(verbose_show, m, x)
stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x)
istextmime(m::MIME) = isa(mimetypetype(m), IsText)

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

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"]
@eval @textmime $mime
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"]
mimetypetype(::MIME{Symbol(mime)}) = IsText()
end

###########################################################################
Expand Down

0 comments on commit c30e797

Please sign in to comment.