11using System ;
22using System . Text . RegularExpressions ;
33using 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)" ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+
7+ namespace Mono . Utilities
8+ {
9+ public class Colorizer
10+ {
11+ //
12+ // Syntax coloring
13+ //
14+ static string [ ] words = { "abstract" , "event" , "new" , "struct" , "as" , "explicit" , "null" , "switch" , "base" , "extern" ,
15+ "object" , "this" , "bool" , "false" , "operator" , "throw" , "break" , "finally" , "out" , "true" ,
16+ "byte" , "fixed" , "override" , "try" , "case" , "float" , "params" , "typeof" , "catch" , "for" ,
17+ "private" , "uint" , "char" , "foreach" , "protected" , "ulong" , "checked" , "goto" , "public" ,
18+ "unchecked" , "class" , "if" , "readonly" , "unsafe" , "const" , "implicit" , "ref" , "ushort" ,
19+ "continue" , "in" , "return" , "using" , "decimal" , "int" , "sbyte" , "virtual" , "default" ,
20+ "interface" , "sealed" , "volatile" , "delegate" , "internal" , "short" , "void" , "do" , "is" ,
21+ "sizeof" , "while" , "double" , "lock" , "stackalloc" , "else" , "long" , "static" , "enum" ,
22+ "namespace" , "string" } ;
23+ static string boundary = @"\b" ;
24+ static string keywords_cs = "(" + string . Join ( "|" , words . Select ( word => $ "{ boundary } { word } { boundary } ") ) + ")" ;
2825
2926#if false
3027// currently not in use
@@ -63,88 +60,92 @@ public class Colorizer {
6360 +
6461 "\\ bUnicode\\ b|\\ bUntil\\ b|\\ bVariant\\ b|\\ bWhen\\ b|\\ bWhile\\ b|\\ bWith\\ b|\\ bWithEvents\\ b|\\ bWriteOnly\\ b|\\ bXor\\ b)" ;
6562#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:<$1}{maroon:$2}{blue:$3>}" ) ;
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 " , " " ) ;
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 ( " " , " " ) ;
114-
115- text = text . Replace ( "<" , "<" ) ;
116- text = text . Replace ( ">" , ">" ) ;
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 " , " " ) ;
139- text = text . Replace ( "\n " , "<br/>" ) ;
140-
141- return text ;
142- }
143-
144- static string ColorizeVb ( string text ) {
145- text = text . Replace ( " " , " " ) ;
146-
147- /* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
63+
64+ public static string Colorize ( string text , string lang )
65+ {
66+ lang = lang . Trim ( ) . ToLower ( ) ;
67+ switch ( lang )
68+ {
69+ case "xml" :
70+ return ColorizeXml ( text ) ;
71+ case "cs" :
72+ case "c#" :
73+ 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:<$1}{maroon:$2}{blue:$3>}" ) ;
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 " , " " ) ;
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 ( " " , " " ) ;
114+
115+ text = text . Replace ( "<" , "<" ) ;
116+ text = text . Replace ( ">" , ">" ) ;
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 " , " " ) ;
139+ text = text . Replace ( "\n " , "<br/>" ) ;
140+
141+ return text ;
142+ }
143+
144+ static string ColorizeVb ( string text )
145+ {
146+ text = text . Replace ( " " , " " ) ;
147+
148+ /* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
148149 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
149150
150151 re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
@@ -153,19 +154,19 @@ static string ColorizeVb(string text) {
153154 re = new Regex (keywords_vb);
154155 text = re.Replace (text,"<span style='color:blue'>$1</span>");
155156 */
156- text = text . Replace ( "\t " , " " ) ;
157- text = text . Replace ( "\n " , "<br/>" ) ;
158- return text ;
159- }
160-
161- static string Escape ( string text )
162- {
163- text = text . Replace ( "&" , "&" ) ;
164- text = text . Replace ( " " , " " ) ;
165- text = text . Replace ( "<" , "<" ) ;
166- text = text . Replace ( ">" , ">" ) ;
167- text = text . Replace ( "\n " , "<br/>" ) ;
168- return text ;
169- }
170- }
157+ text = text . Replace ( "\t " , " " ) ;
158+ text = text . Replace ( "\n " , "<br/>" ) ;
159+ return text ;
160+ }
161+
162+ static string Escape ( string text )
163+ {
164+ text = text . Replace ( "&" , "&" ) ;
165+ text = text . Replace ( " " , " " ) ;
166+ text = text . Replace ( "<" , "<" ) ;
167+ text = text . Replace ( ">" , ">" ) ;
168+ text = text . Replace ( "\n " , "<br/>" ) ;
169+ return text ;
170+ }
171+ }
171172}
0 commit comments