2 using System
.Text
.RegularExpressions
;
3 using System
.Collections
;
5 namespace Mono
.Utilities
{
6 public class Colorizer
{
11 static string keywords_cs
=
12 "(\\babstract\\b|\\bevent\\b|\\bnew\\b|\\bstruct\\b|\\bas\\b|\\bexplicit\\b|\\bnull\\b|\\bswitch\\b|\\bbase\\b|\\bextern\\b|"
14 "\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|"
16 "\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|"
18 "\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|"
20 "\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|"
22 "\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|"
24 "\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|"
26 "\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|"
27 + "\\bnamespace\\b|\\bstring\\b)";
30 // currently not in use
31 static string keywords_vb
=
32 "(\\bAddHandler\\b|\\bAddressOf\\b|\\bAlias\\b|\\bAnd\\b|\\bAndAlso\\b|\\bAnsi\\b|\\bAs\\b|\\bAssembly\\b|"
34 "\\bAuto\\b|\\bBoolean\\b|\\bByRef\\b|\\bByte\\b|\\bByVal\\b|\\bCall\\b|\\bCase\\b|\\bCatch\\b|"
36 "\\bCBool\\b|\\bCByte\\b|\\bCChar\\b|\\bCDate\\b|\\bCDec\\b|\\bCDbl\\b|\\bChar\\b|\\bCInt\\b|"
38 "\\bClass\\b|\\bCLng\\b|\\bCObj\\b|\\bConst\\b|\\bCShort\\b|\\bCSng\\b|\\bCStr\\b|\\bCType\\b|"
40 "\\bDate\\b|\\bDecimal\\b|\\bDeclare\\b|\\bDefault\\b|\\bDelegate\\b|\\bDim\\b|\\bDirectCast\\b|\\bDo\\b|"
42 "\\bDouble\\b|\\bEach\\b|\\bElse\\b|\\bElseIf\\b|\\bEnd\\b|\\bEnum\\b|\\bErase\\b|\\bError\\b|"
44 "\\bEvent\\b|\\bExit\\b|\\bFalse\\b|\\bFinally\\b|\\bFor\\b|\\bFriend\\b|\\bFunction\\b|\\bGet\\b|"
46 "\\bGetType\\b|\\bGoSub\\b|\\bGoTo\\b|\\bHandles\\b|\\bIf\\b|\\bImplements\\b|\\bImports\\b|\\bIn\\b|"
48 "\\bInherits\\b|\\bInteger\\b|\\bInterface\\b|\\bIs\\b|\\bLet\\b|\\bLib\\b|\\bLike\\b|\\bLong\\b|"
50 "\\bLoop\\b|\\bMe\\b|\\bMod\\b|\\bModule\\b|\\bMustInherit\\b|\\bMustOverride\\b|\\bMyBase\\b|\\bMyClass\\b|"
52 "\\bNamespace\\b|\\bNew\\b|\\bNext\\b|\\bNot\\b|\\bNothing\\b|\\bNotInheritable\\b|\\bNotOverridable\\b|\\bObject\\b|"
54 "\\bOn\\b|\\bOption\\b|\\bOptional\\b|\\bOr\\b|\\bOrElse\\b|\\bOverloads\\b|\\bOverridable\\b|\\bOverrides\\b|"
56 "\\bParamArray\\b|\\bPreserve\\b|\\bPrivate\\b|\\bProperty\\b|\\bProtected\\b|\\bPublic\\b|\\bRaiseEvent\\b|\\bReadOnly\\b|"
58 "\\bReDim\\b|\\bREM\\b|\\bRemoveHandler\\b|\\bResume\\b|\\bReturn\\b|\\bSelect\\b|\\bSet\\b|\\bShadows\\b|"
60 "\\bShared\\b|\\bShort\\b|\\bSingle\\b|\\bStatic\\b|\\bStep\\b|\\bStop\\b|\\bString\\b|\\bStructure\\b|"
62 "\\bSub\\b|\\bSyncLock\\b|\\bThen\\b|\\bThrow\\b|\\bTo\\b|\\bTrue\\b|\\bTry\\b|\\bTypeOf\\b|"
64 "\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
67 public static string Colorize(string text
, string lang
)
69 lang
= lang
.Trim().ToLower();
72 return ColorizeXml(text
);
73 case "cs": case "c#": case "csharp":
74 return ColorizeCs(text
);
76 return ColorizeVb(text
);
81 static string ColorizeXml(string text
)
83 // Order is highly important.
85 // s/ / /g must be first, as later substitutions add required spaces
86 text
= text
.Replace(" ", " ");
88 // Find & mark XML elements
89 Regex re
= new Regex("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
90 text
= re
.Replace(text
, "{blue:<$1}{maroon:$2}{blue:$3>}");
92 // Colorize attribute strings; must be done before colorizing marked XML
93 // elements so that we don't clobber the colorized XML tags.
94 re
= new Regex ("([\"'])(.*?)\\1");
95 text
= re
.Replace (text
,
96 "$1<font color=\"purple\">$2</font>$1");
98 // Colorize marked XML elements
99 re
= new Regex("\\{(\\w*):([\\s\\S]*?)\\}");
100 //text = re.Replace(text, "<span style='color:$1'>$2</span>");
101 text
= re
.Replace(text
, "<font color=\"$1\">$2</font>");
103 // Standard Structure
104 text
= text
.Replace("\t", " ");
105 re
= new Regex("\r\n|\r|\n");
106 text
= re
.Replace(text
, "<br/>");
111 static string ColorizeCs(string text
)
113 text
= text
.Replace(" ", " ");
115 text
= text
.Replace("<", "<");
116 text
= text
.Replace(">", ">");
118 Regex re
= new Regex("\"((((?!\").)|\\\")*?)\"");
122 "<font color=\"purple\">\"$1\"</font>");
123 //"<span style='color:purple'>\"$1\"</span>");
127 ("//(((.(?!\"</font>))|\"(((?!\").)*)\"</font>)*)(\r|\n|\r\n)");
128 //("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
131 "<font color=\"green\">//$1</font><br/>");
132 // "<span style='color:green'>//$1</span><br/>");
134 re
= new Regex(keywords_cs
);
135 text
= re
.Replace(text
, "<font color=\"blue\">$1</font>");
136 //text = re.Replace(text, "<span style='color:blue'>$1</span>");
138 text
= text
.Replace("\t", " ");
139 text
= text
.Replace("\n", "<br/>");
144 static string ColorizeVb(string text
) {
145 text
= text
.Replace(" ", " ");
147 /* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
148 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
150 re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
151 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
153 re = new Regex (keywords_vb);
154 text = re.Replace (text,"<span style='color:blue'>$1</span>");
156 text
= text
.Replace("\t", " ");
157 text
= text
.Replace("\n", "<br/>");
161 static string Escape(string text
)
163 text
= text
.Replace("&", "&");
164 text
= text
.Replace(" ", " ");
165 text
= text
.Replace("<", "<");
166 text
= text
.Replace(">", ">");
167 text
= text
.Replace("\n", "<br/>");