在线代码高亮显示

/*  Online Syntax Highlighter - converts sourcecode to html.
    Copyright (C) 2003, 2004  Tobi Vollebregt

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Please send your reaction to: tobivollebregt at hotmail dot com
*/
function isAlpha(ch)
{
	if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) return true;
	return false;
}
function isDigit(ch)
{
	if (ch >= '0' && ch <= '9') return true;
	return false;
}
function isDigitNext(ch)
{
	if (isAlpha(ch) || isDigit(ch) || (ch == '.')) return true;
	return false;
}
function isIdFirst(ch)
{
	if (isAlpha(ch) || (ch == '_') || (ch == '#')) return true;
		// # for C preprocessor keywords
	return false;
}
function isIdNext(ch)
{
	if (isAlpha(ch) || isDigit(ch) || (ch == '_')) return true;
	return false;
}
function isSpace(ch)
{
	if (ch <= 32) return true;
	return false;
}
function fixHtml(s)
{
	var ret="";
	for (var i=0; i<s.length; ++i) {
		var ch=s.charAt(i);
		if (ch=='>') ret+="&gt;";
		else if (ch=='<') ret+="&lt;";
		else if (ch=='/"') ret+="&quot;";
		else if (ch=='&') ret+="&amp;";
		else ret+=ch;
	}
	return ret;
}
function highlightSyntax()
{
	if (document.forms[0].lang_name.value==null || document.forms[0].lang_name.value=="") {
		alert("Please select a programming/scripting language first.");
		return;
	}

	var i=0,j=0;
	var close_tag="</span>";
	var ccomment="<span class=comment>";
	var ckeyword="<span class=keyword>";
	var cnumber="<span class=number>";
	var cstring="<span class=string>";
	var csymbol="<span class=symbol>";

	// Read the language definition

	// End Of Line comments:
	var eol_comment1=document.forms[0].eol_comment.value, eol_comment2=null;
	i=eol_comment1.indexOf(',');
	if (i>=1) {
		eol_comment2=eol_comment1.substring(i+1,eol_comment1.length);
		eol_comment1=eol_comment1.substring(0,i);
	} else {
		if (eol_comment1 == "") eol_comment1 = null;
	}

	// Multiline comments:
	var open_comment1=document.forms[0].open_comment.value, open_comment2=null;
	i=open_comment1.indexOf(',');
	if (i>=1) {
		open_comment2=open_comment1.substring(i+1,open_comment1.length);
		open_comment1=open_comment1.substring(0,i);
	} else {
		if (open_comment1 == "") open_comment1 = null;
	}
	var close_comment1=document.forms[0].close_comment.value, close_comment2=null;
	i=close_comment1.indexOf(',');
	if (i>=1) {
		close_comment2=close_comment1.substring(i+1,close_comment1.length);
		close_comment1=close_comment1.substring(0,i);
	} else {
		if (close_comment1 == "") close_comment1 = null;
	}
	if ((open_comment1 != null && close_comment1 == null) ||
	    (open_comment1 == null && close_comment1 != null) ||
	    (open_comment2 != null && close_comment2 == null) ||
	    (open_comment2 == null && close_comment2 != null)) {
		alert("Number of close comments doesn't match number of open comments./n");
		return;
	}

	var quote1=document.forms[0].string.value, quote2=quote1.charAt(1);
	quote1=quote1.charAt(0);
	var escape=document.forms[0].escape.value.charAt(0);
	var keywords=","+document.forms[0].keywords1.value+","+document.forms[0].keywords2.value;

	// Read the input
	var input=document.forms[1].elements[0].value;

	document.clear();
	document.write("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/" /"http://www.w3.org/TR/html4/loose.dtd/">/n" +
		"<html>/n<head>/n<title>Syntax Highlighter - " + document.forms[0].lang_name.value + "</title>/n" +
		"<link rel=stylesheet type=/"text/css/" href=/"style.css/">/n</head>/n" +
		"<body>/n<p class=/"header/">/n<a href=/"syntax.html/">back</a>/n<hr>/n/n" +
		"<!-- End of header. Generated HTML code begins here. -->/n<pre>/n");

	// Reduce the amount of inserted HTML codes by detecting consecutive elements.
	// 0	-No span
	// 1	-Eol comment
	// 2	-Multiline comment 1
	// 3	-Multiline comment 2
	// 4	-Keywords
	// 5	-Numbers
	// 6	-Strings
	// 7	-Symbols
	var inSpan=0;

	i=0;
	while (i<input.length)
	{
		var ch=input.charAt(i);

		if ((eol_comment1 != null && input.substring(i,i+eol_comment1.length)==eol_comment1) ||
		    (eol_comment2 != null && input.substring(i,i+eol_comment2.length)==eol_comment2)) {

			if (inSpan != 0 && inSpan != 1) document.write(close_tag);
			for(j=i+1; j<input.length && input.charAt(j)!='/n'; ++j) ;
			var cm = fixHtml(input.substring(i, --j));
			i=j;
			if (inSpan != 1) { document.write(ccomment); inSpan = 1; }
			document.write(cm);
		} else
		if (open_comment1 != null && input.substring(i,i+open_comment1.length)==open_comment1) {
			if (inSpan != 0 && inSpan != 2) document.write(close_tag);
			for(j=i+open_comment1.length; j<input.length && 
				input.substring(j,j+close_comment1.length)!=close_comment1; ++j);
			var cm = fixHtml(input.substring (i, j+=close_comment1.length));
			i=j;
			if (inSpan != 2) { document.write(ccomment); inSpan = 2; }
			document.write(cm);
		} else
		if (open_comment2 != null && input.substring(i,i+open_comment2.length)==open_comment2) {
			if (inSpan != 0 && inSpan != 3) document.write(close_tag);
			for(j=i+open_comment2.length; j<input.length &&
				input.substring(j,j+close_comment2.length)!=close_comment2; ++j);
			var cm = fixHtml(input.substring (i, j+=close_comment2.length));
			i=j;
			if (inSpan != 3) { document.write(ccomment); inSpan = 3; }
			document.write(cm);
		} else
		if (isIdFirst(ch)) {
			for (j=i+1; isIdNext(input.charAt(j)); ++j) ;
			var id=input.substring(i, j);
			i=j;
			j=0;
			do {
				var k_end = keywords.indexOf(',', ++j);
				var kw = keywords.substring(j, k_end);
				if (id == kw) {
					if (inSpan != 0 && inSpan != 4) document.write(close_tag);
					if (inSpan != 4) { document.write(ckeyword); inSpan = 4; }
					document.write(kw);
					j=99999999; break;
				}
				j=keywords.indexOf(',', j);
			} while (keywords.charAt(j)==',');
			if (j < 99999999) {
				if (inSpan != 0) { document.write(close_tag); inSpan = 0; }
				document.write(id);
			}
		} else
		if (isDigit(ch)) {
			if (inSpan != 0 && inSpan != 5) document.write(close_tag);
			for (j=i+1; isDigitNext(input.charAt(j)); ++j) ;
			var num=fixHtml(input.substring(i, j));
			i=j;
			if (inSpan != 5) { document.write(cnumber); inSpan = 5; }
			document.write(num);
		} else
		if ((ch==quote1) || (ch==quote2)) {
			if (inSpan != 0 && inSpan != 6) document.write(close_tag);
			for (j=i+1; j<input.length && (input.charAt(j)!=ch ||
				(input.charAt(j-1)==escape && input.charAt(j-2)!=escape)); ++j) ;
			var str=fixHtml(input.substring(i, ++j));
			i=j;
			if (inSpan != 6) { document.write(cstring); inSpan = 6; }
			document.write(str);
		} else
		if (isSpace(ch)) {
			// Here I assume everything will have the same background color
			//  by not writing "if (inSpan) {...}"
			document.write(ch);
			++i;
		} else {
			if (inSpan != 0 && inSpan != 7) document.write(close_tag);
			if (inSpan != 7) { document.write(csymbol); inSpan = 7; }
			document.write(fixHtml(ch));
			++i;
		}
	}
	if (inSpan != 0) document.write(close_tag);
	document.write("</pre>/n<!-- End of generated HTML code. Footer begins here. -->/n/n" +
		"<hr>/n<p class=/"header/">/n<a href=/"syntax.html/">back</a>/n</body>/n</html>/n");
}
function loadCppDefaults()
{
	document.forms[0].lang_name.value="C/C++";
	document.forms[0].eol_comment.value="//";
	document.forms[0].open_comment.value="/*";
	document.forms[0].close_comment.value="*/";
	document.forms[0].string.value="/"'";
	document.forms[0].escape.value="//";
	document.forms[0].keywords1.value=
		"asm,auto,break,case,catch,char,class,const,continue,default,delete," +
		"do,double,else,enum,except,extern,far,finally,float,for,friend,goto," +
		"huge,if,inline,int,bool,string,long,fixed,near,new,operator,private," +
		"protected,typename,false," +
		"public,register,return,short,signed,sizeof,static,struct,switch," +
		"template,this,throw,try,typedef,union,unsigned,virtual,void,volatile," +
		"while,#define,#elif,#else,#endif,#error,#ifdef,#ifndef,#if,#include," +
		"#line,#pragma,#undef,#warning,using,namespace,true," +
		"s8,u8,s16,u16,s32,u32,s64,u64,s128,u128,f32," + 
		"and,or,xor,not,explicit,mutable," +
		"const_cast,dynamic_cast,reinterpret_cast,static_cast";
}
function loadJavaDefaults()
{
	document.forms[0].lang_name.value="Java";
	document.forms[0].eol_comment.value="//";
	document.forms[0].open_comment.value="/*";
	document.forms[0].close_comment.value="*/";
	document.forms[0].string.value="/"'";
	document.forms[0].escape.value="//";
	document.forms[0].keywords1.value=
		"abstract,boolean,break,byte,case,cast,catch,char," +
		"class,const,continue,default,do,double,else,extends," +
		"false,final,finally,float,for,future,generic,goto,if," +
		"implements,import,inner,instanceof,int,interface,long,native," +
		"new,null,operator,outer,package,private,protected,public," +
		"rest,return,short,static,super,switch,synchronized,this," +
		"throw,throws,transient,true,try,var,void,volatile,while";
}
function loadJavaScriptDefaults()
{
	document.forms[0].lang_name.value="JavaScript";
	document.forms[0].eol_comment.value="//";
	document.forms[0].open_comment.value="/*";
	document.forms[0].close_comment.value="*/";
	document.forms[0].string.value="/"'";
	document.forms[0].escape.value="//";
	document.forms[0].keywords1.value=
		"abstract,boolean,break,byte,case,catch,char,cass," +
		"const,continue,default,do,double,else,extends,false," +
		"final,finally,float,for,function,goto,if,implements," +
		"import,in,instanceof,int,interface,long,native,new," +
		"null,package,private,protected,public,return,short,static," +
		"super,switch,synchronized,this,throw,throws,transient,true," +
		"try,var,void,while,with";
}
// Thanks to Dale Anderson (skinnycowboy at hotmail dot com) for supplying the following VbScript add-on.
function loadVbScriptDefaults()
{
	document.forms[0].lang_name.value="VbScript";
	document.forms[0].eol_comment.value="'";
	document.forms[0].open_comment.value="%>";
	document.forms[0].close_comment.value="<%";
	document.forms[0].string.value="/"";
	document.forms[0].escape.value="";
	document.forms[0].keywords1.value=
		"Abs,Add,AddFolders,Addition,And,Array,Asc,AtEndOfLine," +
		"AtEndOfStream,Atn,Attributes,AvailableSpace,BuildPath," +
		"Call,Case,CBool,CByte,CCur,CDate,CDbl,Chr,CInt,Clear," +
		"CLng,Close,Column,CompareMode,Concatenation,Const," +
		"Copy,CopyFile,CopyFolder,Cos,Count,CreateFolder,CreateObject," +
		"CreateTextFile,CSng,CStr,Date,DateAddFunction,DateCreated," +
		"DateDiff,DateLastAccessed,DateLastModified,DatePart," +
		"DateSerial,DateValue,Day,Delete,DeleteFile,DeleteFolder," +
		"Description,Dictionary,Dim,Division,Do,Drive,DriveExists," +
		"DriveLetter,Drives,DriveType,Each,Else,Empty,Eqv," +
		"Erase,Err,Error,Exists,Exit,Exp,Explicit,Exponentiation," +
		"False,File,FileExists,Files,FileSystem,FileSystemObject," +
		"Filter,Fix,Folder,FolderExists,Folders,For,FormatCurrency," +
		"FormatDateTime,FormatNumber,FormatPercent,FreeSpace,Function," +
		"Function,GetAbsolutePathName,GetBaseName,GetDrive," +
		"GetDriveName,GetExtensionName,GetFile,GetFileName,GetFolder," +
		"GetObject,GetParentFolderName,GetSpecialFolder,GetTempName," +
		"HelpContext,HelpFile,Hex,Hour,If,Imp,InputBox," +
		"InStr,InStrRev,Int,Integer,Is,IsArray,IsDate,IsEmpty," +
		"IsNull,IsNumeric,IsObject,IsReady,IsRootFolder,Item," +
		"Items,Join,Key,Keys,LBound,LCase,Left,Len,Line,LoadPicture," +
		"Log,Loop,LTrim,Method,Mid,Minute,Mod,Month,MonthName," +
		"Move,MoveFile,MoveFolder,MsgBox,Muliplication,Name," +
		"Negation,Next,Not,Nothing,Now,Null,Number,Object,Oct," +
		"On,OpenAsTextStream,OpenTextFile,Operator,Option,Or," +
		"ParentFolder,Path,Precedence,Private,Property,Public," +
		"Raise,Randomize,Read,ReadAll,ReadLine,ReDim,Rem,Remove," +
		"RemoveAll,Replace,RGB,Right,Rnd,RootFolder,Round," +
		"RTrim,ScriptEngine,ScriptEngineBuildVersion,ScriptEngineMajorVersion," +
		"ScriptEngineMinorVersion,Second,Select,SerialNumber,Set," +
		"Sgn,ShareName,ShortName,ShortPath,Sin,Size,Skip,SkipLine," +
		"Source,Space,Split,Sqr,Statement,StrComp,String,StrReverse," +
		"Sub,SubFolders,Subtraction,Tan,TextStream,Then,Time," +
		"TimeSerial,TimeValue,TotalSize,Trim,True,Type," +
		"TypeName,UBound,UCase,VarType,VolumeName,Weekday,WeekdayName," +
		"Wend,While,Write,WriteBlankLines,WriteLine,Xor,Year";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值