forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
316 lines (263 loc) · 6.73 KB
/
test.html
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="assets/favicon.png" />
<title>Test drive ▲ Prism</title>
<link rel="stylesheet" href="assets/style.css" />
<link rel="stylesheet" href="themes/prism.css" data-noprefix />
<style>
#theme {
margin-right: -9em;
}
textarea {
width: 100%;
height: 10em;
padding: 1em;
box-sizing: border-box;
margin: .5em 0;
background: #f5f2f0;
color: black;
text-shadow: 0 1px white;
tab-size: 4;
font: 100% Consolas, Monaco, monospace;
white-space: pre;
word-wrap: normal;
}
#language {
column-count: 4;
}
#language label {
display: block;
padding: .2em;
}
#language label[data-id="javascript"] {
border-bottom: 1px solid #aaa;
padding-bottom: 1em;
margin-bottom: 1em;
}
#language input {
margin-right: 1em;
}
#language strong {
display: block;
column-span: all;
}
pre.show-tokens {
line-height: calc(1.5em + 12px);
}
.show-tokens .token:not(:first-child) {
margin-left: 1px;
}
.show-tokens .token:empty {
background: red;
}
.show-tokens .token:empty::before {
color: white;
content: 'empty';
font-style: italic;
text-shadow: black 0 0 .3em;
}
.show-tokens .token {
border: 1px solid;
padding: 6px 1px;
}
.show-tokens .token > .token {
padding: 4px 1px;
}
.show-tokens .token > .token > .token {
padding: 2px 1px;
}
.show-tokens .token > .token > .token > .token {
padding: 0 1px;
}
.show-tokens .token > .token > .token > .token > .token {
border: none;
border-left: 1px solid;
border-right: 1px solid;
padding: 0;
margin: 0 1px;
}
</style>
<script src="assets/prefixfree.min.js"></script>
<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
<script src="https://www.google-analytics.com/ga.js" async></script>
</head>
<body>
<header>
<div class="intro" data-src="assets/templates/header-main.html" data-type="text/html"></div>
<h2>Test drive</h2>
<p>Take Prism for a spin!</p>
</header>
<section>
<form>
<p>
<textarea><p class="hey">Type some code here</p></textarea>
</p>
<p>Result:</p>
<pre><code></code></pre>
<p id="options">
<label><input type="checkbox" id="option-show-tokens"> Show tokens</label>
</p>
<p id="language">
<strong>Language:</strong>
</p>
</form>
</section>
<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
<script src="assets/utopia.js"></script>
<script src="components/prism-core.js"></script>
<script src="components.js"></script>
<script src="assets/code.js"></script>
<script src="assets/vendor/promise.js"></script>
<script>
(function() {
/** @type {HTMLFormElement} */
var form = $('form');
/** @type {HTMLElement} */
var code = $('code', form);
var languages = components.languages;
function highlightCode() {
var newCode = document.createElement('code');
newCode.textContent = code.textContent;
newCode.className = code.className;
Prism.highlightElement(newCode);
code.parentElement.replaceChild(newCode, code);
code = newCode;
};
function updateHashLanguage(lang) {
location.hash = lang ? 'language=' + lang : '';
}
function getHashLanguage() {
var match = /[#&]language=([^&]+)/.exec(location.hash);
return match ? match[1] : null;
}
function getRadio(lang) {
return $('input[name=language][value="' + lang + '"]');
}
window.onhashchange = function () {
var input = getRadio(getHashLanguage());
if (input && !input.checked) {
input.checked = true;
input.onclick();
}
}
for (var id in languages) {
if (id == 'meta') {
continue;
}
var name = languages[id].title || languages[id];
$u.element.create('label', {
attributes: {
'data-id': id
},
contents: [
{
tag: 'input',
properties: {
type: 'radio',
name: 'language',
value: id,
onclick: function () {
var lang = this.value;
code.className = 'language-' + lang;
code.textContent = code.textContent;
updateHashLanguage(lang);
// loadLanguage() returns a promise, so we use highlightCode()
// as resolve callback. The promise will be immediately
// resolved if there is nothing to load.
loadLanguage(lang).then(highlightCode);
}
}
}, name
],
inside: '#language'
});
}
/**
* Loads a language, including all dependencies
*
* @param {string} lang the language to load
* @type {Promise} the promise which resolves as soon as everything is loaded
*/
function loadLanguage (lang)
{
// at first we need to fetch all dependencies for the main language
// Note: we need to do this, even if the main language already is loaded (just to be sure..)
//
// We load an array of all dependencies and call recursively this function on each entry
//
// dependencies is now an (possibly empty) array of loading-promises
var dependencies = getDependenciesOfLanguage(lang).map(loadLanguage);
// We create a promise, which will resolve, as soon as all dependencies are loaded.
// They need to be fully loaded because the main language may extend them.
return Promise.all(dependencies)
.then(function () {
// If the main language itself isn't already loaded, load it now
// and return the newly created promise (we chain the promises).
// If the language is already loaded, just do nothing - the next .then()
// will immediately be called
if (!Prism.languages[lang]) {
return new Promise(function (resolve) {
$u.script('components/prism-' + lang + '.js', resolve);
});
}
});
}
/**
* Returns all dependencies (as identifiers) of a specific language
*
* @param {string} lang
* @returns {Array.<string>} the list of dependencies. Empty if the language has none.
*/
function getDependenciesOfLanguage (lang)
{
if (!components.languages[lang] || !components.languages[lang].require)
{
return [];
}
return ($u.type(components.languages[lang].require) === "array")
? components.languages[lang].require
: [components.languages[lang].require];
}
var radios = $$('input[name=language]');
var selectedRadio = radios[0];
var lastLanguageRadio = getRadio(getHashLanguage());
if (lastLanguageRadio) {
selectedRadio = lastLanguageRadio;
}
selectedRadio.checked = true;
selectedRadio.onclick();
var textarea = $('textarea', form);
try {
var lastCode = sessionStorage.getItem('test-code');
if (lastCode) {
textarea.value = lastCode;
}
} catch (e) {
// ignore sessionStorage errors
}
textarea.oninput = function() {
var codeText = this.value || '';
code.textContent = codeText;
highlightCode();
try {
sessionStorage.setItem('test-code', codeText);
} catch (error) {
// ignore sessionStorage errors
}
}
textarea.oninput();
$('#option-show-tokens').onchange = function () {
var cls = 'show-tokens';
if (this.checked) {
$('pre').classList.add(cls);
} else {
$('pre').classList.remove(cls);
}
};
$('#option-show-tokens').onchange();
})();
</script>
</body>
</html>