1
var _self = (typeof window !== 'undefined')
2
? window // if in browser
4
(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
10
* Prism: Lightweight, robust, elegant syntax highlighting
11
* MIT license http://www.opensource.org/licenses/mit-license.php/
12
* @author Lea Verou http://lea.verou.me
15
var Prism = (function(){
17
// Private helper vars
18
var lang = /\blang(?:uage)?-(\w+)\b/i;
21
var _ = _self.Prism = {
23
encode: function (tokens) {
24
if (tokens instanceof Token) {
25
return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
26
} else if (_.util.type(tokens) === 'Array') {
27
return tokens.map(_.util.encode);
29
return tokens.replace(/&/g, '&').replace(/</g, '<').replace(/\u00a0/g, ' ');
34
return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
37
objId: function (obj) {
39
Object.defineProperty(obj, '__id', { value: ++uniqueId });
44
// Deep clone a language definition (e.g. to extend it)
46
var type = _.util.type(o);
53
if (o.hasOwnProperty(key)) {
54
clone[key] = _.util.clone(o[key]);
61
// Check for existence for IE8
62
return o.map && o.map(function(v) { return _.util.clone(v); });
70
extend: function (id, redef) {
71
var lang = _.util.clone(_.languages[id]);
73
for (var key in redef) {
74
lang[key] = redef[key];
81
* Insert a token before another token in a language literal
82
* As this needs to recreate the object (we cannot actually insert before keys in object literals),
83
* we cannot just provide an object, we need anobject and a key.
84
* @param inside The key (or language id) of the parent
85
* @param before The key to insert before. If not provided, the function appends instead.
86
* @param insert Object with the key/value pairs to insert
87
* @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted.
89
insertBefore: function (inside, before, insert, root) {
90
root = root || _.languages;
91
var grammar = root[inside];
93
if (arguments.length == 2) {
94
insert = arguments[1];
96
for (var newToken in insert) {
97
if (insert.hasOwnProperty(newToken)) {
98
grammar[newToken] = insert[newToken];
107
for (var token in grammar) {
109
if (grammar.hasOwnProperty(token)) {
111
if (token == before) {
113
for (var newToken in insert) {
115
if (insert.hasOwnProperty(newToken)) {
116
ret[newToken] = insert[newToken];
121
ret[token] = grammar[token];
125
// Update references in other language definitions
126
_.languages.DFS(_.languages, function(key, value) {
127
if (value === root[inside] && key != inside) {
132
return root[inside] = ret;
135
// Traverse a language definition with Depth First Search
136
DFS: function(o, callback, type, visited) {
137
visited = visited || {};
139
if (o.hasOwnProperty(i)) {
140
callback.call(o, i, o[i], type || i);
142
if (_.util.type(o[i]) === 'Object' && !visited[_.util.objId(o[i])]) {
143
visited[_.util.objId(o[i])] = true;
144
_.languages.DFS(o[i], callback, null, visited);
146
else if (_.util.type(o[i]) === 'Array' && !visited[_.util.objId(o[i])]) {
147
visited[_.util.objId(o[i])] = true;
148
_.languages.DFS(o[i], callback, i, visited);
156
highlightAll: function(async, callback) {
157
var elements = document.querySelectorAll('code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code');
159
for (var i=0, element; element = elements[i++];) {
160
_.highlightElement(element, async === true, callback);
164
highlightElement: function(element, async, callback) {
166
var language, grammar, parent = element;
168
while (parent && !lang.test(parent.className)) {
169
parent = parent.parentNode;
173
language = (parent.className.match(lang) || [,''])[1];
174
grammar = _.languages[language];
177
// Set language on the element, if not present
178
element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
180
// Set language on the parent, for styling
181
parent = element.parentNode;
183
if (/pre/i.test(parent.nodeName)) {
184
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
187
var code = element.textContent;
196
if (!code || !grammar) {
197
_.hooks.run('complete', env);
201
_.hooks.run('before-highlight', env);
203
if (async && _self.Worker) {
204
var worker = new Worker(_.filename);
206
worker.onmessage = function(evt) {
207
env.highlightedCode = evt.data;
209
_.hooks.run('before-insert', env);
211
env.element.innerHTML = env.highlightedCode;
213
callback && callback.call(env.element);
214
_.hooks.run('after-highlight', env);
215
_.hooks.run('complete', env);
218
worker.postMessage(JSON.stringify({
219
language: env.language,
225
env.highlightedCode = _.highlight(env.code, env.grammar, env.language);
227
_.hooks.run('before-insert', env);
229
env.element.innerHTML = env.highlightedCode;
231
callback && callback.call(element);
233
_.hooks.run('after-highlight', env);
234
_.hooks.run('complete', env);
238
highlight: function (text, grammar, language) {
239
var tokens = _.tokenize(text, grammar);
240
return Token.stringify(_.util.encode(tokens), language);
243
tokenize: function(text, grammar, language) {
248
var rest = grammar.rest;
251
for (var token in rest) {
252
grammar[token] = rest[token];
258
tokenloop: for (var token in grammar) {
259
if(!grammar.hasOwnProperty(token) || !grammar[token]) {
263
var patterns = grammar[token];
264
patterns = (_.util.type(patterns) === "Array") ? patterns : [patterns];
266
for (var j = 0; j < patterns.length; ++j) {
267
var pattern = patterns[j],
268
inside = pattern.inside,
269
lookbehind = !!pattern.lookbehind,
270
lookbehindLength = 0,
271
alias = pattern.alias;
273
pattern = pattern.pattern || pattern;
275
for (var i=0; i<strarr.length; i++) { // Don’t cache length as it changes during the loop
279
if (strarr.length > text.length) {
280
// Something went terribly wrong, ABORT, ABORT!
284
if (str instanceof Token) {
288
pattern.lastIndex = 0;
290
var match = pattern.exec(str);
294
lookbehindLength = match[1].length;
297
var from = match.index - 1 + lookbehindLength,
298
match = match[0].slice(lookbehindLength),
301
before = str.slice(0, from + 1),
302
after = str.slice(to + 1);
310
var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias);
318
Array.prototype.splice.apply(strarr, args);
330
add: function (name, callback) {
331
var hooks = _.hooks.all;
333
hooks[name] = hooks[name] || [];
335
hooks[name].push(callback);
338
run: function (name, env) {
339
var callbacks = _.hooks.all[name];
341
if (!callbacks || !callbacks.length) {
345
for (var i=0, callback; callback = callbacks[i++];) {
352
var Token = _.Token = function(type, content, alias) {
354
this.content = content;
358
Token.stringify = function(o, language, parent) {
359
if (typeof o == 'string') {
363
if (_.util.type(o) === 'Array') {
364
return o.map(function(element) {
365
return Token.stringify(element, language, o);
371
content: Token.stringify(o.content, language, parent),
373
classes: ['token', o.type],
379
if (env.type == 'comment') {
380
env.attributes['spellcheck'] = 'true';
384
var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
385
Array.prototype.push.apply(env.classes, aliases);
388
_.hooks.run('wrap', env);
392
for (var name in env.attributes) {
393
attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"';
396
return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributes + '>' + env.content + '</' + env.tag + '>';
400
if (!_self.document) {
401
if (!_self.addEventListener) {
406
_self.addEventListener('message', function(evt) {
407
var message = JSON.parse(evt.data),
408
lang = message.language,
410
immediateClose = message.immediateClose;
412
_self.postMessage(_.highlight(code, _.languages[lang], lang));
413
if (immediateClose) {
421
//Get current script and highlight
422
var script = document.currentScript || [].slice.call(document.getElementsByTagName("script")).pop();
425
_.filename = script.src;
427
if (document.addEventListener && !script.hasAttribute('data-manual')) {
428
document.addEventListener('DOMContentLoaded', _.highlightAll);
436
if (typeof module !== 'undefined' && module.exports) {
437
module.exports = Prism;
440
// hack for components to work correctly in node.js
441
if (typeof global !== 'undefined') {
442
global.Prism = Prism;