From d126ad9fa221b881db92343c42954467c452aa91 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Mon, 22 May 2017 16:57:51 -0400 Subject: [PATCH] Update Prism.js --- assets/javascripts/vendor/prism.js | 159 +++++++++++++++++------------ 1 file changed, 94 insertions(+), 65 deletions(-) diff --git a/assets/javascripts/vendor/prism.js b/assets/javascripts/vendor/prism.js index d4f82a15..023b17a5 100644 --- a/assets/javascripts/vendor/prism.js +++ b/assets/javascripts/vendor/prism.js @@ -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') ? window // if in browser : ( @@ -206,7 +206,9 @@ var _ = _self.Prism = { if (!env.code || !env.grammar) { if (env.code) { + _.hooks.run('before-highlight', env); env.element.textContent = env.code; + _.hooks.run('after-highlight', env); } _.hooks.run('complete', env); return; @@ -254,26 +256,18 @@ var _ = _self.Prism = { 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 strarr = [text]; - - var rest = grammar.rest; - - if (rest) { - for (var token in rest) { - grammar[token] = rest[token]; - } - - delete grammar.rest; - } - - tokenloop: for (var token in grammar) { + for (var token in grammar) { if(!grammar.hasOwnProperty(token) || !grammar[token]) { continue; } + if (token == target) { + return; + } + var patterns = grammar[token]; patterns = (_.util.type(patterns) === "Array") ? patterns : [patterns]; @@ -294,13 +288,13 @@ var _ = _self.Prism = { pattern = pattern.pattern || pattern; // Don’t cache length as it changes during the loop - for (var i=0, pos = 0; i text.length) { // Something went terribly wrong, ABORT, ABORT! - break tokenloop; + return; } if (str instanceof Token) { @@ -325,7 +319,7 @@ var _ = _self.Prism = { k = i, 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; // Move the index i to the element in strarr that is closest to from if (from >= p) { @@ -349,6 +343,10 @@ var _ = _self.Prism = { } if (!match) { + if (oneshot) { + break; + } + continue; } @@ -365,6 +363,8 @@ var _ = _self.Prism = { var args = [i, delNum]; if (before) { + ++i; + pos += before.length; args.push(before); } @@ -377,9 +377,31 @@ var _ = _self.Prism = { } 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; }, @@ -513,12 +535,12 @@ if (typeof global !== 'undefined') { } ; Prism.languages.markup = { - 'comment': //, - 'prolog': /<\?[\w\W]+?\?>/, - 'doctype': //i, - 'cdata': //i, + 'comment': //, + 'prolog': /<\?[\s\S]+?\?>/, + 'doctype': //i, + 'cdata': //i, '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: { 'tag': { pattern: /^<\/?[^\s>\/]+/i, @@ -528,7 +550,7 @@ Prism.languages.markup = { } }, 'attr-value': { - pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i, + pattern: /=(?:('|")[\s\S]*?(\1)|[^\s>]+)/i, inside: { 'punctuation': /[=>"']/ } @@ -560,7 +582,7 @@ Prism.languages.mathml = Prism.languages.markup; Prism.languages.svg = Prism.languages.markup; Prism.languages.css = { - 'comment': /\/\*[\w\W]*?\*\//, + 'comment': /\/\*[\s\S]*?\*\//, 'atrule': { pattern: /@[\w-]+?.*?(;|(?=\s*\{))/i, inside: { @@ -568,10 +590,10 @@ Prism.languages.css = { // 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*\{)/, 'string': { - pattern: /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/, + pattern: /("|')(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, greedy: true }, '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) { Prism.languages.insertBefore('markup', 'tag', { 'style': { - pattern: /()[\w\W]*?(?=<\/style>)/i, + pattern: /()[\s\S]*?(?=<\/style>)/i, lookbehind: true, inside: Prism.languages.css, alias: 'language-css' @@ -613,7 +635,7 @@ if (Prism.languages.markup) { Prism.languages.clike = { 'comment': [ { - pattern: /(^|[^\\])\/\*[\w\W]*?\*\//, + pattern: /(^|[^\\])\/\*[\s\S]*?\*\//, 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/, // 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, - 'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*\*?|\/|~|\^|%|\.{3}/ + 'operator': /-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/ }); Prism.languages.insertBefore('javascript', 'keyword', { @@ -679,7 +701,7 @@ Prism.languages.insertBefore('javascript', 'string', { if (Prism.languages.markup) { Prism.languages.insertBefore('markup', 'tag', { 'script': { - pattern: /()[\w\W]*?(?=<\/script>)/i, + pattern: /()[\s\S]*?(?=<\/script>)/i, lookbehind: true, inside: Prism.languages.javascript, alias: 'language-javascript' @@ -833,7 +855,10 @@ delete Prism.languages.coffeescript['template-string']; */ (function(Prism) { 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/ }); @@ -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, 'constant': /\b[A-Z0-9_]{2,}\b/, 'comment': { - pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, - lookbehind: true, - greedy: true + pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/, + lookbehind: true } }); @@ -1402,7 +1426,10 @@ Prism.languages.insertBefore('php', 'class-name', { }); Prism.languages.insertBefore('php', 'keyword', { - 'delimiter': /\?>|<\?(?:php)?/i, + 'delimiter': { + pattern: /\?>|<\?(?:php|=)?/i, + alias: 'important' + }, 'variable': /\$\w+\b/i, 'package': { 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) { // Tokenize all inline PHP blocks that are wrapped in // This allows for easy PHP + markup highlighting Prism.hooks.add('before-highlight', function(env) { - if (env.language !== 'php') { + if (env.language !== 'php' || !/(?:<\?php|<\?)/ig.test(env.code)) { return; } env.tokenStack = []; env.backupCode = env.code; - env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) { - env.tokenStack.push(match); + env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig, function(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) Prism.hooks.add('before-insert', function(env) { - if (env.language === 'php') { + if (env.language === 'php' && env.backupCode) { env.code = env.backupCode; delete env.backupCode; } @@ -1451,32 +1487,25 @@ if (Prism.languages.markup) { // Re-insert the tokens after highlighting Prism.hooks.add('after-highlight', function(env) { - if (env.language !== 'php') { + if (env.language !== 'php' || !env.tokenStack) { return; } - for (var i = 0, t; t = env.tokenStack[i]; i++) { - // The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns - env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$')); - } + // Switch the grammar back + env.grammar = Prism.languages.php; - 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 - Prism.hooks.add('wrap', function(env) { - if (env.language === 'php' && env.type === 'markup') { - env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\})/g, "$1"); + // The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns + env.highlightedCode = env.highlightedCode.replace('___PHP' + k + '___', + "" + + Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$') + + ""); } - }); - // Add the rules before all others - Prism.languages.insertBefore('php', 'comment', { - 'markup': { - pattern: /<[^?]\/?(.*?)>/, - inside: Prism.languages.markup - }, - 'php': /\{\{\{PHP[0-9]+\}\}\}/ + env.element.innerHTML = env.highlightedCode; }); } ; @@ -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); @@ -1572,7 +1601,7 @@ Prism.languages.python= { Prism.languages.rust = { 'comment': [ { - pattern: /(^|[^\\])\/\*[\w\W]*?\*\//, + pattern: /(^|[^\\])\/\*[\s\S]*?\*\//, lookbehind: true }, { @@ -1624,7 +1653,7 @@ Prism.languages.rust = { }; Prism.languages.scss = Prism.languages.extend('css', { 'comment': { - pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, + pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/, lookbehind: true }, 'atrule': { @@ -1699,7 +1728,7 @@ Prism.languages.insertBefore('scss', 'function', { Prism.languages.scss['atrule'].inside.rest = Prism.util.clone(Prism.languages.scss); Prism.languages.sql= { 'comment': { - pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|(?:--|\/\/|#).*)/, + pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|(?:--|\/\/|#).*)/, lookbehind: true }, 'string' : {