Skip to content

Commit

Permalink
fix policheck (#685)
Browse files Browse the repository at this point in the history
* fix policheck

* use array

* update format
  • Loading branch information
TinaLiu02 authored Mar 28, 2024
1 parent 627417b commit 3685d38
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 121 deletions.
243 changes: 122 additions & 121 deletions monodoc/Mono.Utilities/colorizer.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
using System;
using System.Text.RegularExpressions;
using System.Collections;

namespace Mono.Utilities {
public class Colorizer {
//
// Syntax coloring
//

static string keywords_cs =
"(\\babstract\\b|\\bevent\\b|\\bnew\\b|\\bstruct\\b|\\bas\\b|\\bexplicit\\b|\\bnull\\b|\\bswitch\\b|\\bbase\\b|\\bextern\\b|"
+
"\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|"
+
"\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|"
+
"\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|"
+
"\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|"
+
"\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|"
+
"\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|"
+
"\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|"
+ "\\bnamespace\\b|\\bstring\\b)";
using System.Collections.Generic;
using System.Linq;

namespace Mono.Utilities
{
public class Colorizer
{
//
// Syntax coloring
//
static string[] words = { "abstract","event","new","struct","as","explicit","null","switch","base","extern",
"object","this","bool","false","operator","throw","break","finally","out","true",
"byte","fixed","override","try","case","float","params","typeof","catch","for",
"private","uint","char","foreach","protected","ulong","checked","goto","public",
"unchecked","class","if","readonly","unsafe","const","implicit","ref","ushort",
"continue","in","return","using","decimal","int","sbyte","virtual","default",
"interface","sealed","volatile","delegate","internal","short","void","do","is",
"sizeof","while","double","lock","stackalloc","else","long","static","enum",
"namespace","string" };
static string boundary = @"\b";
static string keywords_cs = "(" + string.Join("|", words.Select(word => $"{boundary}{word}{boundary}")) + ")";

#if false
// currently not in use
Expand Down Expand Up @@ -63,88 +60,92 @@ public class Colorizer {
+
"\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
#endif

public static string Colorize(string text, string lang)
{
lang = lang.Trim().ToLower();
switch (lang) {
case "xml":
return ColorizeXml(text);
case "cs": case "c#": case "csharp":
return ColorizeCs(text);
case "vb":
return ColorizeVb(text);
}
return Escape (text);
}

static string ColorizeXml(string text)
{
// Order is highly important.

// s/ / /g must be first, as later substitutions add required spaces
text = text.Replace(" ", " ");

// Find & mark XML elements
Regex re = new Regex("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
text = re.Replace(text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");

// Colorize attribute strings; must be done before colorizing marked XML
// elements so that we don't clobber the colorized XML tags.
re = new Regex ("([\"'])(.*?)\\1");
text = re.Replace (text,
"$1<font color=\"purple\">$2</font>$1");

// Colorize marked XML elements
re = new Regex("\\{(\\w*):([\\s\\S]*?)\\}");
//text = re.Replace(text, "<span style='color:$1'>$2</span>");
text = re.Replace(text, "<font color=\"$1\">$2</font>");

// Standard Structure
text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
re = new Regex("\r\n|\r|\n");
text = re.Replace(text, "<br/>");

return text;
}

static string ColorizeCs(string text)
{
text = text.Replace(" ", "&nbsp;");

text = text.Replace("<", "&lt;");
text = text.Replace(">", "&gt;");

Regex re = new Regex("\"((((?!\").)|\\\")*?)\"");

text =
re.Replace(text,
"<font color=\"purple\">\"$1\"</font>");
//"<span style='color:purple'>\"$1\"</span>");

re = new
Regex
("//(((.(?!\"</font>))|\"(((?!\").)*)\"</font>)*)(\r|\n|\r\n)");
//("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
text =
re.Replace(text,
"<font color=\"green\">//$1</font><br/>");
// "<span style='color:green'>//$1</span><br/>");

re = new Regex(keywords_cs);
text = re.Replace(text, "<font color=\"blue\">$1</font>");
//text = re.Replace(text, "<span style='color:blue'>$1</span>");

text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
text = text.Replace("\n", "<br/>");

return text;
}

static string ColorizeVb(string text) {
text = text.Replace(" ", "&nbsp;");

/* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");

public static string Colorize(string text, string lang)
{
lang = lang.Trim().ToLower();
switch (lang)
{
case "xml":
return ColorizeXml(text);
case "cs":
case "c#":
case "csharp":
return ColorizeCs(text);
case "vb":
return ColorizeVb(text);
}
return Escape(text);
}

static string ColorizeXml(string text)
{
// Order is highly important.

// s/ /&nbsp;/g must be first, as later substitutions add required spaces
text = text.Replace(" ", "&nbsp;");

// Find & mark XML elements
Regex re = new Regex("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
text = re.Replace(text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");

// Colorize attribute strings; must be done before colorizing marked XML
// elements so that we don't clobber the colorized XML tags.
re = new Regex("([\"'])(.*?)\\1");
text = re.Replace(text,
"$1<font color=\"purple\">$2</font>$1");

// Colorize marked XML elements
re = new Regex("\\{(\\w*):([\\s\\S]*?)\\}");
//text = re.Replace(text, "<span style='color:$1'>$2</span>");
text = re.Replace(text, "<font color=\"$1\">$2</font>");

// Standard Structure
text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
re = new Regex("\r\n|\r|\n");
text = re.Replace(text, "<br/>");

return text;
}

static string ColorizeCs(string text)
{
text = text.Replace(" ", "&nbsp;");

text = text.Replace("<", "&lt;");
text = text.Replace(">", "&gt;");

Regex re = new Regex("\"((((?!\").)|\\\")*?)\"");

text =
re.Replace(text,
"<font color=\"purple\">\"$1\"</font>");
//"<span style='color:purple'>\"$1\"</span>");

re = new
Regex
("//(((.(?!\"</font>))|\"(((?!\").)*)\"</font>)*)(\r|\n|\r\n)");
//("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
text =
re.Replace(text,
"<font color=\"green\">//$1</font><br/>");
// "<span style='color:green'>//$1</span><br/>");

re = new Regex(keywords_cs);
text = re.Replace(text, "<font color=\"blue\">$1</font>");
//text = re.Replace(text, "<span style='color:blue'>$1</span>");

text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
text = text.Replace("\n", "<br/>");

return text;
}

static string ColorizeVb(string text)
{
text = text.Replace(" ", "&nbsp;");

/* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
Expand All @@ -153,19 +154,19 @@ static string ColorizeVb(string text) {
re = new Regex (keywords_vb);
text = re.Replace (text,"<span style='color:blue'>$1</span>");
*/
text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
text = text.Replace("\n", "<br/>");
return text;
}

static string Escape(string text)
{
text = text.Replace("&", "&amp;");
text = text.Replace(" ", "&nbsp;");
text = text.Replace("<", "&lt;");
text = text.Replace(">", "&gt;");
text = text.Replace("\n", "<br/>");
return text;
}
}
text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
text = text.Replace("\n", "<br/>");
return text;
}

static string Escape(string text)
{
text = text.Replace("&", "&amp;");
text = text.Replace(" ", "&nbsp;");
text = text.Replace("<", "&lt;");
text = text.Replace(">", "&gt;");
text = text.Replace("\n", "<br/>");
return text;
}
}
}
23 changes: 23 additions & 0 deletions monodoc/Test/Mono.Utilities/ColorizerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using Mono.Utilities;
using Monodoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonoTests.Mono.Utilities
{
[TestFixture]
public class ColorizerTests
{
[Test]
public void ColorizeTest()
{
string text = "public int InitialCapacity { get; set; } = 100;";
string result = Colorizer.Colorize(text, "cs");
Assert.AreEqual("<font color=\"blue\">public</font>&nbsp;<font color=\"blue\">int</font>&nbsp;InitialCapacity&nbsp;{&nbsp;get;&nbsp;set;&nbsp;}&nbsp;=&nbsp;100;", result);
}
}
}

0 comments on commit 3685d38

Please sign in to comment.