|
发表于 2004-9-20 10:39:36
|
显示全部楼层
[quote:06bfd53c70="MichaelBibby"]加一些解释会更好
KanKer, 下次记得用code格式格式化代码 [/quote]
版主能否修改一下论坛代码,将code中的代码显亮一下,这样不好看啊!
下面是我以前写的,javascript显亮Delphi代码,也许改改就能用。
----------------------------
[code:1]/*
*
* Copyright (c) 2003, Ander ChongQing China
* All rights reserved.
*
* Filename:hl.js
* Summary:Highlighted Delphi codes
*
* Version:1.0
* Author: Ander
* Email: [email protected]
* Creation date: 2003-2-8
*
//////////////////////////////////
*
* Function:highlighting Delphi codes by HTML tag
* Import:Delphi codes
* Return Value:highlighted Delphi codes by HTML tag
*
*/
function Highlight(sCode) {
var Color = new Array(6);//highlighted text style
var i,j;
//Delphi Keywords
var KeyWrd = new Array("and", "array ", "as", "asm", "begin", "case", "class", "const",
"constructor", "destructor", "dispinterface", "div", "do", "downto", "else",
"end", "except", "exports", "file", "finalization", "finally", "for",
"function", "goto", "if", "implementation", "in", "inherited",
"initialization", "inline", "interface", "is", "label", "library", "mod",
"nil", "not", "object", "of", "or", "out", "packed", "procedure", "program",
"property", "raise", "record", "repeat", "resourcestring", "set", "shl",
"shr", "string", "then", "threadvar", "to", "try", "type", "unit", "until",
"uses", "var", "while", "with", "xor", "private", "protected", "public",
"published", "automated");
//keyword style
Color[0] = "<strong>";
Color[1] = "</strong>";
//number style
Color[2] = "<font color=\"#0000FF\">";
Color[3] = "</font>";
//comment style
Color[4] = "<em><font color=\"#0000FF\">";
Color[5] = "</font></em>";
var KeyReg;
var numReg = /([\W])([0-9]+)/g;
var LCReg = /(\/\/+[^\r]*)/g;
var MCReg = /({[^}]*})/g;
//special symbols
sCode = sCode.replace(/\&/g, "&amp;");
sCode = sCode.replace(/\"/g, "&quot;");
sCode = sCode.replace(/\r|\n/g, "\r<br/>");
sCode = sCode.replace(/ /g, "&nbsp;");
sCode = sCode.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
//highlight number
sCode = sCode.replace(numReg,"$1" + Color[2] + "$2" + Color[3]);
//highlight keywords
for (j = 0; j < KeyWrd.length; j++) {
KeyReg = new RegExp("\\b" + KeyWrd[j] + "\\b", "gi");
sCode = sCode.replace(KeyReg, Color[0] + KeyWrd[j] + Color[1]);
KeyReg = null;
}
//highlight comment
sCode = sCode.replace(LCReg, Color[4] + "$1" + Color[5]);
sCode = sCode.replace(MCReg, Color[4] + "$1" + Color[5]);
return (sCode);
}[/code:1] |
|