Skip to content

Commit

Permalink
Merge pull request #16563 from JuliaLang/jb/writemime_to_show
Browse files Browse the repository at this point in the history
deprecate `writemime` to methods of `show`
  • Loading branch information
JeffBezanson committed May 27, 2016
2 parents dabfa73 + ae62bf0 commit 692618f
Show file tree
Hide file tree
Showing 31 changed files with 123 additions and 112 deletions.
14 changes: 10 additions & 4 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ macro enum(T,syms...)
print(io, x, "::", $(esc(typename)), " = ", Int(x))
end
end
function Base.writemime(io::IO,::MIME"text/plain",::Type{$(esc(typename))})
print(io, "Enum ", $(esc(typename)), ":")
for (sym, i) in $vals
print(io, "\n", sym, " = ", i)
function Base.show(io::IO,t::Type{$(esc(typename))})
if get(io, :multiline, false)
print(io, "Enum ")
Base.show_datatype(io, t)
print(io, ":")
for (sym, i) in $vals
print(io, "\n", sym, " = ", i)
end
else
Base.show_datatype(io, t)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export
import Base:
Display,
display,
writemime,
show,
AnyDict,
==

Expand Down Expand Up @@ -111,7 +111,7 @@ end
function display(d::REPLDisplay, ::MIME"text/plain", x)
io = outstream(d.repl)
Base.have_color && write(io, answer_color(d.repl))
writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x)
show(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x)
println(io)
end
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
Expand Down
3 changes: 1 addition & 2 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import Base:
pipe_reader,
pipe_writer,
read,
readuntil,
writemime
readuntil

## TextTerminal ##

Expand Down
6 changes: 3 additions & 3 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module DataFmt

import Base: _default_delims, tryparse_internal, writemime
import Base: _default_delims, tryparse_internal, show

export countlines, readdlm, readcsv, writedlm, writecsv

Expand Down Expand Up @@ -623,7 +623,7 @@ end
writedlm(io, a; opts...) = writedlm(io, a, '\t'; opts...)
writecsv(io, a; opts...) = writedlm(io, a, ','; opts...)

writemime(io::IO, ::MIME"text/csv", a) = writedlm(io, a, ',')
writemime(io::IO, ::MIME"text/tab-separated-values", a) = writedlm(io, a, '\t')
show(io::IO, ::MIME"text/csv", a) = writedlm(io, a, ',')
show(io::IO, ::MIME"text/tab-separated-values", a) = writedlm(io, a, '\t')

end # module DataFmt
1 change: 1 addition & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ end
@deprecate_binding WORD_SIZE Sys.WORD_SIZE

@deprecate showcompact_lim show
@deprecate writemime show

@deprecate blas_set_num_threads BLAS.set_num_threads

Expand Down
26 changes: 15 additions & 11 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6628,28 +6628,32 @@ Return, but do not print, the time elapsed since the last [`tic`](:func:`tic`).
toq

"""
writemime(stream, mime, x)
show(stream, mime, x)
The `display` functions ultimately call `writemime` in order to write an object `x` as a
The `display` functions ultimately call `show` in order to write an object `x` as a
given `mime` type to a given I/O `stream` (usually a memory buffer), if possible. In order
to provide a rich multimedia representation of a user-defined type `T`, it is only necessary
to define a new `writemime` method for `T`, via: `writemime(stream, ::MIME"mime", x::T) = ...`,
to define a new `show` method for `T`, via: `show(stream, ::MIME"mime", x::T) = ...`,
where `mime` is a MIME-type string and the function body calls `write` (or similar) to write
that representation of `x` to `stream`. (Note that the `MIME""` notation only supports
literal strings; to construct `MIME` types in a more flexible manner use
`MIME{Symbol("")}`.)
For example, if you define a `MyImage` type and know how to write it to a PNG file, you
could define a function `writemime(stream, ::MIME"image/png", x::MyImage) = ...` to allow
could define a function `show(stream, ::MIME"image/png", x::MyImage) = ...` to allow
your images to be displayed on any PNG-capable `Display` (such as IJulia). As usual, be sure
to `import Base.writemime` in order to add new methods to the built-in Julia function
`writemime`.
to `import Base.show` in order to add new methods to the built-in Julia function
`show`.
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
output that calls `show` with 2 arguments. Therefore, this case should be handled by
defining a 2-argument `show(stream::IO, x::MyType)` method.
Technically, the `MIME"mime"` macro defines a singleton type for the given `mime` string,
which allows us to exploit Julia's dispatch mechanisms in determining how to display objects
of any given type.
"""
writemime
show(stream, mime, x)

"""
mean!(r, v)
Expand Down Expand Up @@ -7262,7 +7266,7 @@ Write an informative text representation of a value to the current output stream
should overload `show(io, x)` where the first argument is a stream. The representation used
by `show` generally includes Julia-specific formatting and type information.
"""
show
show(x)

"""
@allocated
Expand Down Expand Up @@ -7909,8 +7913,8 @@ rethrow
reprmime(mime, x)
Returns an `AbstractString` or `Vector{UInt8}` containing the representation of `x` in the
requested `mime` type, as written by `writemime` (throwing a `MethodError` if no appropriate
`writemime` is available). An `AbstractString` is returned for MIME types with textual
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.)
Expand Down Expand Up @@ -8303,7 +8307,7 @@ showall
Returns a boolean value indicating whether or not the object `x` can be written as the given
`mime` type. (By default, this is determined automatically by the existence of the
corresponding `writemime` function for `typeof(x)`.)
corresponding `show` function for `typeof(x)`.)
"""
mimewritable

Expand Down
14 changes: 7 additions & 7 deletions base/docs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Text / HTML objects

import Base: print, writemime
import Base: print, show

export HTML, @html_str

Expand All @@ -26,13 +26,13 @@ end
function HTML(xs...)
HTML() do io
for x in xs
writemime(io, MIME"text/html"(), x)
show(io, MIME"text/html"(), x)
end
end
end

writemime(io::IO, ::MIME"text/html", h::HTML) = print(io, h.content)
writemime{F <: Function}(io::IO, ::MIME"text/html", h::HTML{F}) = h.content(io)
show(io::IO, ::MIME"text/html", h::HTML) = print(io, h.content)
show{F <: Function}(io::IO, ::MIME"text/html", h::HTML{F}) = h.content(io)

"""
@html_str -> Docs.HTML
Expand All @@ -46,7 +46,7 @@ end
function catdoc(xs::HTML...)
HTML() do io
for x in xs
writemime(io, MIME"text/html"(), x)
show(io, MIME"text/html"(), x)
end
end
end
Expand All @@ -70,7 +70,7 @@ end

print(io::IO, t::Text) = print(io, t.content)
print{F <: Function}(io::IO, t::Text{F}) = t.content(io)
writemime(io::IO, ::MIME"text/plain", t::Text) = print(io, t)
show(io::IO, t::Text) = print(io, t)

"""
@text_str -> Docs.Text
Expand All @@ -84,7 +84,7 @@ end
function catdoc(xs::Text...)
Text() do io
for x in xs
writemime(io, MIME"text/plain"(), x)
show(io, MIME"text/plain"(), x)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,6 @@ export
@MIME_str,
reprmime,
stringmime,
writemime,
mimewritable,
popdisplay,
pushdisplay,
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/IPython/IPython.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function blocktex(stream::IO, md::MD)
end
end

writemime(io::IO, ::MIME"text/plain", tex::LaTeX) =
show(io::IO, tex::LaTeX) =
print(io, '$', tex.formula, '$')

latex(io::IO, tex::LaTeX) =
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Markdown

import Base: writemime, ==
import Base: show, ==
import Core: @doc_str

include("parse/config.jl")
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/render/html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export html

html(md) = sprint(html, md)

function writemime(io::IO, ::MIME"text/html", md::MD)
function show(io::IO, ::MIME"text/html", md::MD)
withtag(io, :div, :class=>"markdown") do
html(io, md)
end
Expand Down
5 changes: 2 additions & 3 deletions base/markdown/render/latex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function latex(io::IO, md::List)
end
end

function writemime(io::IO, ::MIME"text/latex", md::HorizontalRule)
function show(io::IO, ::MIME"text/latex", md::HorizontalRule)
println(io, "\\rule{\\textwidth}{1pt}")
end

Expand Down Expand Up @@ -136,5 +136,4 @@ latex(md) = sprint(latex, md)
latexinline(md) = sprint(latexinline, md)
latexesc(s) = sprint(latexesc, s)

writemime(io::IO, ::MIME"text/latex", md::MD) = latex(io, md)
#writemime(io::IO, ::MIME"text/latex", md::MD) = writemime(io, "text/plain", md)
show(io::IO, ::MIME"text/latex", md::MD) = latex(io, md)
10 changes: 5 additions & 5 deletions base/markdown/render/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function plain(io::IO, l::LaTeX)
end

function plain(io::IO, md)
writemime(io, MIME"text/plain"(), md)
show(io, MIME"text/plain"(), md)
println(io)
end

Expand Down Expand Up @@ -95,9 +95,9 @@ plaininline(io::IO, md::Code) = print(io, "`", md.code, "`")

plaininline(io::IO, br::LineBreak) = println(io)

plaininline(io::IO, x) = writemime(io, MIME"text/plain"(), x)
plaininline(io::IO, x) = show(io, MIME"text/plain"(), x)

# writemime
# show

Base.writemime(io::IO, ::MIME"text/plain", md::MD) = plain(io, md)
Base.writemime(io::IO, ::MIME"text/markdown", md::MD) = plain(io, md)
Base.show(io::IO, md::MD) = plain(io, md)
Base.show(io::IO, ::MIME"text/markdown", md::MD) = plain(io, md)
6 changes: 3 additions & 3 deletions base/markdown/render/rich.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This file is a part of Julia. License is MIT: http:https://julialang.org/license

function tohtml(io::IO, m::MIME"text/html", x)
writemime(io, m, x)
show(io, m, x)
end

function tohtml(io::IO, m::MIME"text/plain", x)
htmlesc(io, sprint(writemime, m, x))
htmlesc(io, sprint(show, m, x))
end

function tohtml(io::IO, m::MIME"image/png", img)
Expand All @@ -15,7 +15,7 @@ function tohtml(io::IO, m::MIME"image/png", img)
end

function tohtml(m::MIME"image/svg+xml", img)
writemime(io, m, img)
show(io, m, img)
end

# Display infrastructure
Expand Down
8 changes: 4 additions & 4 deletions base/markdown/render/rst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function rst(io::IO, l::LaTeX)
end
end

rst(io::IO, md) = writemime(io, "text/rst", md)
rst(io::IO, md) = show(io, "text/rst", md)

# Inline elements

Expand Down Expand Up @@ -113,8 +113,8 @@ rstinline(io::IO, br::LineBreak) = println(io)

rstinline(io::IO, l::LaTeX) = print(io, ":math:`", l.formula, "`")

rstinline(io::IO, x) = writemime(io, MIME"text/rst"(), x)
rstinline(io::IO, x) = show(io, MIME"text/rst"(), x)

# writemime
# show

Base.writemime(io::IO, ::MIME"text/rst", md::MD) = rst(io, md)
Base.show(io::IO, ::MIME"text/rst", md::MD) = rst(io, md)
4 changes: 2 additions & 2 deletions base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function term(io::IO, br::HorizontalRule, columns)
println(io, " " ^ margin, "-" ^ (columns - 2margin))
end

term(io::IO, x, _) = writemime(io, MIME"text/plain"(), x)
term(io::IO, x, _) = show(io, MIME"text/plain"(), x)

# Inline Content

Expand Down Expand Up @@ -124,7 +124,7 @@ function terminline(io::IO, code::Code)
print_with_format(:cyan, io, code.code)
end

terminline(io::IO, x) = writemime(io, MIME"text/plain"(), x)
terminline(io::IO, x) = show(io, MIME"text/plain"(), x)

# Show in terminal

Expand Down
12 changes: 6 additions & 6 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function url(m::Method)
end
end

function writemime(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
function show(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}())
tv, decls, file, line = arg_decl_parts(m)
ft = m.sig.parameters[1]
d1 = decls[1]
Expand Down Expand Up @@ -249,7 +249,7 @@ function writemime(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataTy
end
end

function writemime(io::IO, mime::MIME"text/html", ms::MethodList)
function show(io::IO, mime::MIME"text/html", ms::MethodList)
mt = ms.mt
name = mt.name
n = length(ms)
Expand All @@ -260,23 +260,23 @@ function writemime(io::IO, mime::MIME"text/html", ms::MethodList)
kwtype = isdefined(mt, :kwsorter) ? Nullable{DataType}(typeof(mt.kwsorter)) : Nullable{DataType}()
for meth in ms
print(io, "<li> ")
writemime(io, mime, meth; kwtype=kwtype)
show(io, mime, meth; kwtype=kwtype)
print(io, "</li> ")
end
print(io, "</ul>")
end

writemime(io::IO, mime::MIME"text/html", mt::MethodTable) = writemime(io, mime, MethodList(mt))
show(io::IO, mime::MIME"text/html", mt::MethodTable) = show(io, mime, MethodList(mt))

# pretty-printing of Vector{Method} for output of methodswith:

function writemime(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
function show(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
print(io, summary(mt))
if !isempty(mt)
print(io, ":<ul>")
for d in mt
print(io, "<li> ")
writemime(io, mime, d)
show(io, mime, d)
end
print(io, "</ul>")
end
Expand Down
Loading

0 comments on commit 692618f

Please sign in to comment.