Skip to content

Commit 40e7d29

Browse files
kshyattfredrikekre
authored andcommitted
Add doctest, missing import, refs, and stream -> io for multimedia (#25000)
1 parent 8ec273b commit 40e7d29

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

base/multimedia.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export AbstractDisplay, display, pushdisplay, popdisplay, displayable, redisplay
1515
# struct MIME{mime} end
1616
# macro MIME_str(s)
1717
import Base: MIME, @MIME_str
18-
18+
import Base64
1919
import Base: show, print, string, convert
2020
MIME(s) = MIME{Symbol(s)}()
2121
show(io::IO, ::MIME{mime}) where {mime} = print(io, "MIME type ", string(mime))
@@ -45,26 +45,26 @@ mimewritable(::MIME{mime}, x) where {mime} =
4545
method_exists(show, Tuple{IO, MIME{mime}, typeof(x)})
4646

4747
"""
48-
show(stream, mime, x)
48+
show(io, mime, x)
4949
5050
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
5252
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
5656
literal strings; to construct `MIME` types in a more flexible manner use
5757
`MIME{Symbol("")}`.)
5858
5959
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
6161
your images to be displayed on any PNG-capable `AbstractDisplay` (such as IJulia). As usual, be sure
6262
to `import Base.show` in order to add new methods to the built-in Julia function
6363
`show`.
6464
6565
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
6666
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.
6868
6969
Technically, the `MIME"mime"` macro defines a singleton type for the given `mime` string,
7070
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)
8585
reprmime(mime, x)
8686
8787
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
9090
returned for MIME types with textual representations (such as `"text/html"` or
9191
`"application/postscript"`), whereas binary data is returned as
9292
`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
9797
`x` is already in the requested `mime` format and simply returns `x`. This
9898
special case does not apply to the `"text/plain"` MIME type. This is useful so
9999
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+
```
100108
"""
101109
reprmime(m::MIME, x) = istextmime(m) ? _textreprmime(m, x) : _binreprmime(m, x)
102110

0 commit comments

Comments
 (0)