From 01f9d51de3bd936b2ddc7c901b4dfc9141bed287 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sat, 4 Mar 2017 11:34:39 -0500 Subject: [PATCH] Update Prism.js --- assets/javascripts/vendor/prism.js | 189 +++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 48 deletions(-) diff --git a/assets/javascripts/vendor/prism.js b/assets/javascripts/vendor/prism.js index 109beac4..81045e15 100644 --- a/assets/javascripts/vendor/prism.js +++ b/assets/javascripts/vendor/prism.js @@ -20,6 +20,7 @@ var lang = /\blang(?:uage)?-(\w+)\b/i; var uniqueId = 0; var _ = _self.Prism = { + manual: _self.Prism && _self.Prism.manual, util: { encode: function (tokens) { if (tokens instanceof Token) { @@ -204,6 +205,9 @@ var _ = _self.Prism = { _.hooks.run('before-sanity-check', env); if (!env.code || !env.grammar) { + if (env.code) { + env.element.textContent = env.code; + } _.hooks.run('complete', env); return; } @@ -290,7 +294,7 @@ var _ = _self.Prism = { pattern = pattern.pattern || pattern; // Don’t cache length as it changes during the loop - for (var i=0, pos = 0; i= p) { ++i; @@ -410,7 +414,7 @@ var Token = _.Token = function(type, content, alias, matchedStr, greedy) { this.content = content; this.alias = alias; // Copy of the full string this token was created from - this.matchedStr = matchedStr || null; + this.length = (matchedStr || "").length|0; this.greedy = !!greedy; }; @@ -446,11 +450,9 @@ Token.stringify = function(o, language, parent) { _.hooks.run('wrap', env); - var attributes = ''; - - for (var name in env.attributes) { - attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"'; - } + var attributes = Object.keys(env.attributes).map(function(name) { + return name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"'; + }).join(' '); return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + (attributes ? ' ' + attributes : '') + '>' + env.content + ''; @@ -483,7 +485,7 @@ Token.stringify = function(o, language, parent) { // if (script) { // _.filename = script.src; -// if (document.addEventListener && !script.hasAttribute('data-manual')) { +// if (document.addEventListener && !_.manual && !script.hasAttribute('data-manual')) { // if(document.readyState !== "loading") { // if (window.requestAnimationFrame) { // window.requestAnimationFrame(_.highlightAll); @@ -513,7 +515,7 @@ if (typeof global !== 'undefined') { Prism.languages.markup = { 'comment': //, 'prolog': /<\?[\w\W]+?\?>/, - 'doctype': //, + 'doctype': //i, 'cdata': //i, 'tag': { pattern: /<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i, @@ -568,7 +570,10 @@ Prism.languages.css = { }, 'url': /url\((?:(["'])(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1|.*?)\)/i, 'selector': /[^\{\}\s][^\{\};]*?(?=\s*\{)/, - 'string': /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/, + 'string': { + pattern: /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/, + greedy: true + }, 'property': /(\b|\B)[\w-]+(?=\s*:)/i, 'important': /\B!important\b/i, 'function': /[-a-z0-9]+(?=\()/i, @@ -745,7 +750,7 @@ Prism.languages.coffeescript = Prism.languages.extend('javascript', { // Strings are multiline { pattern: /'(?:\\?[^\\])*?'/, - greedy: true, + greedy: true }, { @@ -847,12 +852,14 @@ delete Prism.languages.coffeescript['template-string']; 'regex': [ { pattern: /%r([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1[gim]{0,3}/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /%r\((?:[^()\\]|\\[\s\S])*\)[gim]{0,3}/, + greedy: true, inside: { 'interpolation': interpolation } @@ -860,25 +867,29 @@ delete Prism.languages.coffeescript['template-string']; { // Here we need to specifically allow interpolation pattern: /%r\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}[gim]{0,3}/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /%r\[(?:[^\[\]\\]|\\[\s\S])*\][gim]{0,3}/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /%r<(?:[^<>\\]|\\[\s\S])*>[gim]{0,3}/, + greedy: true, inside: { 'interpolation': interpolation } }, { - pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/, - lookbehind: true + pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/, + lookbehind: true, + greedy: true } ], 'variable': /[@$]+[a-zA-Z_][a-zA-Z_0-9]*(?:[?!]|\b)/, @@ -886,19 +897,21 @@ delete Prism.languages.coffeescript['template-string']; }); Prism.languages.insertBefore('ruby', 'number', { - 'builtin': /\b(Array|Bignum|Binding|Class|Continuation|Dir|Exception|FalseClass|File|Stat|File|Fixnum|Fload|Hash|Integer|IO|MatchData|Method|Module|NilClass|Numeric|Object|Proc|Range|Regexp|String|Struct|TMS|Symbol|ThreadGroup|Thread|Time|TrueClass)\b/, + 'builtin': /\b(Array|Bignum|Binding|Class|Continuation|Dir|Exception|FalseClass|File|Stat|File|Fixnum|Float|Hash|Integer|IO|MatchData|Method|Module|NilClass|Numeric|Object|Proc|Range|Regexp|String|Struct|TMS|Symbol|ThreadGroup|Thread|Time|TrueClass)\b/, 'constant': /\b[A-Z][a-zA-Z_0-9]*(?:[?!]|\b)/ }); Prism.languages.ruby.string = [ { pattern: /%[qQiIwWxs]?([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /%[qQiIwWxs]?\((?:[^()\\]|\\[\s\S])*\)/, + greedy: true, inside: { 'interpolation': interpolation } @@ -906,24 +919,28 @@ delete Prism.languages.coffeescript['template-string']; { // Here we need to specifically allow interpolation pattern: /%[qQiIwWxs]?\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /%[qQiIwWxs]?\[(?:[^\[\]\\]|\\[\s\S])*\]/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /%[qQiIwWxs]?<(?:[^<>\\]|\\[\s\S])*>/, + greedy: true, inside: { 'interpolation': interpolation } }, { pattern: /("|')(#\{[^}]+\}|\\(?:\r?\n|\r)|\\?.)*?\1/, + greedy: true, inside: { 'interpolation': interpolation } @@ -944,12 +961,14 @@ Prism.languages.elixir = { { // ~s"""foo""", ~s'''foo''', ~s/foo/, ~s|foo|, ~s"foo", ~s'foo', ~s(foo), ~s[foo], ~s{foo}, ~s pattern: /~[cCsSwW](?:("""|'''|[\/|"'])(?:\\.|(?!\1)[^\\])+\1|\((?:\\\)|[^)])+\)|\[(?:\\\]|[^\]])+\]|\{(?:\\\}|#\{[^}]+\}|[^}])+\}|<(?:\\>|[^>])+>)[csa]?/, + greedy: true, inside: { // See interpolation below } }, { pattern: /("""|''')[\s\S]*?\1/, + greedy: true, inside: { // See interpolation below } @@ -957,6 +976,7 @@ Prism.languages.elixir = { { // Multi-line strings are allowed pattern: /("|')(?:\\[\s\S]|(?!\1)[^\\])*\1/, + greedy: true, inside: { // See interpolation below } @@ -1027,18 +1047,21 @@ Prism.languages.go = Prism.languages.extend('clike', { 'boolean': /\b(_|iota|nil|true|false)\b/, 'operator': /[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\./, 'number': /\b(-?(0x[a-f\d]+|(\d+\.?\d*|\.\d+)(e[-+]?\d+)?)i?)\b/i, - 'string': /("|'|`)(\\?.|\r|\n)*?\1/ + 'string': { + pattern: /("|'|`)(\\?.|\r|\n)*?\1/, + greedy: true + } }); delete Prism.languages.go['class-name']; Prism.languages.json = { - 'property': /".*?"(?=\s*:)/ig, - 'string': /"(?!:)(\\?[^"])*?"(?!:)/g, - 'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/g, - 'punctuation': /[{}[\]);,]/g, - 'operator': /:/g, - 'boolean': /\b(true|false)\b/gi, - 'null': /\bnull\b/gi, + 'property': /"(?:\\.|[^\\"])*"(?=\s*:)/ig, + 'string': /"(?!:)(?:\\.|[^\\"])*"(?!:)/g, + 'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee][+-]?\d+)?)\b/g, + 'punctuation': /[{}[\]);,]/g, + 'operator': /:/g, + 'boolean': /\b(true|false)\b/gi, + 'null': /\bnull\b/gi }; Prism.languages.jsonp = Prism.languages.json; @@ -1151,92 +1174,143 @@ Prism.languages.perl = { // TODO Could be nice to handle Heredoc too. 'string': [ // q/.../ - /\b(?:q|qq|qx|qw)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1/, + { + pattern: /\b(?:q|qq|qx|qw)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1/, + greedy: true + }, // q a...a - /\b(?:q|qq|qx|qw)\s+([a-zA-Z0-9])(?:[^\\]|\\[\s\S])*?\1/, + { + pattern: /\b(?:q|qq|qx|qw)\s+([a-zA-Z0-9])(?:[^\\]|\\[\s\S])*?\1/, + greedy: true + }, // q(...) - /\b(?:q|qq|qx|qw)\s*\((?:[^()\\]|\\[\s\S])*\)/, + { + pattern: /\b(?:q|qq|qx|qw)\s*\((?:[^()\\]|\\[\s\S])*\)/, + greedy: true + }, // q{...} - /\b(?:q|qq|qx|qw)\s*\{(?:[^{}\\]|\\[\s\S])*\}/, + { + pattern: /\b(?:q|qq|qx|qw)\s*\{(?:[^{}\\]|\\[\s\S])*\}/, + greedy: true + }, // q[...] - /\b(?:q|qq|qx|qw)\s*\[(?:[^[\]\\]|\\[\s\S])*\]/, + { + pattern: /\b(?:q|qq|qx|qw)\s*\[(?:[^[\]\\]|\\[\s\S])*\]/, + greedy: true + }, // q<...> - /\b(?:q|qq|qx|qw)\s*<(?:[^<>\\]|\\[\s\S])*>/, + { + pattern: /\b(?:q|qq|qx|qw)\s*<(?:[^<>\\]|\\[\s\S])*>/, + greedy: true + }, // "...", `...` - /("|`)(?:[^\\]|\\[\s\S])*?\1/, + { + pattern: /("|`)(?:[^\\]|\\[\s\S])*?\1/, + greedy: true + }, // '...' // FIXME Multi-line single-quoted strings are not supported as they would break variables containing ' - /'(?:[^'\\\r\n]|\\.)*'/ + { + pattern: /'(?:[^'\\\r\n]|\\.)*'/, + greedy: true + } ], 'regex': [ // m/.../ - /\b(?:m|qr)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1[msixpodualngc]*/, + { + pattern: /\b(?:m|qr)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1[msixpodualngc]*/, + greedy: true + }, // m a...a - /\b(?:m|qr)\s+([a-zA-Z0-9])(?:[^\\]|\\.)*?\1[msixpodualngc]*/, + { + pattern: /\b(?:m|qr)\s+([a-zA-Z0-9])(?:[^\\]|\\.)*?\1[msixpodualngc]*/, + greedy: true + }, // m(...) - /\b(?:m|qr)\s*\((?:[^()\\]|\\[\s\S])*\)[msixpodualngc]*/, + { + pattern: /\b(?:m|qr)\s*\((?:[^()\\]|\\[\s\S])*\)[msixpodualngc]*/, + greedy: true + }, // m{...} - /\b(?:m|qr)\s*\{(?:[^{}\\]|\\[\s\S])*\}[msixpodualngc]*/, + { + pattern: /\b(?:m|qr)\s*\{(?:[^{}\\]|\\[\s\S])*\}[msixpodualngc]*/, + greedy: true + }, // m[...] - /\b(?:m|qr)\s*\[(?:[^[\]\\]|\\[\s\S])*\][msixpodualngc]*/, + { + pattern: /\b(?:m|qr)\s*\[(?:[^[\]\\]|\\[\s\S])*\][msixpodualngc]*/, + greedy: true + }, // m<...> - /\b(?:m|qr)\s*<(?:[^<>\\]|\\[\s\S])*>[msixpodualngc]*/, + { + pattern: /\b(?:m|qr)\s*<(?:[^<>\\]|\\[\s\S])*>[msixpodualngc]*/, + greedy: true + }, // The lookbehinds prevent -s from breaking // FIXME We don't handle change of separator like s(...)[...] // s/.../.../ { pattern: /(^|[^-]\b)(?:s|tr|y)\s*([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\2(?:[^\\]|\\[\s\S])*?\2[msixpodualngcer]*/, - lookbehind: true + lookbehind: true, + greedy: true }, // s a...a...a { pattern: /(^|[^-]\b)(?:s|tr|y)\s+([a-zA-Z0-9])(?:[^\\]|\\[\s\S])*?\2(?:[^\\]|\\[\s\S])*?\2[msixpodualngcer]*/, - lookbehind: true + lookbehind: true, + greedy: true }, // s(...)(...) { pattern: /(^|[^-]\b)(?:s|tr|y)\s*\((?:[^()\\]|\\[\s\S])*\)\s*\((?:[^()\\]|\\[\s\S])*\)[msixpodualngcer]*/, - lookbehind: true + lookbehind: true, + greedy: true }, // s{...}{...} { pattern: /(^|[^-]\b)(?:s|tr|y)\s*\{(?:[^{}\\]|\\[\s\S])*\}\s*\{(?:[^{}\\]|\\[\s\S])*\}[msixpodualngcer]*/, - lookbehind: true + lookbehind: true, + greedy: true }, // s[...][...] { pattern: /(^|[^-]\b)(?:s|tr|y)\s*\[(?:[^[\]\\]|\\[\s\S])*\]\s*\[(?:[^[\]\\]|\\[\s\S])*\][msixpodualngcer]*/, - lookbehind: true + lookbehind: true, + greedy: true }, // s<...><...> { pattern: /(^|[^-]\b)(?:s|tr|y)\s*<(?:[^<>\\]|\\[\s\S])*>\s*<(?:[^<>\\]|\\[\s\S])*>[msixpodualngcer]*/, - lookbehind: true + lookbehind: true, + greedy: true }, // /.../ // The look-ahead tries to prevent two divisions on // the same line from being highlighted as regex. // This does not support multi-line regex. - /\/(?:[^\/\\\r\n]|\\.)*\/[msixpodualngc]*(?=\s*(?:$|[\r\n,.;})&|\-+*~<>!?^]|(lt|gt|le|ge|eq|ne|cmp|not|and|or|xor|x)\b))/ + { + pattern: /\/(?:[^\/\\\r\n]|\\.)*\/[msixpodualngc]*(?=\s*(?:$|[\r\n,.;})&|\-+*~<>!?^]|(lt|gt|le|ge|eq|ne|cmp|not|and|or|xor|x)\b))/, + greedy: true + } ], // FIXME Not sure about the handling of ::, ', and # @@ -1342,6 +1416,7 @@ if (Prism.languages.markup) { env.tokenStack = []; + env.backupCode = env.code; env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function(match) { env.tokenStack.push(match); @@ -1349,6 +1424,14 @@ 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 Prism.hooks.add('after-highlight', function(env) { if (env.language !== 'php') { @@ -1411,7 +1494,7 @@ Prism.languages.python= { (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/, + /\b(?:abstract|alias|as|asm|begin|break|case|class|def|do|else|elsif|end|ensure|enum|extend|for|fun|if|include|instance_sizeof|lib|macro|module|next|of|out|pointerof|private|protected|rescue|return|require|select|self|sizeof|struct|super|then|type|typeof|uninitialized|union|unless|until|when|while|with|yield|__DIR__|__END_LINE__|__FILE__|__LINE__)\b/, { pattern: /(\.\s*)(?:is_a|responds_to)\?/, lookbehind: true @@ -1481,13 +1564,20 @@ Prism.languages.rust = { } ], 'string': [ - /b?r(#*)"(?:\\?.)*?"\1/, - /b?("|')(?:\\?.)*?\1/ + { + pattern: /b?r(#*)"(?:\\?.)*?"\1/, + greedy: true + }, + { + pattern: /b?("|')(?:\\?.)*?\1/, + greedy: true + } ], 'keyword': /\b(?:abstract|alignof|as|be|box|break|const|continue|crate|do|else|enum|extern|false|final|fn|for|if|impl|in|let|loop|match|mod|move|mut|offsetof|once|override|priv|pub|pure|ref|return|sizeof|static|self|struct|super|true|trait|type|typeof|unsafe|unsized|use|virtual|where|while|yield)\b/, 'attribute': { pattern: /#!?\[.+?\]/, + greedy: true, alias: 'attr-name' }, @@ -1597,6 +1687,7 @@ Prism.languages.sql= { }, 'string' : { pattern: /(^|[^@\\])("|')(?:\\?[\s\S])*?\2/, + greedy: true, lookbehind: true }, 'variable': /@[\w.$]+|@("|'|`)(?:\\?[\s\S])+?\1/, @@ -1608,6 +1699,8 @@ Prism.languages.sql= { 'punctuation': /[;[\]()`,.]/ }; Prism.languages.typescript = Prism.languages.extend('javascript', { - 'keyword': /\b(break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|function|get|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|set|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield|module|declare|constructor|string|Function|any|number|boolean|Array|enum)\b/ + // From JavaScript Prism keyword list and TypeScript language spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#221-reserved-words + '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|false|true|module|declare|constructor|string|Function|any|number|boolean|Array|enum|symbol|namespace|abstract|require|type)\b/ }); +Prism.languages.ts = Prism.languages.typescript;