forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prism-autoloader.js
476 lines (444 loc) · 10.4 KB
/
prism-autoloader.js
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
(function () {
if (typeof self === 'undefined' || !self.Prism || !self.document || !document.createElement) {
return;
}
/**
* The dependencies map is built automatically with gulp.
*
* @type {Object<string, string | string[]>}
*/
var lang_dependencies = /*dependencies_placeholder[*/{
"javascript": "clike",
"actionscript": "javascript",
"arduino": "cpp",
"aspnet": [
"markup",
"csharp"
],
"birb": "clike",
"bison": "c",
"c": "clike",
"csharp": "clike",
"cpp": "c",
"coffeescript": "javascript",
"crystal": "ruby",
"css-extras": "css",
"d": "clike",
"dart": "clike",
"django": "markup-templating",
"ejs": [
"javascript",
"markup-templating"
],
"etlua": [
"lua",
"markup-templating"
],
"erb": [
"ruby",
"markup-templating"
],
"fsharp": "clike",
"firestore-security-rules": "clike",
"flow": "javascript",
"ftl": "markup-templating",
"gml": "clike",
"glsl": "c",
"go": "clike",
"groovy": "clike",
"haml": "ruby",
"handlebars": "markup-templating",
"haxe": "clike",
"hlsl": "c",
"java": "clike",
"javadoc": [
"markup",
"java",
"javadoclike"
],
"jolie": "clike",
"jsdoc": [
"javascript",
"javadoclike",
"typescript"
],
"js-extras": "javascript",
"json5": "json",
"jsonp": "json",
"js-templates": "javascript",
"kotlin": "clike",
"latte": [
"clike",
"markup-templating",
"php"
],
"less": "css",
"lilypond": "scheme",
"markdown": "markup",
"markup-templating": "markup",
"mongodb": "javascript",
"n4js": "javascript",
"nginx": "clike",
"objectivec": "c",
"opencl": "c",
"parser": "markup",
"php": [
"clike",
"markup-templating"
],
"phpdoc": [
"php",
"javadoclike"
],
"php-extras": "php",
"plsql": "sql",
"processing": "clike",
"protobuf": "clike",
"pug": [
"markup",
"javascript"
],
"purebasic": "clike",
"purescript": "haskell",
"qml": "javascript",
"qore": "clike",
"racket": "scheme",
"jsx": [
"markup",
"javascript"
],
"tsx": [
"jsx",
"typescript"
],
"reason": "clike",
"ruby": "clike",
"sass": "css",
"scss": "css",
"scala": "java",
"shell-session": "bash",
"smarty": "markup-templating",
"solidity": "clike",
"soy": "markup-templating",
"sparql": "turtle",
"sqf": "clike",
"swift": "clike",
"t4-cs": [
"t4-templating",
"csharp"
],
"t4-vb": [
"t4-templating",
"vbnet"
],
"tap": "yaml",
"tt2": [
"clike",
"markup-templating"
],
"textile": "markup",
"twig": "markup",
"typescript": "javascript",
"vala": "clike",
"vbnet": "basic",
"velocity": "markup",
"wiki": "markup",
"xeora": "markup",
"xml-doc": "markup",
"xquery": "markup"
}/*]*/;
var lang_aliases = /*aliases_placeholder[*/{
"html": "markup",
"xml": "markup",
"svg": "markup",
"mathml": "markup",
"ssml": "markup",
"atom": "markup",
"rss": "markup",
"js": "javascript",
"g4": "antlr4",
"adoc": "asciidoc",
"shell": "bash",
"shortcode": "bbcode",
"rbnf": "bnf",
"oscript": "bsl",
"cs": "csharp",
"dotnet": "csharp",
"coffee": "coffeescript",
"conc": "concurnas",
"jinja2": "django",
"dns-zone": "dns-zone-file",
"dockerfile": "docker",
"eta": "ejs",
"xlsx": "excel-formula",
"xls": "excel-formula",
"gamemakerlanguage": "gml",
"hs": "haskell",
"gitignore": "ignore",
"hgignore": "ignore",
"npmignore": "ignore",
"webmanifest": "json",
"kt": "kotlin",
"kts": "kotlin",
"tex": "latex",
"context": "latex",
"ly": "lilypond",
"emacs": "lisp",
"elisp": "lisp",
"emacs-lisp": "lisp",
"md": "markdown",
"moon": "moonscript",
"n4jsd": "n4js",
"nani": "naniscript",
"objc": "objectivec",
"objectpascal": "pascal",
"px": "pcaxis",
"pcode": "peoplecode",
"pq": "powerquery",
"mscript": "powerquery",
"pbfasm": "purebasic",
"purs": "purescript",
"py": "python",
"rkt": "racket",
"rpy": "renpy",
"robot": "robotframework",
"rb": "ruby",
"sh-session": "shell-session",
"shellsession": "shell-session",
"smlnj": "sml",
"sol": "solidity",
"sln": "solution-file",
"rq": "sparql",
"t4": "t4-cs",
"trig": "turtle",
"ts": "typescript",
"tsconfig": "typoscript",
"uscript": "unrealscript",
"uc": "unrealscript",
"vb": "visual-basic",
"vba": "visual-basic",
"xeoracube": "xeora",
"yml": "yaml"
}/*]*/;
/**
* @typedef LangDataItem
* @property {{ success?: () => void, error?: () => void }[]} callbacks
* @property {boolean} [error]
* @property {boolean} [loading]
*/
/** @type {Object<string, LangDataItem>} */
var lang_data = {};
var ignored_language = 'none';
var languages_path = 'components/';
var script = Prism.util.currentScript();
if (script) {
var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)?js(?:\?[^\r\n/]*)?$/i;
var prismFile = /(^|\/)[\w-]+\.(?:min\.)?js(?:\?[^\r\n/]*)?$/i;
var autoloaderPath = script.getAttribute('data-autoloader-path');
if (autoloaderPath != null) {
// data-autoloader-path is set, so just use it
languages_path = autoloaderPath.trim().replace(/\/?$/, '/');
} else {
var src = script.src;
if (autoloaderFile.test(src)) {
// the script is the original autoloader script in the usual Prism project structure
languages_path = src.replace(autoloaderFile, 'components/');
} else if (prismFile.test(src)) {
// the script is part of a bundle like a custom prism.js from the download page
languages_path = src.replace(prismFile, '$1components/');
}
}
}
var config = Prism.plugins.autoloader = {
languages_path: languages_path,
use_minified: true,
loadLanguages: loadLanguages
};
/**
* Lazily loads an external script.
*
* @param {string} src
* @param {() => void} [success]
* @param {() => void} [error]
*/
function addScript(src, success, error) {
var s = document.createElement('script');
s.src = src;
s.async = true;
s.onload = function () {
document.body.removeChild(s);
success && success();
};
s.onerror = function () {
document.body.removeChild(s);
error && error();
};
document.body.appendChild(s);
}
/**
* Returns all additional dependencies of the given element defined by the `data-dependencies` attribute.
*
* @param {Element} element
* @returns {string[]}
*/
function getDependencies(element) {
var deps = (element.getAttribute('data-dependencies') || '').trim();
if (!deps) {
var parent = element.parentElement;
if (parent && parent.tagName.toLowerCase() === 'pre') {
deps = (parent.getAttribute('data-dependencies') || '').trim();
}
}
return deps ? deps.split(/\s*,\s*/g) : [];
}
/**
* Returns whether the given language is currently loaded.
*
* @param {string} lang
* @returns {boolean}
*/
function isLoaded(lang) {
if (lang.indexOf('!') >= 0) {
// forced reload
return false;
}
lang = lang_aliases[lang] || lang; // resolve alias
if (lang in Prism.languages) {
// the given language is already loaded
return true;
}
// this will catch extensions like CSS extras that don't add a grammar to Prism.languages
var data = lang_data[lang];
return data && !data.error && data.loading === false;
}
/**
* Returns the path to a grammar, using the language_path and use_minified config keys.
*
* @param {string} lang
* @returns {string}
*/
function getLanguagePath(lang) {
return config.languages_path + 'prism-' + lang + (config.use_minified ? '.min' : '') + '.js'
}
/**
* Loads all given grammars concurrently.
*
* @param {string[]|string} languages
* @param {(languages: string[]) => void} [success]
* @param {(language: string) => void} [error] This callback will be invoked on the first language to fail.
*/
function loadLanguages(languages, success, error) {
if (typeof languages === 'string') {
languages = [languages];
}
var total = languages.length;
var completed = 0;
var failed = false;
if (total === 0) {
if (success) {
setTimeout(success, 0);
}
return;
}
function successCallback() {
if (failed) {
return;
}
completed++;
if (completed === total) {
success && success(languages);
}
}
languages.forEach(function (lang) {
loadLanguage(lang, successCallback, function () {
if (failed) {
return;
}
failed = true;
error && error(lang);
});
});
}
/**
* Loads a grammar with its dependencies.
*
* @param {string} lang
* @param {() => void} [success]
* @param {() => void} [error]
*/
function loadLanguage(lang, success, error) {
var force = lang.indexOf('!') >= 0;
lang = lang.replace('!', '');
lang = lang_aliases[lang] || lang;
function load() {
var data = lang_data[lang];
if (!data) {
data = lang_data[lang] = {
callbacks: []
};
}
data.callbacks.push({
success: success,
error: error
});
if (!force && isLoaded(lang)) {
// the language is already loaded and we aren't forced to reload
languageCallback(lang, 'success');
} else if (!force && data.error) {
// the language failed to load before and we don't reload
languageCallback(lang, 'error');
} else if (force || !data.loading) {
// the language isn't currently loading and/or we are forced to reload
data.loading = true;
data.error = false;
addScript(getLanguagePath(lang), function () {
data.loading = false;
languageCallback(lang, 'success');
}, function () {
data.loading = false;
data.error = true;
languageCallback(lang, 'error');
});
}
};
var dependencies = lang_dependencies[lang];
if (dependencies && dependencies.length) {
loadLanguages(dependencies, load, error);
} else {
load();
}
}
/**
* Runs all callbacks of the given type for the given language.
*
* @param {string} lang
* @param {"success" | "error"} type
*/
function languageCallback(lang, type) {
if (lang_data[lang]) {
var callbacks = lang_data[lang].callbacks;
for (var i = 0, l = callbacks.length; i < l; i++) {
var callback = callbacks[i][type];
if (callback) {
setTimeout(callback, 0);
}
}
callbacks.length = 0;
}
}
Prism.hooks.add('complete', function (env) {
var element = env.element;
var language = env.language;
if (!element || !language || language === ignored_language) {
return;
}
var deps = getDependencies(element);
deps.push(language);
if (!deps.every(isLoaded)) {
// the language or some dependencies aren't loaded
loadLanguages(deps, function () {
Prism.highlightElement(element);
});
}
});
}());