📄 _codehighlight.js
字号:
//标记
this._tags = this.str2hashtable("html,head,body,title,style,script,language,input,select,div,span,button,img,iframe,frame,frameset,table,tr,td,caption,form,font,meta,textarea",false);
//得到分割字符
this._wordDelimiters= " ,.?!;:\\/<>(){}[]\"'\r\n\t=+-|*%@#$^&";
//引用字符
this._quotation = this.str2hashtable("\",'");
//行注释字符
this._lineComment = "//";
//转义字符
this._escape = "\\";
//多行引用开始
this._commentOn = "/*";
//多行引用结束
this._commentOff = "*/";
//忽略词
this._ignore = "<!--";
//是否处理标记
this._dealTag = true;
break;
case "xml":
default:
//是否大小写敏感
this._caseSensitive = true;
//得到关键字哈希表
this._keywords = this.str2hashtable("!DOCTYPE,?xml,script,version,encoding");
//得到内建对象哈希表
this._commonObjects = this.str2hashtable("");
//标记
this._tags = this.str2hashtable("",false);
//得到分割字符
this._wordDelimiters= " ,.;:\\/<>(){}[]\"'\r\n\t=+-|*%@#$^&";
//引用字符
this._quotation = this.str2hashtable("\",'");
//行注释字符
this._lineComment = "";
//转义字符
this._escape = "\\";
//多行引用开始
this._commentOn = "<!--";
//多行引用结束
this._commentOff = "-->";
//忽略词
this._ignore = "<!--";
//是否处理标记
this._dealTag = true;
break;
}
this.highlight = function() {
var codeArr = new Array();
var word_index = 0;
var htmlTxt = new Array();
//得到分割字符数组(分词)
for (var i = 0; i < this._codetxt.length; i++) {
if (this._wordDelimiters.indexOf(this._codetxt.charAt(i)) == -1) { //找不到关键字
if (codeArr[word_index] == null || typeof(codeArr[word_index]) == 'undefined') {
codeArr[word_index] = "";
}
codeArr[word_index] += this._codetxt.charAt(i);
} else {
if (typeof(codeArr[word_index]) != 'undefined' && codeArr[word_index].length > 0)
word_index++;
codeArr[word_index++] = this._codetxt.charAt(i);
}
}
var quote_opened = false; //引用标记
var slash_star_comment_opened = false; //多行注释标记
var slash_slash_comment_opened = false; //单行注释标记
var line_num = 1; //行号
var quote_char = ""; //引用标记类型
var tag_opened = false; //标记开始
htmlTxt[htmlTxt.length] = ("<div style='font-family: Courier New;font-size:12px;overflow:auto;border-width:1px;border-style:solid;border-color:#8a8a8a;background-color:#eeeeee;margin:1px;padding:6px;'>");
//按分割字,分块显示
for (var i=0; i <=word_index; i++){
//处理空行(由于转义带来)
if(typeof(codeArr[i])=="undefined"||codeArr[i].length==0){
continue;
}
//处理空格
if (codeArr[i] == " "){
htmlTxt[htmlTxt.length] = (" ");
//处理关键字
} else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isKeyword(codeArr[i])){
htmlTxt[htmlTxt.length] = ("<span style='color:#0000FF;'>" + codeArr[i] + "</span>");
//处理普通对象
} else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isCommonObject(codeArr[i])){
htmlTxt[htmlTxt.length] = ("<span style='color:#808000;'>" + codeArr[i] + "</span>");
//处理标记
} else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && tag_opened && this.isTag(codeArr[i])){
htmlTxt[htmlTxt.length] = ("<span style='color:#0000FF;'>" + codeArr[i] + "</span>");
//处理换行
} else if (codeArr[i] == "\n"){
if (slash_slash_comment_opened){
htmlTxt[htmlTxt.length] = ("</span>");
slash_slash_comment_opened = false;
}
htmlTxt[htmlTxt.length] = ("<br/>");
line_num++;
//处理双引号(引号前不能为转义字符)
} else if (this._quotation.contains(codeArr[i])&&!slash_star_comment_opened&&!slash_slash_comment_opened){
if (quote_opened){
//是相应的引号
if(quote_char==codeArr[i]){
if(tag_opened){
htmlTxt[htmlTxt.length] = (codeArr[i]+"</span><span style='color:#808000;'>");
} else {
htmlTxt[htmlTxt.length] = (codeArr[i]+"</span>");
}
quote_opened = false;
quote_char = "";
} else {
htmlTxt[htmlTxt.length] = codeArr[i].replace(/\</g,"<");
}
} else {
if(tag_opened){
htmlTxt[htmlTxt.length] = ("</span><span style='color:#FF00FF;'>"+codeArr[i]);
} else {
htmlTxt[htmlTxt.length] = ("<span style='color:#FF00FF;'>"+codeArr[i]);
}
quote_opened = true;
quote_char = codeArr[i];
}
//处理转义字符
} else if(codeArr[i] == this._escape){
htmlTxt[htmlTxt.length] = (codeArr[i]);
if(i<word_index-1){
if(codeArr[i+1].charCodeAt(0)>=32&&codeArr[i+1].charCodeAt(0)<=127){
htmlTxt[htmlTxt.length] = codeArr[i+1].substr(0,1).replace("&","&").replace(/\</g,"<");
codeArr[i+1] = codeArr[i+1].substr(1);
}
}
//处理Tab
} else if (codeArr[i] == "\t") {
htmlTxt[htmlTxt.length] = (" ");
//处理多行注释的开始
} else if (this.isStartWith(this._commentOn,codeArr,i)&&!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened){
slash_star_comment_opened = true;
htmlTxt[htmlTxt.length] = ("<span style='color:#008000;'>" + this._commentOn.replace(/\</g,"<"));
i = i + this._commentOn.length-1;
//处理单行注释
} else if (this.isStartWith(this._lineComment,codeArr,i)&&!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened){
slash_slash_comment_opened = true;
htmlTxt[htmlTxt.length] = ("<span style='color:#008000;'>" + this._lineComment);
i = i + this._lineComment.length-1;
//处理忽略词
} else if (this.isStartWith(this._ignore,codeArr,i)&&!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened){
slash_slash_comment_opened = true;
htmlTxt[htmlTxt.length] = ("<span style='color:#008000;'>" + this._ignore.replace(/\</g,"<"));
i = i + this._ignore.length-1;
//处理多行注释结束
} else if (this.isStartWith(this._commentOff,codeArr,i)&&!quote_opened&&!slash_slash_comment_opened){
if (slash_star_comment_opened) {
slash_star_comment_opened = false;
htmlTxt[htmlTxt.length] = (this._commentOff +"</span>");
i = i + this._commentOff.length-1;
}
//处理左标记
} else if (this._dealTag&&!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&codeArr[i] == "<") {
htmlTxt[htmlTxt.length] = "<<span style='color:#808000;'>";
tag_opened = true;
//处理右标记
} else if (this._dealTag&&tag_opened&&codeArr[i] == ">") {
htmlTxt[htmlTxt.length] = "</span>>";
tag_opened = false;
//处理HTML转义符号
} else if (codeArr[i] == "&") {
htmlTxt[htmlTxt.length] = "&";
} else {
htmlTxt[htmlTxt.length] = codeArr[i].replace(/\</g,"<");
}
}
htmlTxt[htmlTxt.length] = ("</div>");
return htmlTxt.join("");
}
this.isStartWith = function(str,code,index){
if(typeof(str)!="undefined"&&str.length>0){
for(var i=0;i<str.length;i++){
if(this._caseSensitive){
if(str.charAt(i)!=code[index+i]||(index+i>=code.length)){
return false;
}
} else {
if(str.charAt(i).toLowerCase()!=code[index+i].toLowerCase()||(index+i>=code.length)){
return false;
}
}
}
return true;
} else {
return false;
}
}
this.isKeyword = function(val) {
return this._keywords.contains(this._caseSensitive?val:val.toLowerCase());
}
this.isCommonObject = function(val) {
return this._commonObjects.contains(this._caseSensitive?val:val.toLowerCase());
}
this.isTag = function(val) {
return this._tags.contains(val.toLowerCase());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -