-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh_main.min.js
3 lines (3 loc) · 5.42 KB
/
sh_main.min.js
1
2
3
/* Copyright (C) 2007, 2008 gnombat@users.sourceforge.net */
/* License: http://shjs.sourceforge.net/doc/license.html */
if(!this.sh_languages){this.sh_languages={}}var sh_requests={};function sh_highlightString(b,S,R){var Y={_stack:[],getLength:function(){return this._stack.length},getTop:function(){var d=this._stack;var e=d.length;if(e===0){return undefined}return d[e-1]},push:function(d){this._stack.push(d)},pop:function(){if(this._stack.length===0){throw"pop on empty stack"}this._stack.pop()}};var H=0;var O=undefined;var L=function(e,d){var f=e.length;if(f===0){return }if(!d){var g=Y.getTop();if(g!==undefined&&!("state" in g)){d=g.style}}if(O!==d){if(O){R.endElement()}if(d){R.startElement(d)}}R.text(e);H+=f;O=d};var Q=/\r\n|\r|\n/g;Q.lastIndex=0;var X=b.length;while(H<X){var G=H;var D;var U;var a=Q.exec(b);if(a===null){D=X;U=X}else{D=a.index;U=Q.lastIndex}var M=b.substring(G,D);var J=null;var I=-1;for(;;){var B=H-G;var W=Y.getTop();var F=W===undefined?0:W.next;var E=S[F];var c=E.length;if(F!==I){J=[]}var A=null;var N=-1;for(var V=0;V<c;V++){var K;if(F===I&&(J[V]===null||B<=J[V].index)){K=J[V]}else{var C=E[V].regex;C.lastIndex=B;K=C.exec(M);J[V]=K}if(K!==null&&(A===null||K.index<A.index)){A=K;N=V}}I=F;if(A===null){L(M.substring(B),null);break}else{if(A.index>B){L(M.substring(B,A.index),null)}W=E[N];var P=W.style;var T;if(P instanceof Array){for(var Z=0;Z<P.length;Z++){T=A[Z+1];L(T,P[Z])}}else{T=A[0];L(T,P)}if("next" in W){Y.push(W)}else{if("exit" in W){Y.pop()}if("exitall" in W){while(Y.getLength()>0){Y.pop()}}}}}if(O){R.endElement()}O=undefined;if(a){R.text(a[0])}H=U}}function sh_getClasses(D){var A=[];var B=D.className;if(B&&B.length>0){var E=B.split(" ");for(var C=0;C<E.length;C++){if(E[C].length>0){A.push(E[C])}}}return A}function sh_addClass(C,A){var D=sh_getClasses(C);for(var B=0;B<D.length;B++){if(A.toLowerCase()===D[B].toLowerCase()){return }}D.push(A);C.className=D.join(" ")}function sh_getText(C){if(C.nodeType===3||C.nodeType===4){return C.data}else{if(C.nodeType===1&&C.tagName==="BR"){return"\n"}else{if(C.childNodes.length===1){return sh_getText(C.firstChild)}else{var A="";for(var B=0;B<C.childNodes.length;B++){A+=sh_getText(C.childNodes.item(B))}return A}}}}function sh_isEmailAddress(A){if(/^mailto:/.test(A)){return false}return A.indexOf("@")!==-1}var sh_builder={init:function(B,A){while(A.hasChildNodes()){A.removeChild(A.firstChild)}this._document=B;this._element=A;this._currentText=null;this._documentFragment=B.createDocumentFragment();this._currentParent=this._documentFragment;this._span=B.createElement("span");this._a=B.createElement("a")},startElement:function(B){if(this._currentText!==null){this._currentParent.appendChild(this._document.createTextNode(this._currentText));this._currentText=null}var A=this._span.cloneNode(true);A.className=B;this._currentParent.appendChild(A);this._currentParent=A},endElement:function(){if(this._currentText!==null){if(this._currentParent.className==="sh_url"){var A=this._a.cloneNode(true);A.className="sh_url";var B=this._currentText;if(B.length>0&&B.charAt(0)==="<"&&B.charAt(B.length-1)===">"){B=B.substr(1,B.length-2)}if(sh_isEmailAddress(B)){B="mailto:"+B}A.setAttribute("href",B);A.appendChild(this._document.createTextNode(this._currentText));this._currentParent.appendChild(A)}else{this._currentParent.appendChild(this._document.createTextNode(this._currentText))}this._currentText=null}this._currentParent=this._currentParent.parentNode},text:function(A){if(this._currentText===null){this._currentText=A}else{this._currentText+=A}},close:function(){if(this._currentText!==null){this._currentParent.appendChild(this._document.createTextNode(this._currentText));this._currentText=null}this._element.appendChild(this._documentFragment)}};function sh_highlightElement(B,A,D){sh_addClass(A,"sh_sourceCode");var C;if(A.childNodes.length===0){return }else{C=sh_getText(A)}sh_builder.init(B,A);sh_highlightString(C,D,sh_builder);sh_builder.close()}function sh_getXMLHttpRequest(){if(window.ActiveXObject){return new ActiveXObject("Msxml2.XMLHTTP")}else{if(window.XMLHttpRequest){return new XMLHttpRequest()}}throw"No XMLHttpRequest implementation available"}function sh_load(language,prefix,suffix){if(language in sh_requests){return }sh_requests[language]=1;var request=sh_getXMLHttpRequest();var url=prefix+"sh_"+language+suffix;request.open("GET",url,true);request.onreadystatechange=function(){if(request.readyState===4){try{if(request.status===0||request.status===200){eval(request.responseText);var nodeList=document.getElementsByTagName("pre");for(var i=0;i<nodeList.length;i++){var element=nodeList.item(i);var htmlClasses=sh_getClasses(element);for(var j=0;j<htmlClasses.length;j++){var htmlClass=htmlClasses[j].toLowerCase();if(htmlClass==="sh_sourcecode"){continue}if(htmlClass==="sh_"+language){sh_highlightElement(document,element,sh_languages[language]);break}}}}else{throw"HTTP error: status "+request.status}}finally{request=null}}};request.send(null)}function sh_highlightHTMLDocument(H,G,J){var B=H.getElementsByTagName("pre");for(var E=0;E<B.length;E++){var F=B.item(E);var A=sh_getClasses(F);for(var C=0;C<A.length;C++){var I=A[C].toLowerCase();if(I==="sh_sourcecode"){continue}if(I.substr(0,3)==="sh_"){var D=I.substring(3);if(D in sh_languages){sh_highlightElement(H,F,sh_languages[D])}else{if(typeof (G)==="string"&&typeof (J)==="string"){sh_load(D,G,J)}else{throw"Found <pre> element with class='"+I+"', but no such language exists"}}break}}}}function sh_highlightDocument(A,B){sh_highlightHTMLDocument(document,A,B)};