This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.ts
176 lines (150 loc) · 3.38 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
IRenderer
} from '../rendermime';
import * as Convert
from 'ansi-to-html';
import {
Widget
} from 'phosphor-widget';
import {
Message
} from 'phosphor-messaging';
import {
typeset
} from './latex';
/**
* A widget for displaying HTML and rendering math.
*/
export
class HTMLWidget extends Widget {
constructor(html: string) {
super();
this.node.innerHTML = html;
}
/**
* A message handler invoked on an `'after-attach'` message.
*
* ####Notes
* If the node is visible, it is typeset.
*/
onAfterAttach(msg: Message) {
typeset(this.node);
}
}
/**
* A widget for displaying text and rendering math.
*/
export
class LatexWidget extends Widget {
constructor(text: string) {
super();
this.node.textContent = text;
}
/**
* A message handler invoked on an `'after-attach'` message.
*
* ####Notes
* If the node is visible, it is typeset.
*/
onAfterAttach(msg: Message) {
typeset(this.node);
}
}
/**
* A renderer for raw html.
*/
export
class HTMLRenderer implements IRenderer<Widget> {
mimetypes = ['text/html'];
render(mimetype: string, data: string): Widget {
return new HTMLWidget(data);
}
}
/**
* A renderer for `<img>` data.
*/
export
class ImageRenderer implements IRenderer<Widget> {
mimetypes = ['image/png', 'image/jpeg', 'image/gif'];
render(mimetype: string, data: string): Widget {
let w = new Widget();
let img = document.createElement('img');
img.src = `data:${mimetype};base64,${data}`;
w.node.appendChild(img);
return w;
}
}
/**
* A renderer for raw `textContent` data.
*/
export
class TextRenderer implements IRenderer<Widget> {
mimetypes = ['text/plain'];
render(mimetype: string, data: string): Widget {
let w = new Widget();
w.node.textContent = data;
return w;
}
}
/**
* A renderer for Jupyter console text data.
*/
export
class ConsoleTextRenderer implements IRenderer<Widget> {
mimetypes = ['application/vnd.jupyter.console-text'];
constructor() {
this._converter = new Convert({
escapeXML: true,
newline: true
});
}
render(mimetype: string, data: string): Widget {
let w = new Widget();
w.node.innerHTML = this._converter.toHtml(data);
return w;
}
private _converter: Convert = null;
}
/**
* A renderer for raw `<script>` data.
*/
export
class JavascriptRenderer implements IRenderer<Widget> {
mimetypes = ['text/javascript', 'application/javascript'];
render(mimetype: string, data: string): Widget {
let w = new Widget();
let s = document.createElement('script');
s.type = mimetype;
s.textContent = data;
w.node.appendChild(s);
return w;
}
}
/**
* A renderer for `<svg>` data.
*/
export
class SVGRenderer implements IRenderer<Widget> {
mimetypes = ['image/svg+xml'];
render(mimetype: string, data: string): Widget {
let w = new Widget();
w.node.innerHTML = data;
let svgElement = w.node.getElementsByTagName('svg')[0];
if (!svgElement) {
throw new Error('SVGRender: Error: Failed to create <svg> element');
}
return w;
}
}
/**
* A renderer for LateX data.
*/
export
class LatexRenderer implements IRenderer<Widget> {
mimetypes = ['text/latex'];
render(mimetype: string, data: string): Widget {
return new LatexWidget(data);
}
}