prism/prism.js

894 lines
22 KiB
JavaScript
Raw Normal View History

2012-07-19 06:57:08 +08:00
/* **********************************************
Begin prism-core.js
********************************************** */
2012-07-19 06:57:08 +08:00
var _self = (typeof window !== 'undefined')
? window // if in browser
: (
(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
? self // if in worker
: {} // if in node js
);
2013-11-13 06:33:44 +08:00
2012-07-11 03:48:14 +08:00
/**
* Prism: Lightweight, robust, elegant syntax highlighting
* MIT license http://www.opensource.org/licenses/mit-license.php/
* @author Lea Verou http://lea.verou.me
*/
2013-11-13 06:33:44 +08:00
var Prism = (function(){
2012-07-11 03:48:14 +08:00
// Private helper vars
var lang = /\blang(?:uage)?-([\w-]+)\b/i;
var uniqueId = 0;
var _ = _self.Prism = {
2017-01-28 16:57:30 +08:00
manual: _self.Prism && _self.Prism.manual,
disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
util: {
2014-05-25 04:14:20 +08:00
encode: function (tokens) {
if (tokens instanceof Token) {
return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
2014-05-25 04:14:20 +08:00
} else if (_.util.type(tokens) === 'Array') {
return tokens.map(_.util.encode);
} else {
return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
}
},
type: function (o) {
return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
},
objId: function (obj) {
if (!obj['__id']) {
Object.defineProperty(obj, '__id', { value: ++uniqueId });
}
return obj['__id'];
},
// Deep clone a language definition (e.g. to extend it)
clone: function (o, visited) {
var type = _.util.type(o);
visited = visited || {};
switch (type) {
case 'Object':
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
}
var clone = {};
visited[_.util.objId(o)] = clone;
for (var key in o) {
if (o.hasOwnProperty(key)) {
clone[key] = _.util.clone(o[key], visited);
}
}
return clone;
case 'Array':
if (visited[_.util.objId(o)]) {
return visited[_.util.objId(o)];
}
var clone = [];
visited[_.util.objId(o)] = clone;
o.forEach(function (v, i) {
clone[i] = _.util.clone(v, visited);
});
return clone;
}
return o;
}
},
languages: {
extend: function (id, redef) {
var lang = _.util.clone(_.languages[id]);
for (var key in redef) {
lang[key] = redef[key];
}
return lang;
},
/**
* Insert a token before another token in a language literal
* As this needs to recreate the object (we cannot actually insert before keys in object literals),
* we cannot just provide an object, we need anobject and a key.
* @param inside The key (or language id) of the parent
* @param before The key to insert before. If not provided, the function appends instead.
* @param insert Object with the key/value pairs to insert
* @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted.
*/
insertBefore: function (inside, before, insert, root) {
root = root || _.languages;
var grammar = root[inside];
if (arguments.length == 2) {
insert = arguments[1];
for (var newToken in insert) {
if (insert.hasOwnProperty(newToken)) {
grammar[newToken] = insert[newToken];
}
}
return grammar;
}
var ret = {};
for (var token in grammar) {
if (grammar.hasOwnProperty(token)) {
if (token == before) {
for (var newToken in insert) {
if (insert.hasOwnProperty(newToken)) {
ret[newToken] = insert[newToken];
}
}
}
ret[token] = grammar[token];
}
}
var old = root[inside];
root[inside] = ret;
// Update references in other language definitions
_.languages.DFS(_.languages, function(key, value) {
if (value === old && key != inside) {
this[key] = ret;
}
});
return ret;
},
// Traverse a language definition with Depth First Search
2015-12-26 16:50:23 +08:00
DFS: function(o, callback, type, visited) {
visited = visited || {};
for (var i in o) {
if (o.hasOwnProperty(i)) {
callback.call(o, i, o[i], type || i);
if (_.util.type(o[i]) === 'Object' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
2015-12-26 16:50:23 +08:00
_.languages.DFS(o[i], callback, null, visited);
}
else if (_.util.type(o[i]) === 'Array' && !visited[_.util.objId(o[i])]) {
visited[_.util.objId(o[i])] = true;
2015-12-26 16:50:23 +08:00
_.languages.DFS(o[i], callback, i, visited);
}
}
}
}
},
plugins: {},
2012-07-27 09:17:55 +08:00
highlightAll: function(async, callback) {
_.highlightAllUnder(document, async, callback);
},
highlightAllUnder: function(container, async, callback) {
var env = {
callback: callback,
selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
};
_.hooks.run("before-highlightall", env);
var elements = env.elements || container.querySelectorAll(env.selector);
2012-07-11 03:48:14 +08:00
for (var i=0, element; element = elements[i++];) {
_.highlightElement(element, async === true, env.callback);
2012-07-11 03:48:14 +08:00
}
},
2012-07-27 09:17:55 +08:00
highlightElement: function(element, async, callback) {
// Find language
var language, grammar, parent = element;
2012-07-27 09:17:55 +08:00
while (parent && !lang.test(parent.className)) {
parent = parent.parentNode;
2012-07-11 03:48:14 +08:00
}
2012-07-27 09:17:55 +08:00
if (parent) {
2016-05-17 20:23:02 +08:00
language = (parent.className.match(lang) || [,''])[1].toLowerCase();
2012-07-27 09:17:55 +08:00
grammar = _.languages[language];
}
2012-07-11 03:48:14 +08:00
// Set language on the element, if not present
2012-07-31 15:08:31 +08:00
element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
if (element.parentNode) {
// Set language on the parent, for styling
parent = element.parentNode;
if (/pre/i.test(parent.nodeName)) {
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
}
2012-07-27 09:17:55 +08:00
}
2012-09-14 01:06:11 +08:00
var code = element.textContent;
var env = {
element: element,
language: language,
grammar: grammar,
code: code
};
2016-02-16 06:08:24 +08:00
_.hooks.run('before-sanity-check', env);
if (!env.code || !env.grammar) {
if (env.code) {
_.hooks.run('before-highlight', env);
env.element.textContent = env.code;
_.hooks.run('after-highlight', env);
}
2015-08-09 06:05:14 +08:00
_.hooks.run('complete', env);
return;
}
2012-07-16 13:36:05 +08:00
_.hooks.run('before-highlight', env);
if (async && _self.Worker) {
var worker = new Worker(_.filename);
2012-07-11 03:48:14 +08:00
worker.onmessage = function(evt) {
env.highlightedCode = evt.data;
_.hooks.run('before-insert', env);
2012-07-16 13:36:05 +08:00
env.element.innerHTML = env.highlightedCode;
2012-07-16 13:36:05 +08:00
_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(env.element);
2012-07-11 03:48:14 +08:00
};
worker.postMessage(JSON.stringify({
language: env.language,
code: env.code,
immediateClose: true
}));
2012-07-11 03:48:14 +08:00
}
else {
2015-01-09 15:11:48 +08:00
env.highlightedCode = _.highlight(env.code, env.grammar, env.language);
_.hooks.run('before-insert', env);
2012-07-16 13:36:05 +08:00
env.element.innerHTML = env.highlightedCode;
2012-07-16 13:36:05 +08:00
_.hooks.run('after-highlight', env);
_.hooks.run('complete', env);
callback && callback.call(element);
2012-07-11 03:48:14 +08:00
}
},
2013-05-10 02:01:17 +08:00
highlight: function (text, grammar, language) {
var env = {
code: text,
grammar: grammar,
language: language
};
_.hooks.run('before-tokenize', env);
env.tokens = _.tokenize(env.code, env.grammar);
_.hooks.run('after-tokenize', env);
return Token.stringify(_.util.encode(env.tokens), env.language);
},
matchGrammar: function (text, strarr, grammar, index, startPos, oneshot, target) {
var Token = _.Token;
for (var token in grammar) {
if(!grammar.hasOwnProperty(token) || !grammar[token]) {
continue;
}
if (token == target) {
return;
}
2014-08-12 17:27:26 +08:00
var patterns = grammar[token];
patterns = (_.util.type(patterns) === "Array") ? patterns : [patterns];
2014-08-12 17:27:26 +08:00
for (var j = 0; j < patterns.length; ++j) {
var pattern = patterns[j],
inside = pattern.inside,
lookbehind = !!pattern.lookbehind,
greedy = !!pattern.greedy,
lookbehindLength = 0,
alias = pattern.alias;
if (greedy && !pattern.pattern.global) {
// Without the global flag, lastIndex won't work
var flags = pattern.pattern.toString().match(/[imuy]*$/)[0];
pattern.pattern = RegExp(pattern.pattern.source, flags + "g");
}
2014-08-12 17:27:26 +08:00
pattern = pattern.pattern || pattern;
// Dont cache length as it changes during the loop
for (var i = index, pos = startPos; i < strarr.length; pos += strarr[i].length, ++i) {
2014-08-12 17:27:26 +08:00
var str = strarr[i];
2014-08-12 17:27:26 +08:00
if (strarr.length > text.length) {
// Something went terribly wrong, ABORT, ABORT!
return;
2014-08-12 17:27:26 +08:00
}
2014-08-12 17:27:26 +08:00
if (str instanceof Token) {
continue;
}
if (greedy && i != strarr.length - 1) {
pattern.lastIndex = pos;
var match = pattern.exec(text);
if (!match) {
break;
}
var from = match.index + (lookbehind ? match[1].length : 0),
to = match.index + match[0].length,
k = i,
p = pos;
for (var len = strarr.length; k < len && (p < to || (!strarr[k].type && !strarr[k - 1].greedy)); ++k) {
p += strarr[k].length;
// Move the index i to the element in strarr that is closest to from
if (from >= p) {
++i;
pos = p;
}
}
// If strarr[i] is a Token, then the match starts inside another Token, which is invalid
if (strarr[i] instanceof Token) {
continue;
}
// Number of tokens to delete and replace with the new match
delNum = k - i;
str = text.slice(pos, p);
match.index -= pos;
} else {
pattern.lastIndex = 0;
var match = pattern.exec(str),
delNum = 1;
}
2014-08-12 17:27:26 +08:00
if (!match) {
if (oneshot) {
break;
}
continue;
}
if(lookbehind) {
lookbehindLength = match[1] ? match[1].length : 0;
}
var from = match.index + lookbehindLength,
match = match[0].slice(lookbehindLength),
to = from + match.length,
before = str.slice(0, from),
after = str.slice(to);
var args = [i, delNum];
if (before) {
++i;
pos += before.length;
args.push(before);
}
var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match, greedy);
args.push(wrapped);
if (after) {
args.push(after);
2014-08-12 17:27:26 +08:00
}
Array.prototype.splice.apply(strarr, args);
if (delNum != 1)
_.matchGrammar(text, strarr, grammar, i, pos, true, token);
if (oneshot)
break;
2012-07-11 03:48:14 +08:00
}
}
}
},
tokenize: function(text, grammar, language) {
var strarr = [text];
var rest = grammar.rest;
if (rest) {
for (var token in rest) {
grammar[token] = rest[token];
}
delete grammar.rest;
}
_.matchGrammar(text, strarr, grammar, 0, 0, false);
2012-07-11 03:48:14 +08:00
return strarr;
2012-07-11 03:48:14 +08:00
},
hooks: {
all: {},
add: function (name, callback) {
var hooks = _.hooks.all;
hooks[name] = hooks[name] || [];
hooks[name].push(callback);
},
run: function (name, env) {
var callbacks = _.hooks.all[name];
if (!callbacks || !callbacks.length) {
return;
}
for (var i=0, callback; callback = callbacks[i++];) {
callback(env);
}
}
2012-07-11 03:48:14 +08:00
}
};
var Token = _.Token = function(type, content, alias, matchedStr, greedy) {
this.type = type;
this.content = content;
this.alias = alias;
// Copy of the full string this token was created from
this.length = (matchedStr || "").length|0;
this.greedy = !!greedy;
};
2013-05-11 09:37:44 +08:00
Token.stringify = function(o, language, parent) {
if (typeof o == 'string') {
return o;
}
2013-05-11 09:37:44 +08:00
2015-02-19 04:52:53 +08:00
if (_.util.type(o) === 'Array') {
2013-05-10 02:01:17 +08:00
return o.map(function(element) {
2013-05-11 09:37:44 +08:00
return Token.stringify(element, language, o);
2013-05-10 02:01:17 +08:00
}).join('');
}
var env = {
type: o.type,
2013-05-11 09:37:44 +08:00
content: Token.stringify(o.content, language, parent),
tag: 'span',
classes: ['token', o.type],
2013-05-10 02:01:17 +08:00
attributes: {},
2013-05-11 09:37:44 +08:00
language: language,
parent: parent
};
if (o.alias) {
var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
Array.prototype.push.apply(env.classes, aliases);
}
_.hooks.run('wrap', env);
var attributes = Object.keys(env.attributes).map(function(name) {
return name + '="' + (env.attributes[name] || '').replace(/"/g, '&quot;') + '"';
}).join(' ');
return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + (attributes ? ' ' + attributes : '') + '>' + env.content + '</' + env.tag + '>';
};
if (!_self.document) {
if (!_self.addEventListener) {
2013-11-13 06:33:44 +08:00
// in Node.js
return _self.Prism;
2013-11-13 06:33:44 +08:00
}
if (!_.disableWorkerMessageHandler) {
// In worker
_self.addEventListener('message', function (evt) {
var message = JSON.parse(evt.data),
lang = message.language,
code = message.code,
immediateClose = message.immediateClose;
_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
}, false);
}
return _self.Prism;
}
//Get current script and highlight
var script = document.currentScript || [].slice.call(document.getElementsByTagName("script")).pop();
if (script) {
_.filename = script.src;
if (!_.manual && !script.hasAttribute('data-manual')) {
if(document.readyState !== "loading") {
if (window.requestAnimationFrame) {
window.requestAnimationFrame(_.highlightAll);
} else {
window.setTimeout(_.highlightAll, 16);
}
}
else {
document.addEventListener('DOMContentLoaded', _.highlightAll);
}
2012-07-12 03:01:44 +08:00
}
2012-07-11 03:48:14 +08:00
}
return _self.Prism;
2013-11-13 06:33:44 +08:00
2012-07-11 03:48:14 +08:00
})();
2013-11-13 06:33:44 +08:00
if (typeof module !== 'undefined' && module.exports) {
module.exports = Prism;
}
2015-09-04 00:43:33 +08:00
// hack for components to work correctly in node.js
if (typeof global !== 'undefined') {
global.Prism = Prism;
}
/* **********************************************
Begin prism-markup.js
********************************************** */
2012-07-11 03:48:14 +08:00
Prism.languages.markup = {
2017-05-08 19:27:57 +08:00
'comment': /<!--[\s\S]*?-->/,
'prolog': /<\?[\s\S]+?\?>/,
'doctype': /<!DOCTYPE[\s\S]+?>/i,
'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
2012-07-11 03:48:14 +08:00
'tag': {
2018-03-11 19:02:50 +08:00
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+))?)*\s*\/?>/i,
2018-03-15 03:38:47 +08:00
greedy: true,
2012-07-11 03:48:14 +08:00
inside: {
'tag': {
pattern: /^<\/?[^\s>\/]+/i,
inside: {
'punctuation': /^<\/?/,
'namespace': /^[^\s>\/:]+:/
}
},
2012-07-11 03:48:14 +08:00
'attr-value': {
pattern: /=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">=]+)/i,
2012-07-11 03:48:14 +08:00
inside: {
'punctuation': [
/^=/,
{
pattern: /(^|[^\\])["']/,
lookbehind: true
}
]
2012-07-11 03:48:14 +08:00
}
},
'punctuation': /\/?>/,
'attr-name': {
pattern: /[^\s>\/]+/,
inside: {
'namespace': /^[^\s>\/:]+:/
}
}
2012-07-11 03:48:14 +08:00
}
},
'entity': /&#?[\da-z]{1,8};/i
2012-07-11 03:48:14 +08:00
};
2017-07-06 01:55:44 +08:00
Prism.languages.markup['tag'].inside['attr-value'].inside['entity'] =
Prism.languages.markup['entity'];
// Plugin to make entity title show the real entity, idea by Roman Komarov
Prism.hooks.add('wrap', function(env) {
if (env.type === 'entity') {
env.attributes['title'] = env.content.replace(/&amp;/, '&');
}
});
2015-09-04 05:39:16 +08:00
Prism.languages.xml = Prism.languages.markup;
Prism.languages.html = Prism.languages.markup;
Prism.languages.mathml = Prism.languages.markup;
Prism.languages.svg = Prism.languages.markup;
2014-02-28 02:25:09 +08:00
/* **********************************************
Begin prism-css.js
********************************************** */
Prism.languages.css = {
2017-05-08 19:27:57 +08:00
'comment': /\/\*[\s\S]*?\*\//,
'atrule': {
pattern: /@[\w-]+?.*?(?:;|(?=\s*\{))/i,
inside: {
2015-07-09 01:32:10 +08:00
'rule': /@[\w-]+/
// See rest below
}
},
'url': /url\((?:(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|.*?)\)/i,
'selector': /[^{}\s][^{};]*?(?=\s*\{)/,
2016-08-17 21:56:41 +08:00
'string': {
pattern: /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
2016-08-17 21:56:41 +08:00
greedy: true
},
'property': /[-_a-z\xA0-\uFFFF][-\w\xA0-\uFFFF]*(?=\s*:)/i,
'important': /!important\b/i,
'function': /[-a-z0-9]+(?=\()/i,
'punctuation': /[(){};:]/
};
Prism.languages.css['atrule'].inside.rest = Prism.languages.css;
2015-07-09 01:32:10 +08:00
if (Prism.languages.markup) {
Prism.languages.insertBefore('markup', 'tag', {
'style': {
2017-05-08 19:27:57 +08:00
pattern: /(<style[\s\S]*?>)[\s\S]*?(?=<\/style>)/i,
lookbehind: true,
inside: Prism.languages.css,
alias: 'language-css',
greedy: true
}
});
Prism.languages.insertBefore('inside', 'attr-value', {
'style-attr': {
pattern: /\s*style=("|')(?:\\[\s\S]|(?!\1)[^\\])*\1/i,
inside: {
'attr-name': {
pattern: /^\s*style/i,
inside: Prism.languages.markup.tag.inside
},
'punctuation': /^\s*=\s*['"]|['"]\s*$/,
'attr-value': {
pattern: /.+/i,
inside: Prism.languages.css
}
},
alias: 'language-css'
}
}, Prism.languages.markup.tag);
2012-07-11 03:48:14 +08:00
}
/* **********************************************
Begin prism-clike.js
********************************************** */
Prism.languages.clike = {
2014-08-12 18:11:31 +08:00
'comment': [
{
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
2014-08-12 18:11:31 +08:00
lookbehind: true
},
{
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true
2014-08-12 18:11:31 +08:00
}
],
'string': {
pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true
},
'class-name': {
pattern: /((?:\b(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[\w.\\]+/i,
2013-05-18 07:57:08 +08:00
lookbehind: true,
inside: {
punctuation: /[.\\]/
2013-05-18 07:57:08 +08:00
}
},
'keyword': /\b(?:if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,
'boolean': /\b(?:true|false)\b/,
'function': /\w+(?=\()/,
'number': /\b0x[\da-f]+\b|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?/i,
'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,
'punctuation': /[{}[\];(),.:]/
};
2014-02-28 02:25:09 +08:00
/* **********************************************
Begin prism-javascript.js
********************************************** */
Prism.languages.javascript = Prism.languages.extend('clike', {
'class-name': [
Prism.languages.clike['class-name'],
{
pattern: /(^|[^$\w\xA0-\uFFFF])[_$A-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\.(?:prototype|constructor))/,
lookbehind: true
}
],
'keyword': [
{
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
lookbehind: true
},
/\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/
],
'number': /\b(?:(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+)n?|\d+n|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\(|\.(?:apply|bind|call)\()/,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});
Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/
Prism.languages.insertBefore('javascript', 'keyword', {
'regex': {
2018-04-13 14:59:26 +08:00
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
lookbehind: true,
greedy: true
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,
alias: 'function'
},
'constant': /\b[A-Z][A-Z\d_]*\b/
});
Prism.languages.insertBefore('javascript', 'string', {
2015-06-15 00:26:16 +08:00
'template-string': {
pattern: /`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,
greedy: true,
2015-06-15 00:26:16 +08:00
inside: {
'interpolation': {
pattern: /\${[^}]+}/,
2015-06-15 00:26:16 +08:00
inside: {
'interpolation-punctuation': {
pattern: /^\${|}$/,
2015-06-15 00:26:16 +08:00
alias: 'punctuation'
},
rest: Prism.languages.javascript
2015-06-15 00:26:16 +08:00
}
},
'string': /[\s\S]+/
}
}
});
if (Prism.languages.markup) {
Prism.languages.insertBefore('markup', 'tag', {
'script': {
2017-05-08 19:27:57 +08:00
pattern: /(<script[\s\S]*?>)[\s\S]*?(?=<\/script>)/i,
lookbehind: true,
inside: Prism.languages.javascript,
alias: 'language-javascript',
greedy: true
}
});
2013-05-19 08:20:58 +08:00
}
2015-09-04 05:39:16 +08:00
Prism.languages.js = Prism.languages.javascript;
2014-02-28 02:25:09 +08:00
2017-07-06 01:55:44 +08:00
2013-05-19 08:20:58 +08:00
/* **********************************************
Begin prism-file-highlight.js
********************************************** */
2015-03-21 06:48:56 +08:00
(function () {
2015-09-04 00:43:33 +08:00
if (typeof self === 'undefined' || !self.Prism || !self.document || !document.querySelector) {
2015-03-21 06:48:56 +08:00
return;
}
2013-05-19 08:20:58 +08:00
2015-03-21 06:48:56 +08:00
self.Prism.fileHighlight = function() {
var Extensions = {
'js': 'javascript',
'py': 'python',
'rb': 'ruby',
'ps1': 'powershell',
'psm1': 'powershell',
'sh': 'bash',
'bat': 'batch',
'h': 'c',
'tex': 'latex'
2015-03-21 06:48:56 +08:00
};
2013-05-19 08:20:58 +08:00
2016-07-03 17:59:59 +08:00
Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function (pre) {
var src = pre.getAttribute('data-src');
2016-07-03 17:59:59 +08:00
var language, parent = pre;
var lang = /\blang(?:uage)?-([\w-]+)\b/i;
2016-07-03 17:59:59 +08:00
while (parent && !lang.test(parent.className)) {
parent = parent.parentNode;
}
2016-07-03 17:59:59 +08:00
if (parent) {
language = (pre.className.match(lang) || [, ''])[1];
}
2016-07-03 17:59:59 +08:00
if (!language) {
var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
language = Extensions[extension] || extension;
}
2013-05-19 08:20:58 +08:00
2016-07-03 17:59:59 +08:00
var code = document.createElement('code');
code.className = 'language-' + language;
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
pre.textContent = '';
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
code.textContent = 'Loading…';
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
pre.appendChild(code);
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
var xhr = new XMLHttpRequest();
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
xhr.open('GET', src, true);
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
if (xhr.status < 400 && xhr.responseText) {
code.textContent = xhr.responseText;
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
Prism.highlightElement(code);
}
else if (xhr.status >= 400) {
code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
2015-03-21 06:48:56 +08:00
}
2016-07-03 17:59:59 +08:00
else {
code.textContent = '✖ Error: File does not exist or is empty';
}
}
};
2015-03-21 06:48:56 +08:00
2016-07-03 17:59:59 +08:00
xhr.send(null);
});
2013-05-19 08:20:58 +08:00
if (Prism.plugins.toolbar) {
Prism.plugins.toolbar.registerButton('download-file', function (env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName) || !pre.hasAttribute('data-src') || !pre.hasAttribute('data-download-link')) {
return;
}
var src = pre.getAttribute('data-src');
var a = document.createElement('a');
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
a.setAttribute('download', '');
a.href = src;
return a;
});
}
2013-05-19 08:20:58 +08:00
};
2015-03-21 06:48:56 +08:00
document.addEventListener('DOMContentLoaded', self.Prism.fileHighlight);
2013-05-19 08:20:58 +08:00
})();