Blame view

bower_components/prism/plugins/autoloader/prism-autoloader.js 5.14 KB
eb240478   Luigi Serra   public room cards...
1
2
3
4
5
6
  (function () {
  	if (typeof self === 'undefined' || !self.Prism || !self.document || !document.createElement) {
  		return;
  	}
  
  	// The dependencies map is built automatically with gulp
f748e9cf   Luigi Serra   new controllet an...
7
  	var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","aspnet":"markup","bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","fsharp":"clike","glsl":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup","haxe":"clike","jade":"javascript","java":"clike","kotlin":"clike","less":"css","markdown":"markup","nginx":"clike","objectivec":"c","parser":"markup","php":"clike","php-extras":"php","processing":"clike","qore":"clike","jsx":["markup","javascript"],"ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup","swift":"clike","textile":"markup","twig":"markup","typescript":"javascript","wiki":"markup"}/*]*/;
eb240478   Luigi Serra   public room cards...
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
  
  	var lang_data = {};
  
  	var config = Prism.plugins.autoloader = {
  		languages_path: 'components/',
  		use_minified: true
  	};
  
  	/**
  	 * Lazy loads an external script
  	 * @param {string} src
  	 * @param {function=} success
  	 * @param {function=} error
  	 */
  	var script = function (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 the path to a grammar, using the language_path and use_minified config keys.
  	 * @param {string} lang
  	 * @returns {string}
  	 */
  	var getLanguagePath = function (lang) {
  		return config.languages_path +
  			'prism-' + lang
  			+ (config.use_minified ? '.min' : '') + '.js'
  	};
  
  	/**
  	 * Tries to load a grammar and
  	 * highlight again the given element once loaded.
  	 * @param {string} lang
  	 * @param {HTMLElement} elt
  	 */
  	var registerElement = function (lang, elt) {
  		var data = lang_data[lang];
  		if (!data) {
  			data = lang_data[lang] = {};
  		}
  
  		// Look for additional dependencies defined on the <code> or <pre> tags
  		var deps = elt.getAttribute('data-dependencies');
  		if (!deps && elt.parentNode && elt.parentNode.tagName.toLowerCase() === 'pre') {
  			deps = elt.parentNode.getAttribute('data-dependencies');
  		}
  
  		if (deps) {
  			deps = deps.split(/\s*,\s*/g);
  		} else {
  			deps = [];
  		}
  
  		loadLanguages(deps, function () {
  			loadLanguage(lang, function () {
  				Prism.highlightElement(elt);
  			});
  		});
  	};
  
  	/**
  	 * Sequentially loads an array of grammars.
  	 * @param {string[]|string} langs
  	 * @param {function=} success
  	 * @param {function=} error
  	 */
  	var loadLanguages = function (langs, success, error) {
  		if (typeof langs === 'string') {
  			langs = [langs];
  		}
  		var i = 0;
  		var l = langs.length;
  		var f = function () {
  			if (i < l) {
  				loadLanguage(langs[i], function () {
  					i++;
  					f();
  				}, function () {
  					error && error(langs[i]);
  				});
  			} else if (i === l) {
  				success && success(langs);
  			}
  		};
  		f();
  	};
  
  	/**
  	 * Load a grammar with its dependencies
  	 * @param {string} lang
  	 * @param {function=} success
  	 * @param {function=} error
  	 */
  	var loadLanguage = function (lang, success, error) {
  		var load = function () {
  			var force = false;
  			// Do we want to force reload the grammar?
  			if (lang.indexOf('!') >= 0) {
  				force = true;
  				lang = lang.replace('!', '');
  			}
  
  			var data = lang_data[lang];
  			if (!data) {
  				data = lang_data[lang] = {};
  			}
  			if (success) {
  				if (!data.success_callbacks) {
  					data.success_callbacks = [];
  				}
  				data.success_callbacks.push(success);
  			}
  			if (error) {
  				if (!data.error_callbacks) {
  					data.error_callbacks = [];
  				}
  				data.error_callbacks.push(error);
  			}
  
  			if (!force && Prism.languages[lang]) {
  				languageSuccess(lang);
  			} else if (!force && data.error) {
  				languageError(lang);
  			} else if (force || !data.loading) {
  				data.loading = true;
  				var src = getLanguagePath(lang);
  				script(src, function () {
  					data.loading = false;
  					languageSuccess(lang);
  
  				}, function () {
  					data.loading = false;
  					data.error = true;
  					languageError(lang);
  				});
  			}
  		};
  		var dependencies = lang_dependencies[lang];
  		if(dependencies && dependencies.length) {
  			loadLanguages(dependencies, load);
  		} else {
  			load();
  		}
  	};
  
  	/**
  	 * Runs all success callbacks for this language.
  	 * @param {string} lang
  	 */
  	var languageSuccess = function (lang) {
  		if (lang_data[lang] && lang_data[lang].success_callbacks && lang_data[lang].success_callbacks.length) {
  			lang_data[lang].success_callbacks.forEach(function (f) {
  				f(lang);
  			});
  		}
  	};
  
  	/**
  	 * Runs all error callbacks for this language.
  	 * @param {string} lang
  	 */
  	var languageError = function (lang) {
  		if (lang_data[lang] && lang_data[lang].error_callbacks && lang_data[lang].error_callbacks.length) {
  			lang_data[lang].error_callbacks.forEach(function (f) {
  				f(lang);
  			});
  		}
  	};
  
  	Prism.hooks.add('complete', function (env) {
  		if (env.element && env.language && !env.grammar) {
  			registerElement(env.language, env.element);
  		}
  	});
  
  }());