您的位置:首页 > Web前端 > JavaScript

JScript多语言语法加亮引擎(不断更新中,最后更新:2006-05-09)

2006-05-14 16:42 375 查看
一个用JScript加正则表达式***的多语言语法加亮引擎,目前不断更新中,欢迎您加入一起研究。

请看相关文章(按发布时间先后排序):

JScript中正则表达式用法详解( 05-06 10:02)
JScript多语言语法加亮引擎显示(代码精简) ( 05-06 16:45)
JScript多语言语法加亮引擎改进(添加注释识别) ( 05-08 09:23)
JScript多语言语法加亮引擎改进(修正多行注释识别) ( 05-08 18:16)
JScript多语言语法加亮引擎改进(修正号识别,添加行号) ( 05-09 11:02)

项目远景:
1.支持多语言,并可灵活添加语言支持
2.每种语言支持最多5种类型的关键字
3.代码简洁,使用者可以根据需要二次开发

目前等待解决的问题有:
1.多语言支持,现在还是停留在只支持C#的阶段,一步步来吧,呵呵
2.代码折叠的支持

代码文件下载(时间先后排序):
%20SourceHighlight01.rar][2006-05-06-04] SourceHighlight01.rar
%20SourceHighlight01.rar][2006-05-08-01] SourceHighlight01.rar
%20SourceHighlight01.rar][2006-05-09-01] SourceHighlight01.rar

相关工具:
RegexDesigerSetup1_1.zip 开源免费又好用的正则工具
regex buddy 2.06.zip 帮助很全,功能也很不错,特别是可以生成各种语言的代码

◎如果大家对代码有什么不理解的或者使用中有什么问题,欢迎在这里提出。

最新版Demo:

◎请在文本框中输入代码,或者复制一段代码到文本框中,如果看不到效果请按“Execute”按钮




测试用C#代码


public class Test






{


private string _name;




public string Name






{




get

{ return _name;}


}




public void SetName(string name)






{


//执行某些操作


}


}

◎请在文本框中输入代码,或者复制一段代码到文本框中,如果看不到效果请按“Execute”按钮


.sourceCode
{
color:#000000;
background:#FFFFFF;
width:100%;
height:200px;
border:1px solid #000000;
font-size:12px;
padding-top:4px;
padding-bottom:4px;
}

#HtmlCode .keyWord1
{
color:#0000FF;
}

#HtmlCode .keyWord2
{
color:#FF0048;
}

#HtmlCode .keyWord3,
#HtmlCode .keyWord4,
#HtmlCode .keyWord5
{
color:#0000FF;
}

#HtmlCode #stringCSS,
#stringCSS .keyWord1,
#stringCSS .keyWord2,
#stringCSS .keyWord3,
#stringCSS .keyWord4,
#stringCSS .keyWord5
{
color:#008000;
}

#lineCSS
{
width:48px;
border-right:1px solid #000000;
text-align:right;
margin-right:8px;
padding-right:4px;
}

#HtmlCode #commentCSS,
#commentCSS .keyWord1,
#commentCSS .keyWord2,
#commentCSS .keyWord3,
#commentCSS .keyWord4,
#commentCSS .keyWord5
{
color:#FF0000;
}
#commentCSS #lineCSS
{
color:#000000;
}


//ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WEBDEV.v10.en/script56/html/js56jsobjregexp.htm
//ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.WEBDEV.v10.en/script56/html/js56jsmthreplace.htm

var htmlMode = false;
var codeType = "CSharp";

//关键字匹配正则
var keyWordRegex;

//字符串匹配正则
var stringRegex1;
var stringRegex2;

var lineCommentRegex;
var multiLineCommentRegex;

var lineRegex;

function InitRegex()
{
switch(codeType)
{
case "CSharp":
keyWordRegex = new RegExp();

stringRegex1 = new RegExp();
stringRegex2 = new RegExp();

lineCommentRegex = new RegExp();
multiLineCommentRegex = new RegExp();

lineRegex = new RegExp();
keyWordRegex.compile(//b(as|auto|base|break|case|catch|const|continue|default|do|else|event|explicit|extern|false|finally|fixed|for|foreach|goto|get|if|implicit|in|internal|lock|namespace|new|null|operator|out|override|params|private|protected|public|partial|readonly|ref|return|sealed|stackalloc|static|switch|this|throw|true|try|unsafe|using|virtual|void|while|set)/b|/b(bool|byte|char|class|decimal|delegate|double|enum|float|int|interface|long|object|sbyte|short|string|struct|uint|ulong|ushort)/b/g);

stringRegex1.compile(/"[^"///r/n]*(//.[^"///r/n]*)*"/g);
stringRegex2.compile(/@"[^"//]*(//.[^"//]*)*"/g);

lineCommentRegex.compile("//.*$", "gm");
multiLineCommentRegex.compile("///*[.//b//w//W]*//*/", "g");

lineRegex.compile(/^.*(/n|$)/gm);

break;
case "VBDotNet":
break;
case "CPP":
break;
case "XML":
break;
case "JScript":
break;
}
}

function Execute()
{
//var startTime = new Date();

if(htmlMode) htmlMode = false;

InitRegex();

var temp = document.getElementById("code").innerText;

//将$符号转为$,因为后面使用了$作为标记
temp = temp.replace(//$/g, "$$");

//替换空白符与特殊字符
temp = temp.replace(/&/g, "&");
temp = temp.replace(/ /g, " ");
temp = temp.replace(//t/g, " ");
temp = temp.replace(//g, ">");

//把代码拆分成行,然后每行间加入行号和标签
var _lines = temp.split("/n");
var _newCode = new Array();

for(var i=0; i<_lines.length; i++)
{
_newCode.push(" $LINE ");
_newCode.push(i+1);
_newCode.push(" $LINEE ");
_newCode.push(_lines[i]);
_newCode.push("/n");
}

//重新组合行
temp = _newCode.join(" ");

//alert(temp);

//匹配关键字
temp = temp.replace(keyWordRegex, " $KW1 $1 $KWE $KW2 $2 $KWE $KW3 $3 $KWE $KW4 $4 $KWE $KW5 $5 $KWE");

//匹配字符串
temp = temp.replace(stringRegex1, " $STR1___FCKsi___1 $STRE");
temp = temp.replace(stringRegex2, " $STR2___FCKsi___1 $STRE");

//
temp = temp.replace(lineCommentRegex, " $COM1___FCKsi___1 $COME");
temp = temp.replace(multiLineCommentRegex, " $COM2___FCKsi___1 $COME");

//清除冗余的关键字加亮代码
temp = temp.replace(/[^/$]/$KW/d [^/$]/$KWE/g, "");
temp = temp.replace(/[^/$]/$KW/d[^/$]/$/d[^/$]/$KWE/g, "");

//将匹配结果替换为HTML标签
temp = temp.replace(//n/g, "
/n");
//temp = temp.replace(/[^/$]/$BR /g, "
/n");

temp = temp.replace(/[^/$]/$KW1 /g, "");
temp = temp.replace(/[^/$]/$KW2 /g, "");
temp = temp.replace(/[^/$]/$KW3 /g, "");
temp = temp.replace(/[^/$]/$KW4 /g, "");
temp = temp.replace(/[^/$]/$KW5 /g, "");
temp = temp.replace(/[^/$]/$KWE/g, "");

temp = temp.replace(/[^/$]/$STR1/g, "");
temp = temp.replace(/[^/$]/$STR2/g, "");
temp = temp.replace(/[^/$]/$STRE/g, "");

temp = temp.replace(/[^/$]/$COM1/g, "");
temp = temp.replace(/[^/$]/$COM2/g, "");
temp = temp.replace(/[^/$]/$COME/g, "");

temp = temp.replace(/([^/$]/$LINE ) /g, "");
temp = temp.replace(/([^/$]/$LINEE ) /g, "");

//还原$符号
temp = temp.replace(//$/$/g, "$");

document.getElementById("HtmlCode").innerHTML = temp;

//alert("耗时:" + (new Date().getTime()-startTime.getTime()) + "ms");
}




介于对某些浏览器不支持,我抓张图放这里,呵呵。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: