📄 x2codehighlighter.js
字号:
/*
X2CodeHighlighter V1.1 @2005.9
powered by supNate
@ http://www.X2Design.net
*/
Array.prototype.exist=function(_obj){
for(var i=0;i<this.length;i++){
if(this[i]==_obj)return true;
}
return false;
}
var fb1="<font color='";
var fb2="'>";
var fb3="</font>";
String.prototype.escapeHTML3=function(){
return this.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/\"/g,""").replace(/ /g," ").replace(/\t/g," ").replace(/\n/g,"<br />");
}
String.prototype.escapeHTML2=function(){
//return this;
return this.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/\"/g,""").replace(/ /g," ").replace(/\t/g," ").replace(/\n/g,"<br />");
}
String.prototype.unescapeHTML2=function(){
return this.replace(/</g,"<").replace(/>/g,">").replace(/"/g,"\"").replace(/ /g," ").replace(/&/g,"&");
}
document.X2Lighter=1;
var Lighter=Class.create();
Lighter.langs=new Array();
Lighter.supportedLanguages=new Array();
Lighter.addLanguage=function(_option){
Lighter.supportedLanguages.push({
objectColor:"#ff0000",
lineNumberColor:"#006600",
lineNumberSepColor:"#006600",
reservedWordColor:"#0000ff",
normalWordColor:"#000000",
delimiterColor:"#000000",
propertyColor:"#008684",
methodColor:"#a52a2a",
stringColor:"#ff00ff",
baseColor:"#000000",
commentColor:"#006600"
}.extend(_option || {}));
Lighter.langs.push(_option.language);
}
Lighter.prototype = {
retarray:0,
initialize:function(){
this.currentWord="";
this.currentState="";
this.allCode="";
this.element=null;
this.lan=null;
this.language="";
if(arguments.length==2){
this.element=arguments[0];
this.language=arguments[1];
}
},
highlight:function(code){
for(var j=0;j<Lighter.supportedLanguages.length;j++){
if(Lighter.supportedLanguages[j].language==this.language){
this.lan=Lighter.supportedLanguages[j];
break;
}
}
if(this.lan==null)return code;
var c="";
this.retarray=new Array();
this.retarray.push("<div style='color:"+this.lan.baseColor+"'>");
this.currentWord="";
this.currentState="";
this.allCode=code.replace(/<br *\/?>/ig,"\n").replace(/<p>/ig,"\n").stripTags().unescapeHTML2();
for(var i=0;i<this.allCode.length;i++){
//find comment begin
var commentFlag=true;
//find line comment
for(var s=0;s<this.lan.linecomment.length;s++){
if(s+i==this.allCode.length && s<this.lan.linecomment.length){
commentFlag=false;
break;
}
if(this.lan.linecomment.charAt(s)!=this.allCode.charAt(i+s))commentFlag=false;
}
if(this.lan.linecomment.length==0)commentFlag=false;
if(commentFlag){
this.changeToState("comment");
this.currentWord+=this.lan.linecomment;
i+=this.lan.linecomment.length;
while(i<this.allCode.length && this.allCode.charAt(i)!="\n"){
this.currentWord+=this.allCode.charAt(i);
i++;
}
if(i<this.allCode.length)this.currentWord+=this.allCode.charAt(i);
continue;
}
//find multiline comment
commentFlag=true;
for(var s=0;s<this.lan.commenton.length;s++){
if(s+i==this.allCode.length && s<this.lan.commenton.length){
commentFlag=false;
break;
}
if(this.lan.commenton.charAt(s)!=this.allCode.charAt(i+s))commentFlag=false;
}
if(this.lan.commenton.length==0 || this.lan.commentoff.length==0)commentFlag=false;
if(commentFlag){
this.changeToState("comment");
this.currentWord+=this.lan.commenton;
i+=this.lan.commenton.length;
var offpos=this.allCode.indexOf(this.lan.commentoff,i)+this.lan.commentoff.length;
if(offpos==this.lan.commentoff.length-1)offpos=this.allCode.length;
for(var s=i;s<offpos;s++){
this.currentWord+=this.allCode.charAt(s);
}
i=offpos-1;
continue;
}
//find comment end
c=this.allCode.charAt(i);
if(/\w/.test(c)){ //find a current word
this.changeToState("word");
this.currentWord+=c;
}
else if(c==this.lan.quotation1 || c==this.lan.quotation2){ //find string
var quot=c;
this.changeToState("string");
this.currentWord+=c;
i++;
while(i<this.allCode.length && this.allCode.charAt(i)!=quot && this.allCode.charAt(i)!="\n"){
if(this.allCode.charAt(i)==this.lan.escape && i<this.allCode.length-1){ //find escaped char in a string
this.currentWord+=this.allCode.charAt(i);
i++;
}
this.currentWord+=this.allCode.charAt(i);
i++;
}
if(i<this.allCode.length)this.currentWord+=this.allCode.charAt(i);
}
else if(this.lan.delimiter.indexOf(c)!=-1){
this.changeToState("delimiter");
this.currentWord+=c;
}
else if(c=="\n"){
this.changeToState("lineEnd");
this.currentWord+=c;
}
else{
this.changeToState("other");
this.currentWord+=c;
}
}
this.changeToState("end");
this.retarray.push("</div>");
this.result=this.retarray.join("");
this.result=this.result.replace(/\n/g,"<br />");
this.result=this.result.replace(/\t/g," ");
return this.result;
},
changeToState:function(_state){
if(this.currentState!=_state){
if(this.currentWord.length>0)this.handleCurrentWord();
this.currentState=_state;
}
},
handleCurrentWord:function(){
switch(this.currentState){
case "word":
if(this.lan.reservedWords.exist(this.currentWord)){
this.retarray.push(fb1+this.lan.reservedWordColor+fb2+this.currentWord+fb3);
}
else if(this.lan.objects.exist(this.currentWord)){
this.retarray.push(fb1+this.lan.objectColor+fb2+this.currentWord+fb3);
}
else if(this.lan.properties.exist(this.currentWord)){
this.retarray.push(fb1+this.lan.propertyColor+fb2+this.currentWord+fb3);
}
else if(this.lan.methods.exist(this.currentWord)){
this.retarray.push(fb1+this.lan.methodColor+fb2+this.currentWord+fb3);
}
else if(this.lan.properties.exist(this.currentWord)){
this.retarray.push(fb1+this.lan.propertyColor+fb2+this.currentWord+fb3);
}
else{
this.retarray.push(fb1+this.lan.normalWordColor+fb2+this.currentWord+fb3);
}
break;
case "string":
this.retarray.push(fb1+this.lan.stringColor+fb2+this.currentWord.escapeHTML2()+fb3);
break;
case "comment":
this.retarray.push(fb1+this.lan.commentColor+fb2+this.currentWord.escapeHTML2()+fb3);
break;
case "delimiter":
this.retarray.push(fb1+this.lan.delimiterColor+fb2+this.currentWord.escapeHTML2()+fb3);
break;
case "other":
this.retarray.push(this.currentWord.escapeHTML2());
break;
case "lineEnd":
this.retarray.push(this.currentWord.escapeHTML2());
break;
case "end":
this.retarray.push(this.currentWord.escapeHTML2());
break;
default:
break;
}
this.currentState="";
this.currentWord="";
}
}
function highLightCode(){
var lighter=new Lighter();
var cls=document.getElementsByClassName("codeArea");
var className="";
var lang="";
for(var i=0;i<cls.length;i++)
{
className=cls[i].className;
if(!className.match(" "))continue;
lang=className.split(" ")[1];
if(!Lighter.langs.exist(lang)){
var s=$import("js/codeLighter/lang/"+lang+".js","js");
Event.observe(s,"load",abc(cls[i],lighter,lang),1);
Event.observe(s,"readystatechange",abc(cls[i],lighter,lang,s),1);
}else{
lighter.language=lang;
cls[i].innerHTML=lighter.highlight(cls[i].innerHTML);
}
}
}
function abc(a,b,c,s){
return function(){
if(s && s.readyState=="loading")return;
b.language=c;
a.innerHTML=b.highlight(a.innerHTML);
}
}
Event.observe(window,"load",highLightCode,1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -