Update Prism.js

pull/447/merge
Thibaut Courouble 9 years ago
parent 3036c712e9
commit 4ad1898902

@ -1,4 +1,4 @@
/* http://prismjs.com/download.html?themes=prism&languages=markup+css+clike+javascript+c+cpp+coffeescript+ruby+elixir+go+json+kotlin+lua+nginx+perl+php+python+rust+sql+typescript */ /* http://prismjs.com/download.html?themes=prism&languages=markup+css+clike+javascript+c+cpp+coffeescript+ruby+elixir+go+json+kotlin+lua+nginx+perl+php+python+crystal+rust+sql+typescript */
var _self = (typeof window !== 'undefined') var _self = (typeof window !== 'undefined')
? window // if in browser ? window // if in browser
: ( : (
@ -281,9 +281,16 @@ var _ = _self.Prism = {
lookbehindLength = 0, lookbehindLength = 0,
alias = pattern.alias; 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");
}
pattern = pattern.pattern || pattern; pattern = pattern.pattern || pattern;
for (var i=0; i<strarr.length; i++) { // Dont cache length as it changes during the loop // Dont cache length as it changes during the loop
for (var i=0, pos = 0; i<strarr.length; pos += (strarr[i].matchedStr || strarr[i]).length, ++i) {
var str = strarr[i]; var str = strarr[i];
@ -303,40 +310,38 @@ var _ = _self.Prism = {
// Greedy patterns can override/remove up to two previously matched tokens // Greedy patterns can override/remove up to two previously matched tokens
if (!match && greedy && i != strarr.length - 1) { if (!match && greedy && i != strarr.length - 1) {
// Reconstruct the original text using the next two tokens pattern.lastIndex = pos;
var nextToken = strarr[i + 1].matchedStr || strarr[i + 1], match = pattern.exec(text);
combStr = str + nextToken; if (!match) {
break;
if (i < strarr.length - 2) {
combStr += strarr[i + 2].matchedStr || strarr[i + 2];
} }
// Try the pattern again on the reconstructed text var from = match.index + (lookbehind ? match[1].length : 0),
pattern.lastIndex = 0; to = match.index + match[0].length,
match = pattern.exec(combStr); k = i,
if (!match) { p = pos;
continue;
for (var len = strarr.length; k < len && p < to; ++k) {
p += (strarr[k].matchedStr || strarr[k]).length;
// Move the index i to the element in strarr that is closest to from
if (from >= p) {
++i;
pos = p;
}
} }
var from = match.index + (lookbehind ? match[1].length : 0); /*
// To be a valid candidate, the new match has to start inside of str * If strarr[i] is a Token, then the match starts inside another Token, which is invalid
if (from >= str.length) { * If strarr[k - 1] is greedy we are in conflict with another greedy pattern
*/
if (strarr[i] instanceof Token || strarr[k - 1].greedy) {
continue; continue;
} }
var to = match.index + match[0].length,
len = str.length + nextToken.length;
// Number of tokens to delete and replace with the new match // Number of tokens to delete and replace with the new match
delNum = 3; delNum = k - i;
str = text.slice(pos, p);
if (to <= len) { match.index -= pos;
if (strarr[i + 1].greedy) {
continue;
}
delNum = 2;
combStr = combStr.slice(0, len);
}
str = combStr;
} }
if (!match) { if (!match) {
@ -447,7 +452,7 @@ Token.stringify = function(o, language, parent) {
attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"'; attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"';
} }
return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributes + '>' + env.content + '</' + env.tag + '>'; return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + (attributes ? ' ' + attributes : '') + '>' + env.content + '</' + env.tag + '>';
}; };
@ -480,7 +485,11 @@ Token.stringify = function(o, language, parent) {
// if (document.addEventListener && !script.hasAttribute('data-manual')) { // if (document.addEventListener && !script.hasAttribute('data-manual')) {
// if(document.readyState !== "loading") { // if(document.readyState !== "loading") {
// requestAnimationFrame(_.highlightAll, 0); // if (window.requestAnimationFrame) {
// window.requestAnimationFrame(_.highlightAll);
// } else {
// window.setTimeout(_.highlightAll, 16);
// }
// } // }
// else { // else {
// document.addEventListener('DOMContentLoaded', _.highlightAll); // document.addEventListener('DOMContentLoaded', _.highlightAll);
@ -507,7 +516,7 @@ Prism.languages.markup = {
'doctype': /<!DOCTYPE[\w\W]+?>/, 'doctype': /<!DOCTYPE[\w\W]+?>/,
'cdata': /<!\[CDATA\[[\w\W]*?]]>/i, 'cdata': /<!\[CDATA\[[\w\W]*?]]>/i,
'tag': { 'tag': {
pattern: /<\/?(?!\d)[^\s>\/=.$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i, pattern: /<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i,
inside: { inside: {
'tag': { 'tag': {
pattern: /^<\/?[^\s>\/]+/i, pattern: /^<\/?[^\s>\/]+/i,
@ -630,7 +639,8 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
'keyword': /\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|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/, 'keyword': /\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|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-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/, 'number': /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444) // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i 'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i,
'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*\*?|\/|~|\^|%|\.{3}/
}); });
Prism.languages.insertBefore('javascript', 'keyword', { Prism.languages.insertBefore('javascript', 'keyword', {
@ -1285,7 +1295,8 @@ Prism.languages.php = Prism.languages.extend('clike', {
'constant': /\b[A-Z0-9_]{2,}\b/, 'constant': /\b[A-Z0-9_]{2,}\b/,
'comment': { 'comment': {
pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/,
lookbehind: true lookbehind: true,
greedy: true
} }
}); });
@ -1331,7 +1342,6 @@ if (Prism.languages.markup) {
env.tokenStack = []; env.tokenStack = [];
env.backupCode = env.code;
env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) { env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) {
env.tokenStack.push(match); env.tokenStack.push(match);
@ -1339,14 +1349,6 @@ if (Prism.languages.markup) {
}); });
}); });
// Restore env.code for other plugins (e.g. line-numbers)
Prism.hooks.add('before-insert', function(env) {
if (env.language === 'php') {
env.code = env.backupCode;
delete env.backupCode;
}
});
// Re-insert the tokens after highlighting // Re-insert the tokens after highlighting
Prism.hooks.add('after-highlight', function(env) { Prism.hooks.add('after-highlight', function(env) {
if (env.language !== 'php') { if (env.language !== 'php') {
@ -1406,6 +1408,60 @@ Prism.languages.python= {
'punctuation' : /[{}[\];(),.:]/ 'punctuation' : /[{}[\];(),.:]/
}; };
(function(Prism) {
Prism.languages.crystal = Prism.languages.extend('ruby', {
keyword: [
/\b(?:abstract|alias|as|asm|begin|break|case|class|def|do|else|elsif|end|ensure|enum|extend|for|fun|if|ifdef|include|instance_sizeof|lib|macro|module|next|of|out|pointerof|private|protected|rescue|return|require|self|sizeof|struct|super|then|type|typeof|union|unless|until|when|while|with|yield|__DIR__|__FILE__|__LINE__)\b/,
{
pattern: /(\.\s*)(?:is_a|responds_to)\?/,
lookbehind: true
}
],
number: /\b(?:0b[01_]*[01]|0o[0-7_]*[0-7]|0x[0-9a-fA-F_]*[0-9a-fA-F]|(?:[0-9](?:[0-9_]*[0-9])?)(?:\.[0-9_]*[0-9])?(?:[eE][+-]?[0-9_]*[0-9])?)(?:_(?:[uif](?:8|16|32|64))?)?\b/,
});
var rest = Prism.util.clone(Prism.languages.crystal);
Prism.languages.insertBefore('crystal', 'string', {
attribute: {
pattern: /@\[.+?\]/,
alias: 'attr-name',
inside: {
delimiter: {
pattern: /^@\[|\]$/,
alias: 'tag'
},
rest: rest
}
},
expansion: [
{
pattern: /\{\{.+?\}\}/,
inside: {
delimiter: {
pattern: /^\{\{|\}\}$/,
alias: 'tag'
},
rest: rest
}
},
{
pattern: /\{%.+?%\}/,
inside: {
delimiter: {
pattern: /^\{%|%\}$/,
alias: 'tag'
},
rest: rest
}
}
]
});
}(Prism));
/* TODO /* TODO
Add support for Markdown notation inside doc comments Add support for Markdown notation inside doc comments
Add support for nested block comments... Add support for nested block comments...

Loading…
Cancel
Save