@@ -33,56 +33,89 @@ mimewritable{mime}(::MIME{mime}, x) =
33
33
show (io:: IO , m:: AbstractString , x) = show (io, MIME (m), x)
34
34
mimewritable (m:: AbstractString , x) = mimewritable (MIME (m), x)
35
35
36
- # ##########################################################################
37
- # MIME types are assumed to be binary data except for a set of types known
38
- # to be text data (possibly Unicode). istextmime(m) returns whether
39
- # m::MIME is text data, and reprmime(m, x) returns x written to either
40
- # a string (for text m::MIME) or a Vector{UInt8} (for binary m::MIME),
41
- # assuming the corresponding write_mime method exists. stringmime
42
- # is like reprmime except that it always returns a string, which in the
43
- # case of binary data is Base64-encoded.
44
- #
45
- # Also, if reprmime is passed a AbstractString for a text type or Vector{UInt8} for
46
- # a binary type, the argument is assumed to already be in the corresponding
47
- # format and is returned unmodified. This is useful so that raw data can be
48
- # passed to display(m::MIME, x).
36
+ abstract MIMETypeType
37
+
38
+ immutable IsText <: MIMETypeType end
39
+ immutable IsBytes <: MIMETypeType end
49
40
50
41
verbose_show (io, m, x) = show (IOContext (io,limit= false ), m, x)
51
42
43
+ """
44
+ MIME types are assumed to be binary data except for a set of types known to be
45
+ text data (possibly Unicode). `mimetypetype(m)` returns `Multimedia.IsText` or
46
+ `Multimedia.IsBytes` for text or binary data respectively.
47
+ """
48
+ mimetypetype {M} (:: MIME{M} ) =
49
+ startswith (string (M), " text/" ) ? IsText () : IsBytes ()
50
+
51
+ """
52
+ reprmime(mime, x)
53
+
54
+ Returns an `AbstractString` or `Vector{UInt8}` containing the representation of
55
+ `x` in the requested `mime` type, as written by `show` (throwing a
56
+ `MethodError` if no appropriate `show` is available). An `AbstractString` is
57
+ returned for MIME types with textual representations (such as `"text/html"` or
58
+ `"application/postscript"`), whereas binary data is returned as
59
+ `Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia
60
+ treats a given `mime` type as text.)
61
+
62
+ As a special case, if `x` is an `AbstractString` (for textual MIME types) or a
63
+ `Vector{UInt8}` (for binary MIME types), the `reprmime` function assumes that
64
+ `x` is already in the requested `mime` format and simply returns `x`. This
65
+ special case does not apply to the `"text/plain"` MIME type. This is useful so
66
+ that raw data can be passed to `display(m::MIME, x)`.
67
+ """
68
+ reprmime (m:: MIME , x) = reprmime (mimetypetype (m), m, x)
69
+ reprmime (:: IsText , m:: MIME , x) = sprint (verbose_show, m, x)
70
+
71
+ # strings are shown escaped for text/plain
72
+ reprmime (:: IsText , :: MIME , x:: AbstractString ) = x
73
+ reprmime (:: IsText , m:: MIME"text/plain" , x:: AbstractString ) =
74
+ sprint (verbose_show, m, x)
75
+
76
+ function reprmime (:: IsBytes , m:: MIME , x)
77
+ s = IOBuffer ()
78
+ verbose_show (s, m, x)
79
+ takebuf_array (s)
80
+ end
81
+ reprmime (:: IsBytes , m:: MIME , x:: Vector{UInt8} ) = x
82
+
83
+ """
84
+ stringmime(mime, x)
85
+
86
+ Returns an `AbstractString` containing the representation of `x` in the
87
+ requested `mime` type. This is similar to [`reprmime`](:func:`reprmime`) except
88
+ that binary data is base64-encoded as an ASCII string.
89
+ """
90
+ stringmime (m:: MIME , x) = stringmime (mimetypetype (m), m, x)
91
+ stringmime (:: IsText , m:: MIME , x) = reprmime (m, x)
92
+ stringmime (:: IsBytes , m:: MIME , x) = base64encode (verbose_show, m, x)
93
+ stringmime (:: IsBytes , m:: MIME , x:: Vector{UInt8} ) = base64encode (write, x)
94
+
52
95
macro textmime (mime)
96
+ Base. depwarn (string (" `@textmime mime` is deprecated; use " ,
97
+ " `Base.Multimedia.mimetypetype(::MIME{mime}) = " ,
98
+ " Base.Multimedia.IsText` instead." ))
53
99
quote
54
- mimeT = MIME{Symbol ($ mime)}
55
- # avoid method ambiguities with the general definitions below:
56
- # (Q: should we treat Vector{UInt8} as a String?)
57
- Base. Multimedia. reprmime (m:: mimeT , x:: Vector{UInt8} ) = sprint (verbose_show, m, x)
58
- Base. Multimedia. stringmime (m:: mimeT , x:: Vector{UInt8} ) = reprmime (m, x)
59
-
60
- Base. Multimedia. istextmime (:: mimeT ) = true
61
- if $ (mime != " text/plain" ) # strings are shown escaped for text/plain
62
- Base. Multimedia. reprmime (m:: mimeT , x:: AbstractString ) = x
63
- end
64
- Base. Multimedia. reprmime (m:: mimeT , x) = sprint (verbose_show, m, x)
65
- Base. Multimedia. stringmime (m:: mimeT , x) = reprmime (m, x)
100
+ Base. Multimedia. mimetypetype (:: MIME{$(Symbol(mime))} ) =
101
+ Base. Multimedia. IsText ()
66
102
end
67
103
end
68
104
69
- istextmime (:: MIME ) = false
70
- function reprmime (m:: MIME , x)
71
- s = IOBuffer ()
72
- verbose_show (s, m, x)
73
- takebuf_array (s)
74
- end
75
- reprmime (m:: MIME , x:: Vector{UInt8} ) = x
76
- stringmime (m:: MIME , x) = base64encode (verbose_show, m, x)
77
- stringmime (m:: MIME , x:: Vector{UInt8} ) = base64encode (write, x)
105
+ istextmime (m:: MIME ) = isa (mimetypetype (m), IsText)
78
106
79
107
# it is convenient to accept strings instead of ::MIME
80
108
istextmime (m:: AbstractString ) = istextmime (MIME (m))
81
109
reprmime (m:: AbstractString , x) = reprmime (MIME (m), x)
82
110
stringmime (m:: AbstractString , x) = stringmime (MIME (m), x)
83
111
84
- 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" ]
85
- @eval @textmime $ mime
112
+ for mime in [" application/atom+xml" , " application/ecmascript" ,
113
+ " application/javascript" , " application/julia" , " application/json" ,
114
+ " application/postscript" , " application/rdf+xml" , " application/rss+xml" ,
115
+ " application/x-latex" , " application/xhtml+xml" , " application/xml" ,
116
+ " application/xml-dtd" , " image/svg+xml" , " model/vrml" , " model/x3d+vrml" ,
117
+ " model/x3d+xml" ]
118
+ mimetypetype (:: MIME{Symbol(mime)} ) = IsText ()
86
119
end
87
120
88
121
# ##########################################################################
0 commit comments