7444624192040212f179c0841ed59c789dc5e07c
[mono.git] / mcs / class / monodoc / Mono.Utilities / colorizer.cs
1 using System;
2 using System.Text.RegularExpressions;
3 using System.Collections;
4
5 namespace Mono.Utilities {
6         public class Colorizer {
7                 //
8                 // Syntax coloring
9                 //
10
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|"
13                         +
14                         "\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|"
15                         +
16                         "\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|"
17                         +
18                         "\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|"
19                         +
20                         "\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|"
21                         +
22                         "\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|"
23                         +
24                         "\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|"
25                         +
26                         "\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|"
27                         + "\\bnamespace\\b|\\bstring\\b)";
28
29 #if false
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|"
33                         +
34                         "\\bAuto\\b|\\bBoolean\\b|\\bByRef\\b|\\bByte\\b|\\bByVal\\b|\\bCall\\b|\\bCase\\b|\\bCatch\\b|"
35                         +
36                         "\\bCBool\\b|\\bCByte\\b|\\bCChar\\b|\\bCDate\\b|\\bCDec\\b|\\bCDbl\\b|\\bChar\\b|\\bCInt\\b|"
37                         +
38                         "\\bClass\\b|\\bCLng\\b|\\bCObj\\b|\\bConst\\b|\\bCShort\\b|\\bCSng\\b|\\bCStr\\b|\\bCType\\b|"
39                         +
40                         "\\bDate\\b|\\bDecimal\\b|\\bDeclare\\b|\\bDefault\\b|\\bDelegate\\b|\\bDim\\b|\\bDirectCast\\b|\\bDo\\b|"
41                         +
42                         "\\bDouble\\b|\\bEach\\b|\\bElse\\b|\\bElseIf\\b|\\bEnd\\b|\\bEnum\\b|\\bErase\\b|\\bError\\b|"
43                         +
44                         "\\bEvent\\b|\\bExit\\b|\\bFalse\\b|\\bFinally\\b|\\bFor\\b|\\bFriend\\b|\\bFunction\\b|\\bGet\\b|"
45                         +
46                         "\\bGetType\\b|\\bGoSub\\b|\\bGoTo\\b|\\bHandles\\b|\\bIf\\b|\\bImplements\\b|\\bImports\\b|\\bIn\\b|"
47                         +
48                         "\\bInherits\\b|\\bInteger\\b|\\bInterface\\b|\\bIs\\b|\\bLet\\b|\\bLib\\b|\\bLike\\b|\\bLong\\b|"
49                         +
50                         "\\bLoop\\b|\\bMe\\b|\\bMod\\b|\\bModule\\b|\\bMustInherit\\b|\\bMustOverride\\b|\\bMyBase\\b|\\bMyClass\\b|"
51                         +
52                         "\\bNamespace\\b|\\bNew\\b|\\bNext\\b|\\bNot\\b|\\bNothing\\b|\\bNotInheritable\\b|\\bNotOverridable\\b|\\bObject\\b|"
53                         +
54                         "\\bOn\\b|\\bOption\\b|\\bOptional\\b|\\bOr\\b|\\bOrElse\\b|\\bOverloads\\b|\\bOverridable\\b|\\bOverrides\\b|"
55                         +
56                         "\\bParamArray\\b|\\bPreserve\\b|\\bPrivate\\b|\\bProperty\\b|\\bProtected\\b|\\bPublic\\b|\\bRaiseEvent\\b|\\bReadOnly\\b|"
57                         +
58                         "\\bReDim\\b|\\bREM\\b|\\bRemoveHandler\\b|\\bResume\\b|\\bReturn\\b|\\bSelect\\b|\\bSet\\b|\\bShadows\\b|"
59                         +
60                         "\\bShared\\b|\\bShort\\b|\\bSingle\\b|\\bStatic\\b|\\bStep\\b|\\bStop\\b|\\bString\\b|\\bStructure\\b|"
61                         +
62                         "\\bSub\\b|\\bSyncLock\\b|\\bThen\\b|\\bThrow\\b|\\bTo\\b|\\bTrue\\b|\\bTry\\b|\\bTypeOf\\b|"
63                         +
64                         "\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
65 #endif
66         
67                 public static string Colorize(string text, string lang)
68                 {
69                         lang = lang.Trim().ToLower();
70                         switch (lang) {
71                         case "xml":
72                                 return ColorizeXml(text);
73                         case "cs": case "c#": case "csharp":
74                                 return ColorizeCs(text);
75                         case "vb":
76                                 return ColorizeVb(text);
77                         }
78                         return Escape (text);
79                 }
80
81                 static string ColorizeXml(string text)
82                 {
83                         // Order is highly important.
84
85                         // s/ / /g must be first, as later substitutions add required spaces
86                         text = text.Replace(" ", " ");
87
88                         // Find & mark XML elements
89                         Regex re = new Regex("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
90                         text = re.Replace(text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");
91
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");
97
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>");
102
103                         // Standard Structure
104                         text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
105                         re = new Regex("\r\n|\r|\n");
106                         text = re.Replace(text, "<br/>");
107
108                         return text;
109                 }
110
111                 static string ColorizeCs(string text)
112                 {
113                         text = text.Replace(" ", "&nbsp;");
114
115                         text = text.Replace("<", "&lt;");
116                         text = text.Replace(">", "&gt;");
117
118                         Regex re = new Regex("\"((((?!\").)|\\\")*?)\"");
119
120                         text =
121                                 re.Replace(text,
122                                                 "<font color=\"purple\">\"$1\"</font>");
123                                                 //"<span style='color:purple'>\"$1\"</span>");
124
125                         re = new
126                                 Regex
127                                 ("//(((.(?!\"</font>))|\"(((?!\").)*)\"</font>)*)(\r|\n|\r\n)");
128                                 //("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
129                         text =
130                                 re.Replace(text,
131                                                 "<font color=\"green\">//$1</font><br/>");
132                                         //      "<span style='color:green'>//$1</span><br/>");
133
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>");
137
138                         text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
139                         text = text.Replace("\n", "<br/>");
140
141                         return text;
142                 }
143
144                 static string ColorizeVb(string text) {
145                         text = text.Replace(" ", "&nbsp;");
146
147                         /*      Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
148                                 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
149
150                                 re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
151                                 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
152
153                                 re = new Regex (keywords_vb);
154                                 text = re.Replace (text,"<span style='color:blue'>$1</span>");
155                          */
156                         text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
157                         text = text.Replace("\n", "<br/>");
158                         return text;
159                 }
160
161                 static string Escape(string text)
162                 {
163                         text = text.Replace("&", "&amp;");
164                         text = text.Replace(" ", "&nbsp;");
165                         text = text.Replace("<", "&lt;");
166                         text = text.Replace(">", "&gt;");
167                         text = text.Replace("\n", "<br/>");
168                         return text;
169                 }
170         }
171 }