仿照大炮的 CodeSyntaxHighlighter 2.0
制作了这个简陋版语法着色东东
目前只提供Php和JavaScript代码的语法高亮
在IE6,Firefox3.0,Maxthon1.6,Opera9.2中测试通过由于解析速度主要由需着色关键字数目决定
所以PHP部分只提供了200左右的常用关键字
而Javascript部分包含了自身和DHTML中绝大部分关键字还不支持对自定义变量着色
在复杂的语法环境下解析会有异常,如:"'"+'tests'+"'"欢迎各位拍转
http://hi.baidu.com/wyw5257/blog/item/2f6451f06e0bbcc47931aa14.html效果如下:JS:
PHP:核心脚本 codeCore.js
完整内容: 附件内容
/*
 * simple code syntax highlighter
 *
 * Version: 1.0
 * Date: 2008-11-06
 * Author: wyw5257
 * Blog: http://hi.baidu.com/wyw5257
 * 
**/var yz = {
};yz.util = { evalScript : function(c,id) {
if(!id) id=(new Date()).getTime();
var o = document.createElement("script");
o.id=id; o.type='text/javascript'; o.defer=true;
if(typeof(c)=="string") {
var p = new RegExp("^<scr"+"ipt[^>]+?scr=([^\\s>]+)","i"), src=c.match(p);
if(src) o.src=src;
else o.text=c;
}
if(typeof(c)=="object") {
if(c.src) o.src=c.src;
else o.text=c.innerHTML;
}
document.body.appendChild(o);
return id;
},

extend : function(a,b) {
for ( var i in b ) a[i] = b[i];
return a;
}, replaceAll : function(a,b,c) {
for(var l=a.length,i=0;i<l;i++)
c = c.replace(a[i],b[i]);
return c;
}, regVars : function(a,b) {
return new RegExp("(\\b)("+a.split(" ").join("|")+")(\\b)",'g'+(typeof b=='undefined'?'':b));
}
};yz.highlighter = {
multiComment : { reg:new RegExp('(?:^|[^/])(/\\*[\\s\\S]*?\\*/)',"gm"), css:"cc", type:"mc" },
singleComment : { reg:new RegExp('//.*',"g"), css:"cc" },
doubleQuoted : { reg:new RegExp('"(?:(\\\\")|[^"\\n])*"','g'), css:"cq" },
singleQuoted : { reg:new RegExp("'(?:(\\\\')|[^'\\n])*'","g"), css:"cq" },
parseBox : [],
pathRoot : "./",

setScriptPath : function(s) {
yz.highlighter.pathRoot = s;
}, parse : function(s,t,type) {
if(typeof yz.highlighter.parseBox[type]=='undefined') {
yz.util.evalScript({src:yz.highlighter.pathRoot+"code"+type+".js"});
window.setTimeout( function() {
eval("yz.highlighter.parseBox['"+type+"'] = new yz.highlighter.sh."+type+"();");
yz.highlighter.sh({ source:s, target:t, regs:yz.highlighter.parseBox[type].regs });
}, 100);
} else {
yz.highlighter.sh({ source:s, target:t, regs:yz.highlighter.parseBox[type].regs });
}
}
};yz.highlighter.sh = function(options) { options = yz.util.extend({
source:null,
target:null,
regs:[]
}, options); function repSpecials(s) {
return yz.util.replaceAll([/</g,/>/g,/\$/g,/\r/g],["&lt;","&gt;","&#36;",""],s);
} function repWhiteSpace(s) {
return yz.util.replaceAll([/^ /gm,/  /g,/\t/g,/\n\n/g],["&nbsp;"," &nbsp;","&nbsp;&nbsp;&nbsp;&nbsp;","\n&nbsp;\n"],s);
} function repRegExps(s) {
for(var l=options.regs.length,i=0;i<l;i++) {
if(options.regs[i].type == "mc") {
s = repMultiComment(options.regs[i].reg,options.regs[i].css,s);
} else if(options.regs[i].type == "keys") {
s = s.replace(options.regs[i].reg,"$1<font class="+options.regs[i].css+">$2</font>$3");
} else {
s = s.replace(options.regs[i].reg,"<font class="+options.regs[i].css+">$&</font>");
}
}
return s;
} function repMultiComment(r,c,s) {
var match=null, matches=[];
while((match=r.exec(s))!=null) {
matches[matches.length] = match[1];
}
var s2 = '';
for(var l=matches.length,i=0;i<l;i++) {
s2 = "<font class="+c+">"+matches[i].replace(/\n/g,"</font>\n<font class="+c+">")+"</font>";
r = new RegExp(matches[i].replace(/[\^\[\]\{\}\(\)\.\*\+\?\-\_\|\$\\\\]/g,'\\$&'));
s = s.replace(r,s2);
}
return s;
} function innerCode(s) {
document.getElementById(options.target).innerHTML = s;
} function p() {
if(typeof testdt =='function') testdt('s');
var s = document.getElementById(options.source).value;
s = repSpecials(s);
s = repRegExps(s);
s = repWhiteSpace(s);
s = "<ul><li class=first>"+s.replace(/\n/g,"</li>\n<li>")+"</li></ul>";
innerCode(s);
if(typeof testdt =='function') testdt('e');
}

p();
};