From a5ffc1fb3bd3eba07e991ff9189241098e6e3d93 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 28 Feb 2016 10:55:22 -0500 Subject: [PATCH] Add lua syntax highlighting --- assets/javascripts/vendor/prism.js | 120 +++++++++++++++++----- assets/javascripts/views/pages/lua.coffee | 6 ++ 2 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 assets/javascripts/views/pages/lua.coffee diff --git a/assets/javascripts/vendor/prism.js b/assets/javascripts/vendor/prism.js index 914beafb..535892b2 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+php+python+rust */ +/* http://prismjs.com/download.html?themes=prism&languages=markup+css+clike+javascript+c+cpp+coffeescript+ruby+elixir+go+lua+php+python+rust */ var _self = (typeof window !== 'undefined') ? window // if in browser : ( @@ -155,10 +155,17 @@ var _ = _self.Prism = { plugins: {}, highlightAll: function(async, callback) { - var elements = document.querySelectorAll('code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'); + var env = { + callback: callback, + selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code' + }; + + _.hooks.run("before-highlightall", env); + + var elements = env.elements || document.querySelectorAll(env.selector); for (var i=0, element; element = elements[i++];) { - _.highlightElement(element, async === true, callback); + _.highlightElement(element, async === true, env.callback); } }, @@ -268,6 +275,7 @@ var _ = _self.Prism = { var pattern = patterns[j], inside = pattern.inside, lookbehind = !!pattern.lookbehind, + greedy = !!pattern.greedy, lookbehindLength = 0, alias = pattern.alias; @@ -288,36 +296,73 @@ var _ = _self.Prism = { pattern.lastIndex = 0; - var match = pattern.exec(str); + var match = pattern.exec(str), + delNum = 1; + + // Greedy patterns can override/remove up to two previously matched tokens + if (!match && greedy && i != strarr.length - 1) { + // Reconstruct the original text using the next two tokens + var nextToken = strarr[i + 1].matchedStr || strarr[i + 1], + combStr = str + nextToken; + + if (i < strarr.length - 2) { + combStr += strarr[i + 2].matchedStr || strarr[i + 2]; + } - if (match) { - if(lookbehind) { - lookbehindLength = match[1].length; + // Try the pattern again on the reconstructed text + pattern.lastIndex = 0; + match = pattern.exec(combStr); + if (!match) { + continue; } - var from = match.index - 1 + lookbehindLength, - match = match[0].slice(lookbehindLength), - len = match.length, - to = from + len, - before = str.slice(0, from + 1), - after = str.slice(to + 1); + 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) { + continue; + } + var to = match.index + match[0].length, + len = str.length + nextToken.length; - var args = [i, 1]; + // Number of tokens to delete and replace with the new match + delNum = 3; - if (before) { - args.push(before); + if (to <= len) { + delNum = 2; + combStr = combStr.slice(0, len); } + str = combStr; + } - var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias); + if (!match) { + continue; + } - args.push(wrapped); + if(lookbehind) { + lookbehindLength = match[1].length; + } - if (after) { - args.push(after); - } + var from = match.index + lookbehindLength, + match = match[0].slice(lookbehindLength), + to = from + match.length, + before = str.slice(0, from), + after = str.slice(to); + + var args = [i, delNum]; - Array.prototype.splice.apply(strarr, args); + if (before) { + args.push(before); } + + var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match); + + args.push(wrapped); + + if (after) { + args.push(after); + } + + Array.prototype.splice.apply(strarr, args); } } } @@ -350,10 +395,12 @@ var _ = _self.Prism = { } }; -var Token = _.Token = function(type, content, alias) { +var Token = _.Token = function(type, content, alias, matchedStr) { this.type = type; this.content = content; this.alias = alias; + // Copy of the full string this token was created from + this.matchedStr = matchedStr || null; }; Token.stringify = function(o, language, parent) { @@ -549,7 +596,10 @@ Prism.languages.clike = { lookbehind: true } ], - 'string': /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, + 'string': { + pattern: /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, + greedy: true + }, 'class-name': { pattern: /((?:\b(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i, lookbehind: true, @@ -575,13 +625,14 @@ Prism.languages.javascript = Prism.languages.extend('clike', { Prism.languages.insertBefore('javascript', 'keyword', { 'regex': { pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/, - lookbehind: true + lookbehind: true, + greedy: true } }); Prism.languages.insertBefore('javascript', 'class-name', { 'template-string': { - pattern: /`(?:\\`|\\?[^`])*`/, + pattern: /`(?:\\\\|\\?[^\\])*?`/, inside: { 'interpolation': { pattern: /\$\{[^}]+\}/, @@ -950,6 +1001,23 @@ Prism.languages.go = Prism.languages.extend('clike', { }); delete Prism.languages.go['class-name']; +Prism.languages.lua = { + 'comment': /^#!.+|--(?:\[(=*)\[[\s\S]*?\]\1\]|.*)/m, + // \z may be used to skip the following space + 'string': /(["'])(?:(?!\1)[^\\\r\n]|\\z(?:\r\n|\s)|\\(?:\r\n|[\s\S]))*\1|\[(=*)\[[\s\S]*?\]\2\]/, + 'number': /\b0x[a-f\d]+\.?[a-f\d]*(?:p[+-]?\d+)?\b|\b\d+(?:\.\B|\.?\d*(?:e[+-]?\d+)?\b)|\B\.\d+(?:e[+-]?\d+)?\b/i, + 'keyword': /\b(?:and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b/, + 'function': /(?!\d)\w+(?=\s*(?:[({]))/, + 'operator': [ + /[-+*%^&|#]|\/\/?|<[<=]?|>[>=]?|[=~]=?/, + { + // Match ".." but don't break "..." + pattern: /(^|[^.])\.\.(?!\.)/, + lookbehind: true + } + ], + 'punctuation': /[\[\](){},;]|\.+|:+/ +}; /** * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highlighting-prism/ * Modified by Miles Johnson: http://milesj.me diff --git a/assets/javascripts/views/pages/lua.coffee b/assets/javascripts/views/pages/lua.coffee new file mode 100644 index 00000000..b42bfcc6 --- /dev/null +++ b/assets/javascripts/views/pages/lua.coffee @@ -0,0 +1,6 @@ +#= require views/pages/base + +class app.views.LuaPage extends app.views.BasePage + prepare: -> + @highlightCode @findAllByTag('pre'), 'lua' + return