44 "bytes"
55 "errors"
66 "fmt"
7+ "image"
78 "io"
89 "io/ioutil"
910 "net/http"
@@ -65,12 +66,15 @@ func read(data interface{}) ([]byte, string) {
6566}
6667
6768func Any (mimeType string , data interface {}) Data {
69+ if img , ok := data .(image.Image ); ok {
70+ return Image (img )
71+ }
6872 b , s := read (data )
6973 if len (mimeType ) == 0 {
7074 mimeType = http .DetectContentType (b )
7175 }
7276 d := Data {
73- Data : BundledMIMEData {
77+ Data : MIMEMap {
7478 "text/plain" : s ,
7579 },
7680 }
@@ -80,9 +84,14 @@ func Any(mimeType string, data interface{}) Data {
8084 return d
8185}
8286
87+ // same as Any("", data), autodetects MIME type
88+ func Auto (data interface {}) Data {
89+ return Any ("" , data )
90+ }
91+
8392func MakeData (mimeType string , data interface {}) Data {
8493 d := Data {
85- Data : BundledMIMEData {
94+ Data : MIMEMap {
8695 mimeType : data ,
8796 },
8897 }
@@ -94,90 +103,59 @@ func MakeData(mimeType string, data interface{}) Data {
94103
95104func MakeData3 (mimeType string , plaintext string , data interface {}) Data {
96105 return Data {
97- Data : BundledMIMEData {
106+ Data : MIMEMap {
98107 "text/plain" : plaintext ,
99108 mimeType : data ,
100109 },
101110 }
102111}
103112
104- func Bytes (mimeType string , bytes []byte ) Data {
105- if len (mimeType ) == 0 {
106- mimeType = http .DetectContentType (bytes )
107- }
108- return MakeData3 (mimeType , mimeType , bytes )
109- }
110-
111113func File (mimeType string , path string ) Data {
112114 bytes , err := ioutil .ReadFile (path )
113115 if err != nil {
114116 panic (err )
115117 }
116- return Bytes (mimeType , bytes )
118+ return Any (mimeType , bytes )
117119}
118120
119121func HTML (html string ) Data {
120- return String (MIMETypeHTML , html )
122+ return MakeData (MIMETypeHTML , html )
121123}
122124
123125func JSON (json map [string ]interface {}) Data {
124126 return MakeData (MIMETypeJSON , json )
125127}
126128
127129func JavaScript (javascript string ) Data {
128- return String (MIMETypeJavaScript , javascript )
130+ return MakeData (MIMETypeJavaScript , javascript )
129131}
130132
131133func JPEG (jpeg []byte ) Data {
132- return Bytes (MIMETypeJPEG , jpeg )
134+ return MakeData (MIMETypeJPEG , jpeg )
133135}
134136
135137func Latex (latex string ) Data {
136138 return MakeData3 (MIMETypeLatex , latex , "$" + strings .Trim (latex , "$" )+ "$" )
137139}
138140
139141func Markdown (markdown string ) Data {
140- return String (MIMETypeMarkdown , markdown )
142+ return MakeData (MIMETypeMarkdown , markdown )
141143}
142144
143145func Math (latex string ) Data {
144146 return MakeData3 (MIMETypeLatex , latex , "$$" + strings .Trim (latex , "$" )+ "$$" )
145147}
146148
147149func PDF (pdf []byte ) Data {
148- return Bytes (MIMETypePDF , pdf )
150+ return MakeData (MIMETypePDF , pdf )
149151}
150152
151153func PNG (png []byte ) Data {
152- return Bytes (MIMETypePNG , png )
153- }
154-
155- func Reader (mimeType string , r io.Reader ) Data {
156- b , err := ioutil .ReadAll (r )
157- if err != nil {
158- panic (err )
159- }
160- return Bytes (mimeType , b )
161- }
162-
163- func String (mimeType string , s string ) Data {
164- if len (mimeType ) == 0 {
165- mimeType = http .DetectContentType ([]byte (s ))
166- }
167- return MakeData3 (mimeType , s , s )
154+ return MakeData (MIMETypePNG , png )
168155}
169156
170157func SVG (svg string ) Data {
171- return String (MIMETypeSVG , svg )
172- }
173-
174- func WriterTo (mimeType string , to io.WriterTo ) Data {
175- var buf bytes.Buffer
176- _ , err := to .WriteTo (& buf )
177- if err != nil {
178- panic (err )
179- }
180- return Bytes (mimeType , buf .Bytes ())
158+ return MakeData (MIMETypeSVG , svg )
181159}
182160
183161// MIME encapsulates the data and metadata into a Data.
@@ -187,15 +165,15 @@ func WriterTo(mimeType string, to io.WriterTo) Data {
187165// The exact structure of value is determined by what the frontend expects.
188166// Some easier-to-use functions for common formats supported by the Jupyter frontend
189167// are provided by the various functions above.
190- func MIME (data , metadata map [ string ] interface {} ) Data {
168+ func MIME (data , metadata MIMEMap ) Data {
191169 return Data {data , metadata , nil }
192170}
193171
194172// prepare imports.Package for interpreted code
195173var display = imports.Package {
196174 Binds : map [string ]r.Value {
197175 "Any" : r .ValueOf (Any ),
198- "Bytes " : r .ValueOf (Bytes ),
176+ "Auto " : r .ValueOf (Auto ),
199177 "File" : r .ValueOf (File ),
200178 "HTML" : r .ValueOf (HTML ),
201179 "Image" : r .ValueOf (Image ),
@@ -219,14 +197,11 @@ var display = imports.Package{
219197 "MIMETypeSVG" : r .ValueOf (MIMETypeSVG ),
220198 "PDF" : r .ValueOf (PDF ),
221199 "PNG" : r .ValueOf (PNG ),
222- "Reader" : r .ValueOf (Reader ),
223- "String" : r .ValueOf (String ),
224200 "SVG" : r .ValueOf (SVG ),
225- "WriterTo" : r .ValueOf (WriterTo ),
226201 },
227202 Types : map [string ]r.Type {
228- "BundledMIMEData " : r .TypeOf ((* BundledMIMEData )(nil )).Elem (),
229- "Data " : r .TypeOf ((* Data )(nil )).Elem (),
203+ "Data " : r .TypeOf ((* Data )(nil )).Elem (),
204+ "MIMEMap " : r .TypeOf ((* MIMEMap )(nil )).Elem (),
230205 },
231206}
232207
0 commit comments