33module Multimedia
44
55export AbstractDisplay, display, pushdisplay, popdisplay, displayable, redisplay,
6- MIME, @MIME_str , reprmime, stringmime, istextmime,
6+ MIME, @MIME_str , istextmime,
77 mimewritable, TextDisplay
88
99# ##########################################################################
@@ -15,8 +15,7 @@ export AbstractDisplay, display, pushdisplay, popdisplay, displayable, redisplay
1515# struct MIME{mime} end
1616# macro MIME_str(s)
1717import Base: MIME, @MIME_str
18- import Base64
19- import Base: show, print, string, convert
18+ import Base: show, print, string, convert, repr
2019MIME (s) = MIME {Symbol(s)} ()
2120show (io:: IO , :: MIME{mime} ) where {mime} = print (io, " MIME type " , string (mime))
2221print (io:: IO , :: MIME{mime} ) where {mime} = print (io, mime)
@@ -79,59 +78,60 @@ show(stream, mime, x)
7978show (io:: IO , m:: AbstractString , x) = show (io, MIME (m), x)
8079mimewritable (m:: AbstractString , x) = mimewritable (MIME (m), x)
8180
82- verbose_show (io, m, x) = show (IOContext (io, :limit => false ), m, x)
83-
8481"""
85- reprmime (mime, x)
82+ repr (mime, x; context=nothing )
8683
8784Returns an `AbstractString` or `Vector{UInt8}` containing the representation of
88- `x` in the requested `mime` type, as written by [`show`](@ref) (throwing a
85+ `x` in the requested `mime` type, as written by [`show(io, mime, x) `](@ref) (throwing a
8986[`MethodError`](@ref) if no appropriate `show` is available). An `AbstractString` is
9087returned for MIME types with textual representations (such as `"text/html"` or
9188`"application/postscript"`), whereas binary data is returned as
9289`Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
9390treats a given `mime` type as text.)
9491
92+ The optional keyword argument `context` can be set to `:key=>value` pair
93+ or an `IO` or [`IOContext`](@ref) object whose attributes are used for the I/O
94+ stream passed to `show`.
95+
9596As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
96- `Vector{UInt8}` (for binary MIME types), the `reprmime ` function assumes that
97+ `Vector{UInt8}` (for binary MIME types), the `repr ` function assumes that
9798`x` is already in the requested `mime` format and simply returns `x`. This
9899special case does not apply to the `"text/plain"` MIME type. This is useful so
99100that raw data can be passed to `display(m::MIME, x)`.
100101
102+ In particular, `repr("text/plain", x)` is typically a "pretty-printed" version
103+ of `x` designed for human consumption. See also [`repr(x)`](@ref) to instead
104+ return a string corresponding to [`show(x)`](@ref) that may be closer to how
105+ the value of `x` would be entered in Julia.
106+
101107# Examples
102108```jldoctest
103109julia> A = [1 2; 3 4];
104110
105- julia> reprmime ("text/plain", A)
111+ julia> repr ("text/plain", A)
106112"2×2 Array{Int64,2}:\\ n 1 2\\ n 3 4"
107113```
108114"""
109- reprmime (m:: MIME , x) = istextmime (m) ? _textreprmime (m, x) : _binreprmime (m, x)
115+ repr (m:: MIME , x; context= nothing ) = istextmime (m) ? _textrepr (m, x, context) : _binrepr (m, x, context)
116+ repr (m:: AbstractString , x; context= nothing ) = repr (MIME (m), x; context= context)
110117
111118# strings are shown escaped for text/plain
112- _textreprmime (m:: MIME , x) = sprint (verbose_show, m, x)
113- _textreprmime (:: MIME , x:: AbstractString ) = x
114- _textreprmime (m:: MIME"text/plain" , x:: AbstractString ) =
115- sprint (verbose_show, m, x)
119+ _textrepr (m:: MIME , x, context) = String (__binrepr (m, x, context))
120+ _textrepr (:: MIME , x:: AbstractString , context) = x
121+ _textrepr (m:: MIME"text/plain" , x:: AbstractString , context) = String (__binrepr (m, x, context))
116122
117- function _binreprmime (m:: MIME , x)
123+
124+ function __binrepr (m:: MIME , x, context)
118125 s = IOBuffer ()
119- verbose_show (s, m, x)
126+ if context === nothing
127+ show (s, m, x)
128+ else
129+ show (IOContext (s, context), m, x)
130+ end
120131 take! (s)
121132end
122- _binreprmime (m:: MIME , x:: Vector{UInt8} ) = x
123-
124- """
125- stringmime(mime, x)
126-
127- Returns an `AbstractString` containing the representation of `x` in the
128- requested `mime` type. This is similar to [`reprmime`](@ref) except
129- that binary data is base64-encoded as an ASCII string.
130- """
131- stringmime (m:: MIME , x) = istextmime (m) ? reprmime (m, x) : _binstringmime (m, x)
132-
133- _binstringmime (m:: MIME , x) = Base64. base64encode (verbose_show, m, x)
134- _binstringmime (m:: MIME , x:: Vector{UInt8} ) = Base64. base64encode (write, x)
133+ _binrepr (m:: MIME , x, context) = __binrepr (m, x, context)
134+ _binrepr (m:: MIME , x:: Vector{UInt8} , context) = x
135135
136136"""
137137 istextmime(m::MIME)
@@ -149,11 +149,7 @@ false
149149```
150150"""
151151istextmime (m:: MIME ) = startswith (string (m), " text/" )
152-
153- # it is convenient to accept strings instead of ::MIME
154152istextmime (m:: AbstractString ) = istextmime (MIME (m))
155- reprmime (m:: AbstractString , x) = reprmime (MIME (m), x)
156- stringmime (m:: AbstractString , x) = stringmime (MIME (m), x)
157153
158154for mime in [" application/atom+xml" , " application/ecmascript" ,
159155 " application/javascript" , " application/julia" ,
169165# We have an abstract AbstractDisplay class that can be subclassed in order to
170166# define new rich-display output devices. A typical subclass should
171167# overload display(d::AbstractDisplay, m::MIME, x) for supported MIME types m,
172- # (typically using reprmime or stringmime to get the MIME
168+ # (typically using show, repr, ..., to get the MIME
173169# representation of x) and should also overload display(d::AbstractDisplay, x)
174170# to display x in whatever MIME type is preferred by the AbstractDisplay and
175171# is writable by x. display(..., x) should throw a MethodError if x
0 commit comments