From 31c073f163fa266fc5d1b7f7bb7b59c0b1f60f27 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Sat, 10 Sep 2016 23:36:33 +0000 Subject: [PATCH 1/9] Make application/julia a text MIME --- base/multimedia.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/multimedia.jl b/base/multimedia.jl index de2dd2492b11f..b284752a879b1 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -81,7 +81,7 @@ 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 ["text/vnd.graphviz", "text/latex", "text/calendar", "text/n3", "text/richtext", "text/x-setext", "text/sgml", "text/tab-separated-values", "text/x-vcalendar", "text/x-vcard", "text/cmd", "text/css", "text/csv", "text/html", "text/javascript", "text/markdown", "text/plain", "text/vcard", "text/xml", "application/atom+xml", "application/ecmascript", "application/json", "application/rdf+xml", "application/rss+xml", "application/xml-dtd", "application/postscript", "image/svg+xml", "application/x-latex", "application/xhtml+xml", "application/javascript", "application/xml", "model/x3d+xml", "model/x3d+vrml", "model/vrml"] +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 end From 29622880f58c8cd59c8315bf851e8df45d485163 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 13 Sep 2016 04:41:55 +0000 Subject: [PATCH 2/9] Use trait instead of istextmime --- base/docs/helpdb/Base.jl | 25 ++++------ base/multimedia.jl | 105 +++++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 52 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 902b23fb2b4e1..79e8d6ead4d6d 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -2631,22 +2631,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) @@ -3089,6 +3073,15 @@ type. This is similar to [`reprmime`](:func:`reprmime`) except that binary data """ stringmime +""" + print_with_color(color::Symbol, [io], strings...) + +Print strings in a color specified as a symbol. + +`color` may take any of the values $(Base.available_text_colors_docstring). +""" +print_with_color + """ zero(x) diff --git a/base/multimedia.jl b/base/multimedia.jl index b284752a879b1..3bf4794f6b9b2 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -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 ########################################################################### From 34ba989b4980a40611bf18b6502321c47852d453 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 13 Sep 2016 05:00:26 +0000 Subject: [PATCH 3/9] Add a @pure annotation --- base/docs/helpdb/Base.jl | 7 ------- base/multimedia.jl | 7 ++++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 79e8d6ead4d6d..610e2ac40d277 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -3263,13 +3263,6 @@ processes completed successfully. """ exit -""" - istextmime(m::MIME) - -Determine whether a MIME type is text data. -""" -istextmime - """ skipchars(stream, predicate; linecomment::Char) diff --git a/base/multimedia.jl b/base/multimedia.jl index 3bf4794f6b9b2..05ccb25351b5c 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -45,7 +45,7 @@ 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}) = +Base.@pure mimetypetype{M}(::MIME{M}) = startswith(string(M), "text/") ? IsText() : IsBytes() """ @@ -102,6 +102,11 @@ macro textmime(mime) end end +""" + istextmime(m::MIME) + +Determine whether a MIME type is text data. +""" istextmime(m::MIME) = isa(mimetypetype(m), IsText) # it is convenient to accept strings instead of ::MIME From c8ad5dc0254b3b6a668404bb1076d20c53000129 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 13 Sep 2016 05:07:09 +0000 Subject: [PATCH 4/9] Run genstdlib --- doc/stdlib/io-network.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/stdlib/io-network.rst b/doc/stdlib/io-network.rst index 6c82c0a052eb0..ebbef111e3aba 100644 --- a/doc/stdlib/io-network.rst +++ b/doc/stdlib/io-network.rst @@ -782,7 +782,7 @@ Julia environments (such as the IPython-based IJulia notebook). 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``\ . + 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)``\ . .. function:: stringmime(mime, x) From 7fbc0960ef4acfa71c7bb664949f7192a6bd8e29 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Tue, 13 Sep 2016 05:33:07 +0000 Subject: [PATCH 5/9] Move deprecation to deprecated.jl --- base/deprecated.jl | 11 +++++++++++ base/multimedia.jl | 10 ---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index f042db938f708..0eef82075ff13 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1009,4 +1009,15 @@ export @vectorize_1arg, @vectorize_2arg @deprecate abs(M::SymTridiagonal) abs.(M) @deprecate abs(x::AbstractSparseVector) abs.(x) +# Deprecate @textmime into the Multimedia module, #18441 +eval(Multimedia, :(macro textmime(mime) + Base.depwarn(string("`@textmime mime` is deprecated; use ", + "`Base.Multimedia.mimetypetype(::MIME{mime}) = ", + "Base.Multimedia.IsText` instead."), :textmime) + quote + Base.Multimedia.mimetypetype(::MIME{$(Meta.quot(Symbol(mime)))}) = + Base.Multimedia.IsText() + end +end)) + # End deprecations scheduled for 0.6 diff --git a/base/multimedia.jl b/base/multimedia.jl index 05ccb25351b5c..8debed28605a4 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -92,16 +92,6 @@ 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 - Base.Multimedia.mimetypetype(::MIME{$(Symbol(mime))}) = - Base.Multimedia.IsText() - end -end - """ istextmime(m::MIME) From 3d658f2cccf59f09b5c81d524d80cbe3b5395277 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Wed, 14 Sep 2016 03:02:49 +0000 Subject: [PATCH 6/9] Use boolean instead of traits --- base/multimedia.jl | 51 ++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/base/multimedia.jl b/base/multimedia.jl index 8debed28605a4..dab8043d57d00 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -33,21 +33,8 @@ mimewritable{mime}(::MIME{mime}, x) = show(io::IO, m::AbstractString, x) = show(io, MIME(m), x) mimewritable(m::AbstractString, x) = mimewritable(MIME(m), 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. -""" -Base.@pure mimetypetype{M}(::MIME{M}) = - startswith(string(M), "text/") ? IsText() : IsBytes() - """ reprmime(mime, x) @@ -65,20 +52,20 @@ As a special case, if `x` is an `AbstractString` (for textual MIME types) or a 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) +reprmime(m::MIME, x) = istextmime(m) ? _textreprmime(m, x) : _binreprmime(m, x) # strings are shown escaped for text/plain -reprmime(::IsText, ::MIME, x::AbstractString) = x -reprmime(::IsText, m::MIME"text/plain", x::AbstractString) = +_textreprmime(m::MIME, x) = sprint(verbose_show, m, x) +_textreprmime(::MIME, x::AbstractString) = x +_textreprmime(m::MIME"text/plain", x::AbstractString) = sprint(verbose_show, m, x) -function reprmime(::IsBytes, m::MIME, x) +function _binreprmime(m::MIME, x) s = IOBuffer() verbose_show(s, m, x) takebuf_array(s) end -reprmime(::IsBytes, m::MIME, x::Vector{UInt8}) = x +_binreprmime(m::MIME, x::Vector{UInt8}) = x """ stringmime(mime, x) @@ -87,17 +74,18 @@ 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) +stringmime(m::MIME, x) = istextmime(m) ? reprmime(m, x) : _binstringmime(m, x) + +_binstringmime(m::MIME, x) = base64encode(verbose_show, m, x) +_binstringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x) """ istextmime(m::MIME) -Determine whether a MIME type is text data. +Determine whether a MIME type is text data. MIME types are assumed to be binary +data except for a set of types known to be text data (possibly Unicode). """ -istextmime(m::MIME) = isa(mimetypetype(m), IsText) +istextmime(m::MIME) = startswith(string(m), "text/") # it is convenient to accept strings instead of ::MIME istextmime(m::AbstractString) = istextmime(MIME(m)) @@ -105,12 +93,13 @@ 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"] - mimetypetype(::MIME{Symbol(mime)}) = IsText() + "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"] + istextmime(::MIME{Symbol(mime)}) = true end ########################################################################### From d05866ceed6c68efb40d00992b233188a344a4ea Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Wed, 14 Sep 2016 03:32:09 +0000 Subject: [PATCH 7/9] Fix deprecated @textmime --- base/deprecated.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 0eef82075ff13..f65cf785980cb 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1015,8 +1015,7 @@ eval(Multimedia, :(macro textmime(mime) "`Base.Multimedia.mimetypetype(::MIME{mime}) = ", "Base.Multimedia.IsText` instead."), :textmime) quote - Base.Multimedia.mimetypetype(::MIME{$(Meta.quot(Symbol(mime)))}) = - Base.Multimedia.IsText() + Base.Multimedia.istextmime(::MIME{$(Meta.quot(Symbol(mime)))}) = true end end)) From ee6ee0ccc73cf2189eb08dfa211eb075a750696a Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Wed, 14 Sep 2016 19:04:36 +0000 Subject: [PATCH 8/9] Fix @textmime depwarn message --- base/deprecated.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index f65cf785980cb..a4756fa2c1ef1 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1011,9 +1011,9 @@ export @vectorize_1arg, @vectorize_2arg # Deprecate @textmime into the Multimedia module, #18441 eval(Multimedia, :(macro textmime(mime) - Base.depwarn(string("`@textmime mime` is deprecated; use ", - "`Base.Multimedia.mimetypetype(::MIME{mime}) = ", - "Base.Multimedia.IsText` instead."), :textmime) + Base.depwarn(string("`@textmime \"mime\"` is deprecated; use ", + "`Base.Multimedia.istextmime(::MIME\"mime\") = true` instead" + ), :textmime) quote Base.Multimedia.istextmime(::MIME{$(Meta.quot(Symbol(mime)))}) = true end From f003f009c42092592b0dc09f57e16f0a8b1cafc1 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Wed, 28 Sep 2016 22:16:32 -0400 Subject: [PATCH 9/9] Undo accidental re-helpdbification --- base/docs/helpdb/Base.jl | 9 --------- 1 file changed, 9 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 610e2ac40d277..22ce50906d7a4 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -3073,15 +3073,6 @@ type. This is similar to [`reprmime`](:func:`reprmime`) except that binary data """ stringmime -""" - print_with_color(color::Symbol, [io], strings...) - -Print strings in a color specified as a symbol. - -`color` may take any of the values $(Base.available_text_colors_docstring). -""" -print_with_color - """ zero(x)