forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
203 lines (168 loc) · 4.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="favicon.png" />
<title>Test drive ▲ Prism</title>
<link rel="stylesheet" href="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;
}
</style>
<script src="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="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="language">
<strong>Language:</strong>
</p>
</form>
</section>
<footer data-src="templates/footer.html" data-type="text/html"></footer>
<script src="prism.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
<script src="vendor/promise.js"></script>
<script>
(function() {
var form = $('form'), code = $('code', form),
languages = components.languages,
highlightCode = function() { Prism.highlightElement(code); };
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;
// 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]');
radios[0].checked = true;
radios[0].onclick();
var textarea = $('textarea', form);
(textarea.oninput = function() {
code.textContent = this.value || '';
highlightCode();
}).call(textarea);
})();
</script>
</body>
</html>