From 4ad1898902b04091e17cb04c6a6708de2fe34136 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 24 Jul 2016 11:33:23 -0400 Subject: [PATCH] Update Prism.js --- assets/javascripts/vendor/prism.js | 140 ++++++++++++++++++++--------- 1 file changed, 98 insertions(+), 42 deletions(-) diff --git a/assets/javascripts/vendor/prism.js b/assets/javascripts/vendor/prism.js index 90b8d7a6..85a80948 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+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') ? window // if in browser : ( @@ -281,9 +281,16 @@ var _ = _self.Prism = { 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"); + } + pattern = pattern.pattern || pattern; - for (var i=0; i= 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 (from >= str.length) { + /* + * If strarr[i] is a Token, then the match starts inside another Token, which is invalid + * If strarr[k - 1] is greedy we are in conflict with another greedy pattern + */ + if (strarr[i] instanceof Token || strarr[k - 1].greedy) { continue; } - var to = match.index + match[0].length, - len = str.length + nextToken.length; // Number of tokens to delete and replace with the new match - delNum = 3; - - if (to <= len) { - if (strarr[i + 1].greedy) { - continue; - } - delNum = 2; - combStr = combStr.slice(0, len); - } - str = combStr; + delNum = k - i; + str = text.slice(pos, p); + match.index -= pos; } if (!match) { @@ -447,7 +452,7 @@ Token.stringify = function(o, language, parent) { attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"'; } - return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributes + '>' + env.content + ''; + return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + (attributes ? ' ' + attributes : '') + '>' + env.content + ''; }; @@ -480,7 +485,11 @@ Token.stringify = function(o, language, parent) { // if (document.addEventListener && !script.hasAttribute('data-manual')) { // if(document.readyState !== "loading") { -// requestAnimationFrame(_.highlightAll, 0); +// if (window.requestAnimationFrame) { +// window.requestAnimationFrame(_.highlightAll); +// } else { +// window.setTimeout(_.highlightAll, 16); +// } // } // else { // document.addEventListener('DOMContentLoaded', _.highlightAll); @@ -507,7 +516,7 @@ Prism.languages.markup = { 'doctype': //, 'cdata': //i, '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: { 'tag': { 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/, '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 + 'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i, + 'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*\*?|\/|~|\^|%|\.{3}/ }); Prism.languages.insertBefore('javascript', 'keyword', { @@ -1285,7 +1295,8 @@ Prism.languages.php = Prism.languages.extend('clike', { 'constant': /\b[A-Z0-9_]{2,}\b/, 'comment': { pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/, - lookbehind: true + lookbehind: true, + greedy: true } }); @@ -1331,7 +1342,6 @@ 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); @@ -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 Prism.hooks.add('after-highlight', function(env) { if (env.language !== 'php') { @@ -1406,6 +1408,60 @@ Prism.languages.python= { '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 Add support for Markdown notation inside doc comments Add support for nested block comments...