-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlightJavascript.min.js
15 lines (15 loc) · 7.46 KB
/
highlightJavascript.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(function(x){var u=x.document;if("function"===typeof u.querySelector){var o=function(a){for(var b=[],d,c=0,f=0,c=0;c<a.length;c++)if(Array.isArray(a[c])){d=o(a[c]);for(f=0;f<d.length;f++)b.push(d[f])}else b.push(a[c]);return b},k={regex1:/\/(?![\*])(?:[\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\[\{\]\}\\\|\;\:\'\"\,<\.\>\/\?a-z0-9])+\/[gim]*(?=\,|\;|\]|\)|\}|\n|\r|\n\r|$)(?![a-z0-9\040])/gi,regex2:/\/(?![\*])(?:\040*[\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\[\{\]\}\\\|\;\:\'\"\,<\.\>\/\?a-z0-9])+\/[gim]*(?=\,|\;|\]|\)|\}|\n|\r|\n\r|$)(?![a-z0-9\040])/gi,
number:/[\+\-]?\b(?:(?:0x[A-Fa-f0-9]+)|(?:(?:[\d]*\.)?[\d]+(?:[eE][\+\-]?[\d]+)?))u?(?:(?:int(?:8|16|32|64))|L)?\b/g,string:/(?:'[^'\\]*(?:\\.[^'\\]*)*')|(?:"[^"\\]*(?:\\.[^"\\]*)*")/g,singlecomment:/\/\/.+?(?=\n|\r|$)/ig,multicomment:/\/\*[\s\S]+?\*\//g,whitespace:/^\s+|\s+$/g,newline:/[\r\n]/g,quote:/"/g,lessthan:/</g,greaterthan:/>/g,ampersand:/&/g,link:/<a\b href="[htpfs]+:\/\/[^"]+"[^>]*>(?:.*?)<\/a>/g,tag:/<(?:\!|\/)?[a-z][a-z0-1\-]\s*(?:[^>]+)?\>/ig,url:/(?:http|ftp|https):\/\/[\w\-_]+(?:\.[\w\-_]+)+(?:[\w\-\.,@?\^=%&:\/~\+#]*[\w\-@?\^=%&\/~\+#])?/g},
l={selector:"",findregex:!0,findcomments:!0,findstrings:!0,removewhitespace:!0,starttag:"<\!--",endtag:"--\>",language:"javascript",showlinenumbers:!0,preparsed:!1},n=0,f=[],p={},s={},t="",q=null,v,w=/^[0-9]*[a-z]+[0-9]*$/ig,r=/\r|\n|\n\r/g,c=function(a,b){var d;n=0;f=[a.innerHTML];p=b;s=c.language[b.language];t=a.innerHTML;q=a;v=void 0;for(d in s.regex)s.regex.hasOwnProperty(d)&&k[d]!==s.regex[d]&&(k[d]=s.regex[d]);c.language[q.dataset.type]&&(s=c.language[q.dataset.type]);f[0]=f[0].replace(p.starttag,
"").replace(p.endtag,"");p.removewhitespace&&(f[0]=f[0].replace(k.whitespace,""));n=0;p.preparsed&&(f[0]=f[0].replace(/&/g,"&"),f[0]=f[0].replace(/</g,"<"),f[0]=f[0].replace(/>/g,">"),f[0]=f[0].replace(/"/g,'"'));for(;c.parseQue();)c.parseQue();d=c.mergeResults();d.match(k.newline)&&(d=d.split(k.newline),d=d.join("</span></li><li><span>"));d="<ol><li><span>"+d+"</span></li></ol>";q.parsed=d;q.unparsed=t;q.parse=function(){this.innerHTML===this.parsed?console.warn("Already ran highlightJavascript on this node."):
this.innerHTML=this.parsed};q.unparse=function(){this.innerHTML===this.unparsed?console.warn("Already ran highlightJavascript on this node."):this.innerHTML=this.unparsed};q.innerHTML=d;!1===p.showlinenumbers?(q.children[0].style.marginLeft="5px",q.children[0].style.listStyleType="none"):(d=(-1!==t.indexOf("\n")?t.split("\n"):t.split("\r")).length,100<d&&(q.children[0].style.marginLeft=1E3<d?"5em":"4em"))};c.mergeResults=function(a){a||(a=f);for(var b="",d=a.length,e=0;e<d;e++)"string"===typeof a[e]?
b+=a[e]:"object"===typeof a[e]&&(b=Array.isArray(a[e].data)?b+a[e].before+c.mergeResults(a[e].data)+a[e].after:b+a[e].before+a[e].data+a[e].after);return b};c.parseQue=function(){var a=0,b=["link"];p.findregex&&b.push("regex1");p.findcomments&&b.push("multicomment");p.findcomments&&b.push("singlecomment");p.findstrings&&b.push("string");p.findregex&&b.push("regex2");b.push("url");if("string"===typeof f[n]){for(a=0;a<b.length;a++)if(f[n].match(k[b[a]])&&(f=c.process(k[b[a]],[b[a]]),c.restartScan()))return!0;
for(a=0;a<s.regexList.length;a++)if(f[n].match(s.regexList[a].regex)&&(f=c.process.custom(s.regexList[a].regex,s.regexList[a].css),c.restartScan()))return!0}n+=1;return n<f.length?!0:!1};c.restartScan=function(){return v!==f.length?(v=f.length,!0):!1};c.process=function(a,b){var d=f[n].match(a),e=[f[n]],k,h,j,i,g,m,l;for(g=0;g<d.length;g++){k=c.process[b](d[g]);for(m=0;m<e.length;m++)if("string"===typeof e[m]&&(h=d[g].match(w)&&null===d[g].match(r)?RegExp("\\b"+d[g]+"\\b","gm"):d[g],-1<e[m].indexOf(d[g])&&
e[m].split(h)[0]!==e[m])){h=e[m].split(h);j=h.length-1;i=0;for(l=h.length;0<l;l--)h.splice(l-1,0,k),i+=1,i===j&&(l=0);e[m]=h}e=o(e)}f[n]=o(e);return o(f)};c.process.custom=function(a,b){var d=f[n].match(a),e=[f[n]],l,h,j,i,g,m,k;for(g=0;g<d.length;g++){l=c.process.generic("custom",'<span class="'+b+'">',c.process.sanitize(d[g]),"</span>");for(m=0;m<e.length;m++)if("string"===typeof e[m]&&(h=d[g].match(w)&&null===d[g].match(r)?RegExp("\\b"+d[g]+"\\b","gm"):d[g],-1<e[m].indexOf(d[g])&&e[m].split(h)[0]!==
e[m])){h=e[m].split(h);j=h.length-1;i=0;for(k=h.length;0<k;k--)h.splice(k-1,0,l),i+=1,i===j&&(k=0);e[m]=h}e=o(e)}f[n]=o(e);return o(f)};c.process.sanitize=function(a){a=a.replace(k.ampersand,function(){return"&"});a=a.replace(k.quote,function(){return"""});a=a.replace(k.lessthan,function(){return"<"});return a=a.replace(k.greaterthan,function(){return">"})};c.process.generic=function(a,b,c,e){return{type:a,before:b,data:c,after:e}};c.process.link=function(a){return c.process.generic("link",
"",c.process.escaped(a),"")};c.process.url=function(a){return c.process.generic("url",'<a href="'+a+'">',c.process.escaped(a),"</a>")};c.process.regex1=function(a){return c.process.generic("regex",'<span class="regex">',c.process.escaped(a),"</span>")};c.process.regex2=function(a){return c.process.generic("regex",'<span class="regex">',c.process.escaped(a),"</span>")};c.process.singlecomment=function(a){return c.process.generic("comment",'<span class="comment">',c.process.escaped(a),"</span>")};c.process.multicomment=
function(a){a=c.process.inlineUrl(a);if("object"===typeof a)for(var b=0;b<a.length;b++)"string"===typeof a[b]&&a[b].match(r)&&(a[b]=a[b].replace(r,'</span>$1<span class="comment">'));else a.match(r)&&(a=a.replace(r,'</span>$1<span class="comment">'));return c.process.generic("comment",'<span class="comment">',a,"</span>")};c.process.string=function(a){a=c.process.inlineUrl(a);if("object"===typeof a)for(var b=0;b<a.length;b++)"string"===typeof a[b]&&a[b].match(r)&&(a[b]=a[b].replace(r,'</span>$1<span class="string">'));
else a.match(r)&&(a=a.replace(r,'</span>$1<span class="string">'));return c.process.generic("string",'<span class="string">',a,"</span>")};c.process.inlineUrl=function(a){var b=a.match(k.url);if(null===b)return c.process.sanitize(a);var a=[a],d,e,f,h,j,i,g;for(j=0;j<b.length;j++){d=c.process.generic("url",'<a href="'+b[j]+'">',b[j],"</a>");for(i=0;i<a.length;i++)if("string"===typeof a[i]&&-1<a[i].indexOf(b[j])){e=a[i].split(b[j]);f=e.length-1;h=0;for(g=e.length;0<g;g--)e.splice(g-1,0,d),h+=1,h===
f&&(g=0);a[i]=e}a=o(a)}a=o(a);for(b=0;b<a.length;b++)"string"===typeof a[b]&&(a[b]=c.process.sanitize(a[b]));return a};c.process.escaped=function(a){var b=a.match(k.escaped);if(null===b)return c.process.sanitize(a);var a=[a],d,e,f,h,j,i,g;for(j=0;j<b.length;j++){d=c.process.generic("escaped",'<span class="constant">',c.process.sanitize(b[j]),"</span>");for(i=0;i<a.length;i++)if("string"===typeof a[i]&&-1<a[i].indexOf(b[j])){e=b[j].match(w)&&null===b[j].match(r)?RegExp("\\b"+b[j]+"\\b","gm"):b[j];
e=a[i].split(e);f=e.length-1;h=0;for(g=e.length;0<g;g--)e.splice(g-1,0,d),h+=1,h===f&&(g=0);a[i]=e}a=o(a)}a=o(a);for(b=0;b<a.length;b++)"string"===typeof a[b]&&(a[b]=c.process.sanitize(a[b]));return a};c.init=function(a,b){if(b)for(var d in l)l.hasOwnProperty(d)&&void 0!==b[d]&&b[d]!==l[d]&&(l[d]=b[d]);""===l.selector&&"all"!==a?l.selector='pre code[data-type="'+l.language+'"]':"all"===a&&""===l.selector&&(l.selector="pre code[data-type]");"all"===a?a=u.querySelectorAll(l.selector):"string"===typeof a&&
(a=u.querySelectorAll(a));for(d=0;d<a.length;d++)c(a[d],l)};c.getKeywords=function(a){return"\\b"+a.join("\\b|\\b")+"\\b"};c.language={};c.data={};c.status=function(){};x.highlightJavascript=c}else console&&"function"===typeof console.error&&console.error("Your browser does not support document.querySelector. Please use a newer browser.")})(window);