Update Prism.js

pull/630/head
Thibaut Courouble 8 years ago
parent 7024022e6b
commit d126ad9fa2

@ -1,4 +1,4 @@
/* http://prismjs.com/download.html?themes=prism&languages=markup+css+clike+javascript+c+cpp+coffeescript+ruby+elixir+go+java+json+kotlin+lua+nginx+perl+php+python+crystal+rust+scss+sql+typescript */ /* http://prismjs.com/download.html?themes=prism&languages=markup+css+clike+javascript+c+cpp+coffeescript+ruby+elixir+erlang+go+java+json+kotlin+lua+nginx+perl+php+python+crystal+rust+scss+sql+typescript */
var _self = (typeof window !== 'undefined') var _self = (typeof window !== 'undefined')
? window // if in browser ? window // if in browser
: ( : (
@ -206,7 +206,9 @@ var _ = _self.Prism = {
if (!env.code || !env.grammar) { if (!env.code || !env.grammar) {
if (env.code) { if (env.code) {
_.hooks.run('before-highlight', env);
env.element.textContent = env.code; env.element.textContent = env.code;
_.hooks.run('after-highlight', env);
} }
_.hooks.run('complete', env); _.hooks.run('complete', env);
return; return;
@ -254,26 +256,18 @@ var _ = _self.Prism = {
return Token.stringify(_.util.encode(tokens), language); return Token.stringify(_.util.encode(tokens), language);
}, },
tokenize: function(text, grammar, language) { matchGrammar: function (text, strarr, grammar, index, startPos, oneshot, target) {
var Token = _.Token; var Token = _.Token;
var strarr = [text]; for (var token in grammar) {
var rest = grammar.rest;
if (rest) {
for (var token in rest) {
grammar[token] = rest[token];
}
delete grammar.rest;
}
tokenloop: for (var token in grammar) {
if(!grammar.hasOwnProperty(token) || !grammar[token]) { if(!grammar.hasOwnProperty(token) || !grammar[token]) {
continue; continue;
} }
if (token == target) {
return;
}
var patterns = grammar[token]; var patterns = grammar[token];
patterns = (_.util.type(patterns) === "Array") ? patterns : [patterns]; patterns = (_.util.type(patterns) === "Array") ? patterns : [patterns];
@ -294,13 +288,13 @@ var _ = _self.Prism = {
pattern = pattern.pattern || pattern; pattern = pattern.pattern || pattern;
// 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].length, ++i) { for (var i = index, pos = startPos; i < strarr.length; pos += strarr[i].length, ++i) {
var str = strarr[i]; var str = strarr[i];
if (strarr.length > text.length) { if (strarr.length > text.length) {
// Something went terribly wrong, ABORT, ABORT! // Something went terribly wrong, ABORT, ABORT!
break tokenloop; return;
} }
if (str instanceof Token) { if (str instanceof Token) {
@ -325,7 +319,7 @@ var _ = _self.Prism = {
k = i, k = i,
p = pos; p = pos;
for (var len = strarr.length; k < len && p < to; ++k) { for (var len = strarr.length; k < len && (p < to || (!strarr[k].type && !strarr[k - 1].greedy)); ++k) {
p += strarr[k].length; p += strarr[k].length;
// Move the index i to the element in strarr that is closest to from // Move the index i to the element in strarr that is closest to from
if (from >= p) { if (from >= p) {
@ -349,6 +343,10 @@ var _ = _self.Prism = {
} }
if (!match) { if (!match) {
if (oneshot) {
break;
}
continue; continue;
} }
@ -365,6 +363,8 @@ var _ = _self.Prism = {
var args = [i, delNum]; var args = [i, delNum];
if (before) { if (before) {
++i;
pos += before.length;
args.push(before); args.push(before);
} }
@ -377,9 +377,31 @@ var _ = _self.Prism = {
} }
Array.prototype.splice.apply(strarr, args); Array.prototype.splice.apply(strarr, args);
if (delNum != 1)
_.matchGrammar(text, strarr, grammar, i, pos, true, token);
if (oneshot)
break;
} }
} }
} }
},
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);
return strarr; return strarr;
}, },
@ -513,12 +535,12 @@ if (typeof global !== 'undefined') {
} }
; ;
Prism.languages.markup = { Prism.languages.markup = {
'comment': /<!--[\w\W]*?-->/, 'comment': /<!--[\s\S]*?-->/,
'prolog': /<\?[\w\W]+?\?>/, 'prolog': /<\?[\s\S]+?\?>/,
'doctype': /<!DOCTYPE[\w\W]+?>/i, 'doctype': /<!DOCTYPE[\s\S]+?>/i,
'cdata': /<!\[CDATA\[[\w\W]*?]]>/i, 'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
'tag': { 'tag': {
pattern: /<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i, pattern: /<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\s\S])*\1|[^\s'">=]+))?)*\s*\/?>/i,
inside: { inside: {
'tag': { 'tag': {
pattern: /^<\/?[^\s>\/]+/i, pattern: /^<\/?[^\s>\/]+/i,
@ -528,7 +550,7 @@ Prism.languages.markup = {
} }
}, },
'attr-value': { 'attr-value': {
pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i, pattern: /=(?:('|")[\s\S]*?(\1)|[^\s>]+)/i,
inside: { inside: {
'punctuation': /[=>"']/ 'punctuation': /[=>"']/
} }
@ -560,7 +582,7 @@ Prism.languages.mathml = Prism.languages.markup;
Prism.languages.svg = Prism.languages.markup; Prism.languages.svg = Prism.languages.markup;
Prism.languages.css = { Prism.languages.css = {
'comment': /\/\*[\w\W]*?\*\//, 'comment': /\/\*[\s\S]*?\*\//,
'atrule': { 'atrule': {
pattern: /@[\w-]+?.*?(;|(?=\s*\{))/i, pattern: /@[\w-]+?.*?(;|(?=\s*\{))/i,
inside: { inside: {
@ -568,10 +590,10 @@ Prism.languages.css = {
// See rest below // See rest below
} }
}, },
'url': /url\((?:(["'])(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1|.*?)\)/i, 'url': /url\((?:(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|.*?)\)/i,
'selector': /[^\{\}\s][^\{\};]*?(?=\s*\{)/, 'selector': /[^\{\}\s][^\{\};]*?(?=\s*\{)/,
'string': { 'string': {
pattern: /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/, pattern: /("|')(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true greedy: true
}, },
'property': /(\b|\B)[\w-]+(?=\s*:)/i, 'property': /(\b|\B)[\w-]+(?=\s*:)/i,
@ -585,7 +607,7 @@ Prism.languages.css['atrule'].inside.rest = Prism.util.clone(Prism.languages.css
if (Prism.languages.markup) { if (Prism.languages.markup) {
Prism.languages.insertBefore('markup', 'tag', { Prism.languages.insertBefore('markup', 'tag', {
'style': { 'style': {
pattern: /(<style[\w\W]*?>)[\w\W]*?(?=<\/style>)/i, pattern: /(<style[\s\S]*?>)[\s\S]*?(?=<\/style>)/i,
lookbehind: true, lookbehind: true,
inside: Prism.languages.css, inside: Prism.languages.css,
alias: 'language-css' alias: 'language-css'
@ -613,7 +635,7 @@ if (Prism.languages.markup) {
Prism.languages.clike = { Prism.languages.clike = {
'comment': [ 'comment': [
{ {
pattern: /(^|[^\\])\/\*[\w\W]*?\*\//, pattern: /(^|[^\\])\/\*[\s\S]*?\*\//,
lookbehind: true lookbehind: true
}, },
{ {
@ -645,7 +667,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
'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}/ 'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
}); });
Prism.languages.insertBefore('javascript', 'keyword', { Prism.languages.insertBefore('javascript', 'keyword', {
@ -679,7 +701,7 @@ Prism.languages.insertBefore('javascript', 'string', {
if (Prism.languages.markup) { if (Prism.languages.markup) {
Prism.languages.insertBefore('markup', 'tag', { Prism.languages.insertBefore('markup', 'tag', {
'script': { 'script': {
pattern: /(<script[\w\W]*?>)[\w\W]*?(?=<\/script>)/i, pattern: /(<script[\s\S]*?>)[\s\S]*?(?=<\/script>)/i,
lookbehind: true, lookbehind: true,
inside: Prism.languages.javascript, inside: Prism.languages.javascript,
alias: 'language-javascript' alias: 'language-javascript'
@ -833,7 +855,10 @@ delete Prism.languages.coffeescript['template-string'];
*/ */
(function(Prism) { (function(Prism) {
Prism.languages.ruby = Prism.languages.extend('clike', { Prism.languages.ruby = Prism.languages.extend('clike', {
'comment': /#(?!\{[^\r\n]*?\}).*/, 'comment': [
/#(?!\{[^\r\n]*?\}).*/,
/^=begin(?:\r?\n|\r)(?:.*(?:\r?\n|\r))*?=end/m
],
'keyword': /\b(alias|and|BEGIN|begin|break|case|class|def|define_method|defined|do|each|else|elsif|END|end|ensure|false|for|if|in|module|new|next|nil|not|or|raise|redo|require|rescue|retry|return|self|super|then|throw|true|undef|unless|until|when|while|yield)\b/ 'keyword': /\b(alias|and|BEGIN|begin|break|case|class|def|define_method|defined|do|each|else|elsif|END|end|ensure|false|for|if|in|module|new|next|nil|not|or|raise|redo|require|rescue|retry|return|self|super|then|throw|true|undef|unless|until|when|while|yield)\b/
}); });
@ -1385,9 +1410,8 @@ Prism.languages.php = Prism.languages.extend('clike', {
'keyword': /\b(and|or|xor|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|return|static|switch|use|require|require_once|var|while|abstract|interface|public|implements|private|protected|parent|throw|null|echo|print|trait|namespace|final|yield|goto|instanceof|finally|try|catch)\b/i, 'keyword': /\b(and|or|xor|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|return|static|switch|use|require|require_once|var|while|abstract|interface|public|implements|private|protected|parent|throw|null|echo|print|trait|namespace|final|yield|goto|instanceof|finally|try|catch)\b/i,
'constant': /\b[A-Z0-9_]{2,}\b/, 'constant': /\b[A-Z0-9_]{2,}\b/,
'comment': { 'comment': {
pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
lookbehind: true, lookbehind: true
greedy: true
} }
}); });
@ -1402,7 +1426,10 @@ Prism.languages.insertBefore('php', 'class-name', {
}); });
Prism.languages.insertBefore('php', 'keyword', { Prism.languages.insertBefore('php', 'keyword', {
'delimiter': /\?>|<\?(?:php)?/i, 'delimiter': {
pattern: /\?>|<\?(?:php|=)?/i,
alias: 'important'
},
'variable': /\$\w+\b/i, 'variable': /\$\w+\b/i,
'package': { 'package': {
pattern: /(\\|namespace\s+|use\s+)[\w\\]+/, pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
@ -1421,29 +1448,38 @@ Prism.languages.insertBefore('php', 'operator', {
} }
}); });
// Add HTML support of the markup language exists // Add HTML support if the markup language exists
if (Prism.languages.markup) { if (Prism.languages.markup) {
// Tokenize all inline PHP blocks that are wrapped in <?php ?> // Tokenize all inline PHP blocks that are wrapped in <?php ?>
// This allows for easy PHP + markup highlighting // This allows for easy PHP + markup highlighting
Prism.hooks.add('before-highlight', function(env) { Prism.hooks.add('before-highlight', function(env) {
if (env.language !== 'php') { if (env.language !== 'php' || !/(?:<\?php|<\?)/ig.test(env.code)) {
return; return;
} }
env.tokenStack = []; env.tokenStack = [];
env.backupCode = env.code; env.backupCode = env.code;
env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) { env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig, function(match) {
env.tokenStack.push(match); var i = env.tokenStack.length;
// Check for existing strings
while (env.backupCode.indexOf('___PHP' + i + '___') !== -1)
++i;
// Create a sparse array
env.tokenStack[i] = match;
return '{{{PHP' + env.tokenStack.length + '}}}'; return '___PHP' + i + '___';
}); });
// Switch the grammar to markup
env.grammar = Prism.languages.markup;
}); });
// Restore env.code for other plugins (e.g. line-numbers) // Restore env.code for other plugins (e.g. line-numbers)
Prism.hooks.add('before-insert', function(env) { Prism.hooks.add('before-insert', function(env) {
if (env.language === 'php') { if (env.language === 'php' && env.backupCode) {
env.code = env.backupCode; env.code = env.backupCode;
delete env.backupCode; delete env.backupCode;
} }
@ -1451,32 +1487,25 @@ if (Prism.languages.markup) {
// 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' || !env.tokenStack) {
return; return;
} }
for (var i = 0, t; t = env.tokenStack[i]; i++) { // Switch the grammar back
// The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns env.grammar = Prism.languages.php;
env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$'));
}
env.element.innerHTML = env.highlightedCode; for (var i = 0, keys = Object.keys(env.tokenStack); i < keys.length; ++i) {
}); var k = keys[i];
var t = env.tokenStack[k];
// Wrap tokens in classes that are missing them // The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns
Prism.hooks.add('wrap', function(env) { env.highlightedCode = env.highlightedCode.replace('___PHP' + k + '___',
if (env.language === 'php' && env.type === 'markup') { "<span class=\"token php language-php\">" +
env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\})/g, "<span class=\"token php\">$1</span>"); Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$') +
"</span>");
} }
});
// Add the rules before all others env.element.innerHTML = env.highlightedCode;
Prism.languages.insertBefore('php', 'comment', {
'markup': {
pattern: /<[^?]\/?(.*?)>/,
inside: Prism.languages.markup
},
'php': /\{\{\{PHP[0-9]+\}\}\}/
}); });
} }
; ;
@ -1518,7 +1547,7 @@ Prism.languages.python= {
} }
], ],
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/, number: /\b(?:0b[01_]*[01]|0o[0-7_]*[0-7]|0x[0-9a-fA-F_]*[0-9a-fA-F]|(?:\d(?:[0-9_]*\d)?)(?:\.[0-9_]*\d)?(?:[eE][+-]?[0-9_]*\d)?)(?:_(?:[uif](?:8|16|32|64))?)?\b/,
}); });
var rest = Prism.util.clone(Prism.languages.crystal); var rest = Prism.util.clone(Prism.languages.crystal);
@ -1572,7 +1601,7 @@ Prism.languages.python= {
Prism.languages.rust = { Prism.languages.rust = {
'comment': [ 'comment': [
{ {
pattern: /(^|[^\\])\/\*[\w\W]*?\*\//, pattern: /(^|[^\\])\/\*[\s\S]*?\*\//,
lookbehind: true lookbehind: true
}, },
{ {
@ -1624,7 +1653,7 @@ Prism.languages.rust = {
}; };
Prism.languages.scss = Prism.languages.extend('css', { Prism.languages.scss = Prism.languages.extend('css', {
'comment': { 'comment': {
pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
lookbehind: true lookbehind: true
}, },
'atrule': { 'atrule': {
@ -1699,7 +1728,7 @@ Prism.languages.insertBefore('scss', 'function', {
Prism.languages.scss['atrule'].inside.rest = Prism.util.clone(Prism.languages.scss); Prism.languages.scss['atrule'].inside.rest = Prism.util.clone(Prism.languages.scss);
Prism.languages.sql= { Prism.languages.sql= {
'comment': { 'comment': {
pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|(?:--|\/\/|#).*)/, pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|(?:--|\/\/|#).*)/,
lookbehind: true lookbehind: true
}, },
'string' : { 'string' : {

Loading…
Cancel
Save