[Mono-docs-list] Rendering <example/> blocks

Jonathan Pryor jonpryor@vt.edu
Thu, 04 Nov 2004 21:40:27 -0500


--=-XF9Z656QACrgKUTJ+LD1
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2004-11-03 at 07:37, Joshua Tauberer wrote:
> This is my preference.  <example> blocks should be able to be rendered 
> with or without the colorizer so that we can still get offline HTML 
> output without the help of extension functions.   But, there is too much 
> escaping going on.
>
> The example blocks in the files should be considered as having raw code, 
> not HTML.  The renderer should take care of making sure that &lt;'s come 
> through as less than signs in the final output.  So, you shouldn't 
> double-esacpe.

Attached is a patch to monodoc to implement this default escaping
behavior.  It also has a fix to the XML colorizer so that the output
isn't completely garbled.

OK to commet?

Thanks,
 - Jon


--=-XF9Z656QACrgKUTJ+LD1
Content-Disposition: attachment; filename=colorizer.patch
Content-Type: text/x-patch; name=colorizer.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

Index: browser/ChangeLog
===================================================================
RCS file: /cvs/public/monodoc/browser/ChangeLog,v
retrieving revision 1.139
diff -u -p -r1.139 ChangeLog
--- browser/ChangeLog	3 Nov 2004 07:26:25 -0000	1.139
+++ browser/ChangeLog	5 Nov 2004 01:18:44 -0000
@@ -1,3 +1,13 @@
+2004-11-04  Jonathan Pryor <jonpryor@vt.edu>
+
+	* colorizer.cs: If a language is unrecognized, escape the XML so that it
+	  will be properly rendered within monodoc; 
+		- Fix ColorizeXml.  Due to an ordering dependency, the regex to colorize
+		  double-quoted strings was also replacing the attributes used in a prior
+		  substitution (to colorize XML tags).  This garbled the XML, making it
+		  unreadable.  Handling double-quoted strings earlier fixes this.
+		- Cleanup braces to follow code conventions.
+
 2004-11-01  Richard Torkar <richard.torkar@htu.se>
 
 	* index.cs: Make sure we catch the System.UnauthorizedAccessException 
Index: browser/colorizer.cs
===================================================================
RCS file: /cvs/public/monodoc/browser/colorizer.cs,v
retrieving revision 1.1
diff -u -p -r1.1 colorizer.cs
--- browser/colorizer.cs	10 Dec 2003 08:37:29 -0000	1.1
+++ browser/colorizer.cs	5 Nov 2004 01:18:45 -0000
@@ -62,41 +62,52 @@ namespace Mono.Utilities {
 			+
 			"\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
 
-		public static string Colorize(string text, string lang) {
-			if (lang == "xml")
+		public static string Colorize(string text, string lang)
+		{
+			lang = lang.Trim().ToLower();
+			switch (lang) {
+			case "xml":
 				return ColorizeXml(text);
-			else if (lang == "cs"||lang.Trim().ToLower()=="c#"||lang.Trim().ToLower()=="csharp")
+			case "cs": case "c#": case "csharp":
 				return ColorizeCs(text);
-			else if (lang == "vb")
+			case "vb":
 				return ColorizeVb(text);
-			else
-				return text;
+			}
+			return Escape (text);
 		}
-		static string ColorizeXml(string 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;");
-			Regex re = new Regex("\r\n|\r|\n");
-			text = re.Replace(text, "_br_");
 
-			re = new Regex("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
-			text =
-				re.Replace(text, "{blue:&lt;$1}{maroon:$2}{blue:$3&gt;}");
+			// 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>");
 
-			re = new Regex("\"(.*?)\"");
-			text =
-				re.Replace(text,
-						"\"<font color=\"purple\">$1</font>\"");
-						//"\"<span style='color:purple'>$1</span>\"");
-
-
+			// Standard Structure
 			text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
-			text = text.Replace("_br_", "<br>");
+			re = new Regex("\r\n|\r|\n");
+			text = re.Replace(text, "<br/>");
+
 			return text;
 		}
-		static string ColorizeCs(string text) {
+
+		static string ColorizeCs(string text)
+		{
 			text = text.Replace(" ", "&nbsp;");
 
 			text = text.Replace("<", "&lt;");
@@ -145,5 +156,14 @@ namespace Mono.Utilities {
 			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;
+		}
 	}
 }

--=-XF9Z656QACrgKUTJ+LD1--