@@ -15,7 +15,7 @@ export AbstractDisplay, display, pushdisplay, popdisplay, displayable, redisplay
15
15
# struct MIME{mime} end
16
16
# macro MIME_str(s)
17
17
import Base: MIME, @MIME_str
18
-
18
+ import Base64
19
19
import Base: show, print, string, convert
20
20
MIME (s) = MIME {Symbol(s)} ()
21
21
show (io:: IO , :: MIME{mime} ) where {mime} = print (io, " MIME type " , string (mime))
@@ -45,26 +45,26 @@ mimewritable(::MIME{mime}, x) where {mime} =
45
45
method_exists (show, Tuple{IO, MIME{mime}, typeof (x)})
46
46
47
47
"""
48
- show(stream , mime, x)
48
+ show(io , mime, x)
49
49
50
50
The [`display`](@ref) functions ultimately call `show` in order to write an object `x` as a
51
- given `mime` type to a given I/O ` stream` (usually a memory buffer), if possible. In order
51
+ given `mime` type to a given I/O stream `io ` (usually a memory buffer), if possible. In order
52
52
to provide a rich multimedia representation of a user-defined type `T`, it is only necessary
53
- to define a new `show` method for `T`, via: `show(stream , ::MIME"mime", x::T) = ...`,
54
- where `mime` is a MIME-type string and the function body calls `write` (or similar) to write
55
- that representation of `x` to `stream `. (Note that the `MIME""` notation only supports
53
+ to define a new `show` method for `T`, via: `show(io , ::MIME"mime", x::T) = ...`,
54
+ where `mime` is a MIME-type string and the function body calls [ `write`](@ref) (or similar) to write
55
+ that representation of `x` to `io `. (Note that the `MIME""` notation only supports
56
56
literal strings; to construct `MIME` types in a more flexible manner use
57
57
`MIME{Symbol("")}`.)
58
58
59
59
For example, if you define a `MyImage` type and know how to write it to a PNG file, you
60
- could define a function `show(stream , ::MIME"image/png", x::MyImage) = ...` to allow
60
+ could define a function `show(io , ::MIME"image/png", x::MyImage) = ...` to allow
61
61
your images to be displayed on any PNG-capable `AbstractDisplay` (such as IJulia). As usual, be sure
62
62
to `import Base.show` in order to add new methods to the built-in Julia function
63
63
`show`.
64
64
65
65
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
66
66
output that calls `show` with 2 arguments. Therefore, this case should be handled by
67
- defining a 2-argument `show(stream ::IO, x::MyType)` method.
67
+ defining a 2-argument `show(io ::IO, x::MyType)` method.
68
68
69
69
Technically, the `MIME"mime"` macro defines a singleton type for the given `mime` string,
70
70
which allows us to exploit Julia's dispatch mechanisms in determining how to display objects
@@ -85,8 +85,8 @@ verbose_show(io, m, x) = show(IOContext(io, :limit => false), m, x)
85
85
reprmime(mime, x)
86
86
87
87
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of
88
- `x` in the requested `mime` type, as written by `show` (throwing a
89
- `MethodError` if no appropriate `show` is available). An `AbstractString` is
88
+ `x` in the requested `mime` type, as written by [ `show`](@ref) (throwing a
89
+ [ `MethodError`](@ref) if no appropriate `show` is available). An `AbstractString` is
90
90
returned for MIME types with textual representations (such as `"text/html"` or
91
91
`"application/postscript"`), whereas binary data is returned as
92
92
`Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
@@ -97,6 +97,14 @@ As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
97
97
`x` is already in the requested `mime` format and simply returns `x`. This
98
98
special case does not apply to the `"text/plain"` MIME type. This is useful so
99
99
that raw data can be passed to `display(m::MIME, x)`.
100
+
101
+ # Examples
102
+ ```jldoctest
103
+ julia> A = [1 2; 3 4];
104
+
105
+ julia> reprmime("text/plain", A)
106
+ "2×2 Array{Int64,2}:\\ n 1 2\\ n 3 4"
107
+ ```
100
108
"""
101
109
reprmime (m:: MIME , x) = istextmime (m) ? _textreprmime (m, x) : _binreprmime (m, x)
102
110
0 commit comments