From 1839ff5bbc44c2e1c21547e1c1dc4f0b7bfb8fd6 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Wed, 27 Feb 2019 22:24:01 +0100 Subject: [PATCH] Made a Core a little more editor friendly --- components/prism-core.js | 24 +++++++++++++----------- components/prism-core.min.js | 2 +- prism.js | 24 +++++++++++++----------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/components/prism-core.js b/components/prism-core.js index 453dbe24f0..65d0f8d53c 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -12,13 +12,13 @@ var _self = (typeof window !== 'undefined') * @author Lea Verou http://lea.verou.me */ -var Prism = (function(){ +var Prism = (function (_self){ // Private helper vars var lang = /\blang(?:uage)?-([\w-]+)\b/i; var uniqueId = 0; -var _ = _self.Prism = { +var _ = { manual: _self.Prism && _self.Prism.manual, disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler, util: { @@ -269,8 +269,6 @@ var _ = _self.Prism = { }, matchGrammar: function (text, strarr, grammar, index, startPos, oneshot, target) { - var Token = _.Token; - for (var token in grammar) { if(!grammar.hasOwnProperty(token) || !grammar[token]) { continue; @@ -436,17 +434,21 @@ var _ = _self.Prism = { callback(env); } } - } + }, + + Token: Token }; -var Token = _.Token = function(type, content, alias, matchedStr, greedy) { +_self.Prism = _; + +function Token(type, content, alias, matchedStr, greedy) { this.type = type; this.content = content; this.alias = alias; // Copy of the full string this token was created from this.length = (matchedStr || "").length|0; this.greedy = !!greedy; -}; +} Token.stringify = function(o, language, parent) { if (typeof o == 'string') { @@ -487,7 +489,7 @@ Token.stringify = function(o, language, parent) { if (!_self.document) { if (!_self.addEventListener) { // in Node.js - return _self.Prism; + return _; } if (!_.disableWorkerMessageHandler) { @@ -505,7 +507,7 @@ if (!_self.document) { }, false); } - return _self.Prism; + return _; } //Get current script and highlight @@ -528,9 +530,9 @@ if (script) { } } -return _self.Prism; +return _; -})(); +})(_self); if (typeof module !== 'undefined' && module.exports) { module.exports = Prism; diff --git a/components/prism-core.min.js b/components/prism-core.min.js index bde09e05e4..1955429049 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-([\w-]+)\b/i,t=0,n=_self.Prism={manual:_self.Prism&&_self.Prism.manual,disableWorkerMessageHandler:_self.Prism&&_self.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof a?new a(e.type,n.util.encode(e.content),e.alias):"Array"===n.util.type(e)?e.map(n.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof s)){if(m&&b!=t.length-1){f.lastIndex=k;var _=f.exec(e);if(!_)break;for(var j=_.index+(d?_[1].length:0),P=_.index+_[0].length,A=b,O=k,x=t.length;x>A&&(P>O||!t[A].type&&!t[A-1].greedy);++A)O+=t[A].length,j>=O&&(++b,k=O);if(t[b]instanceof s)continue;I=A-b,w=e.slice(k,O),_.index-=k}else{f.lastIndex=0;var _=f.exec(w),I=1}if(_){d&&(p=_[1]?_[1].length:0);var j=_.index+p,_=_[0].slice(p),P=j+_.length,N=w.slice(0,j),S=w.slice(P),E=[b,I];N&&(++b,k+=N.length,E.push(N));var C=new s(u,h?n.tokenize(_,h):_,y,_,m);if(E.push(C),S&&E.push(S),Array.prototype.splice.apply(t,E),1!=I&&n.matchGrammar(e,t,a,b,k,!0,u),i)break}else if(i)break}}}}},tokenize:function(e,t){var a=[e],r=t.rest;if(r){for(var l in r)t[l]=r[l];delete t.rest}return n.matchGrammar(e,a,t,0,0,!1),a},hooks:{all:{},add:function(e,t){var a=n.hooks.all;a[e]=a[e]||[],a[e].push(t)},run:function(e,t){var a=n.hooks.all[e];if(a&&a.length)for(var r,l=0;r=a[l++];)r(t)}}},a=n.Token=function(e,t,n,a,r){this.type=e,this.content=t,this.alias=n,this.length=0|(a||"").length,this.greedy=!!r};if(a.stringify=function(e,t,r){if("string"==typeof e)return e;if("Array"===n.util.type(e))return e.map(function(n){return a.stringify(n,t,e)}).join("");var l={type:e.type,content:a.stringify(e.content,t,r),tag:"span",classes:["token",e.type],attributes:{},language:t,parent:r};if(e.alias){var i="Array"===n.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(l.classes,i)}n.hooks.run("wrap",l);var o=Object.keys(l.attributes).map(function(e){return e+'="'+(l.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+l.tag+' class="'+l.classes.join(" ")+'"'+(o?" "+o:"")+">"+l.content+""},!_self.document)return _self.addEventListener?(n.disableWorkerMessageHandler||_self.addEventListener("message",function(e){var t=JSON.parse(e.data),a=t.language,r=t.code,l=t.immediateClose;_self.postMessage(n.highlight(r,n.languages[a],a)),l&&_self.close()},!1),_self.Prism):_self.Prism;var r=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return r&&(n.filename=r.src,n.manual||r.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(n.highlightAll):window.setTimeout(n.highlightAll,16):document.addEventListener("DOMContentLoaded",n.highlightAll))),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(e){function t(e,t,n,a,r){this.type=e,this.content=t,this.alias=n,this.length=0|(a||"").length,this.greedy=!!r}var n=/\blang(?:uage)?-([\w-]+)\b/i,a=0,r={manual:e.Prism&&e.Prism.manual,disableWorkerMessageHandler:e.Prism&&e.Prism.disableWorkerMessageHandler,util:{encode:function(e){return e instanceof t?new t(e.type,r.util.encode(e.content),e.alias):"Array"===r.util.type(e)?e.map(r.util.encode):e.replace(/&/g,"&").replace(/e.length)return;if(!(w instanceof t)){if(p&&b!=n.length-1){h.lastIndex=k;var j=h.exec(e);if(!j)break;for(var A=j.index+(f?j[1].length:0),P=j.index+j[0].length,O=b,x=k,I=n.length;I>O&&(P>x||!n[O].type&&!n[O-1].greedy);++O)x+=n[O].length,A>=x&&(++b,k=x);if(n[b]instanceof t)continue;N=O-b,w=e.slice(k,x),j.index-=k}else{h.lastIndex=0;var j=h.exec(w),N=1}if(j){f&&(m=j[1]?j[1].length:0);var A=j.index+m,j=j[0].slice(m),P=A+j.length,S=w.slice(0,A),E=w.slice(P),_=[b,N];S&&(++b,k+=S.length,_.push(S));var C=new t(u,d?r.tokenize(j,d):j,y,j,p);if(_.push(C),E&&_.push(E),Array.prototype.splice.apply(n,_),1!=N&&r.matchGrammar(e,n,a,b,k,!0,u),o)break}else if(o)break}}}}},tokenize:function(e,t){var n=[e],a=t.rest;if(a){for(var i in a)t[i]=a[i];delete t.rest}return r.matchGrammar(e,n,t,0,0,!1),n},hooks:{all:{},add:function(e,t){var n=r.hooks.all;n[e]=n[e]||[],n[e].push(t)},run:function(e,t){var n=r.hooks.all[e];if(n&&n.length)for(var a,i=0;a=n[i++];)a(t)}},Token:t};if(e.Prism=r,t.stringify=function(e,n,a){if("string"==typeof e)return e;if("Array"===r.util.type(e))return e.map(function(a){return t.stringify(a,n,e)}).join("");var i={type:e.type,content:t.stringify(e.content,n,a),tag:"span",classes:["token",e.type],attributes:{},language:n,parent:a};if(e.alias){var l="Array"===r.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(i.classes,l)}r.hooks.run("wrap",i);var o=Object.keys(i.attributes).map(function(e){return e+'="'+(i.attributes[e]||"").replace(/"/g,""")+'"'}).join(" ");return"<"+i.tag+' class="'+i.classes.join(" ")+'"'+(o?" "+o:"")+">"+i.content+""},!e.document)return e.addEventListener?(r.disableWorkerMessageHandler||e.addEventListener("message",function(t){var n=JSON.parse(t.data),a=n.language,i=n.code,l=n.immediateClose;e.postMessage(r.highlight(i,r.languages[a],a)),l&&e.close()},!1),r):r;var i=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return i&&(r.filename=i.src,r.manual||i.hasAttribute("data-manual")||("loading"!==document.readyState?window.requestAnimationFrame?window.requestAnimationFrame(r.highlightAll):window.setTimeout(r.highlightAll,16):document.addEventListener("DOMContentLoaded",r.highlightAll))),r}(_self);"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file diff --git a/prism.js b/prism.js index 2ac524897c..0a040fa9fc 100644 --- a/prism.js +++ b/prism.js @@ -17,13 +17,13 @@ var _self = (typeof window !== 'undefined') * @author Lea Verou http://lea.verou.me */ -var Prism = (function(){ +var Prism = (function (_self){ // Private helper vars var lang = /\blang(?:uage)?-([\w-]+)\b/i; var uniqueId = 0; -var _ = _self.Prism = { +var _ = { manual: _self.Prism && _self.Prism.manual, disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler, util: { @@ -274,8 +274,6 @@ var _ = _self.Prism = { }, matchGrammar: function (text, strarr, grammar, index, startPos, oneshot, target) { - var Token = _.Token; - for (var token in grammar) { if(!grammar.hasOwnProperty(token) || !grammar[token]) { continue; @@ -441,17 +439,21 @@ var _ = _self.Prism = { callback(env); } } - } + }, + + Token: Token }; -var Token = _.Token = function(type, content, alias, matchedStr, greedy) { +_self.Prism = _; + +function Token(type, content, alias, matchedStr, greedy) { this.type = type; this.content = content; this.alias = alias; // Copy of the full string this token was created from this.length = (matchedStr || "").length|0; this.greedy = !!greedy; -}; +} Token.stringify = function(o, language, parent) { if (typeof o == 'string') { @@ -492,7 +494,7 @@ Token.stringify = function(o, language, parent) { if (!_self.document) { if (!_self.addEventListener) { // in Node.js - return _self.Prism; + return _; } if (!_.disableWorkerMessageHandler) { @@ -510,7 +512,7 @@ if (!_self.document) { }, false); } - return _self.Prism; + return _; } //Get current script and highlight @@ -533,9 +535,9 @@ if (script) { } } -return _self.Prism; +return _; -})(); +})(_self); if (typeof module !== 'undefined' && module.exports) { module.exports = Prism;